Skip to content

Schulze Results Adapter

Class used to create a schulze results object instance for schulze polls.

SchulzeResultsAdapter

Bases: ISchulzeResults

Class used to manage data for a Schulze poll results

Source code in apps/votes_results/classes/schulze_results/schulze_results_adapter.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class SchulzeResultsAdapter(ISchulzeResults):
    """Class used to manage data for a Schulze poll results"""

    schulze_votes: List[SchulzeVoteModel]
    """List containing all the votes for the specified schulze poll."""

    schulze_results: List[List[PollOptionModel]]
    """List containing all the results in the winning order."""

    schulze_str_options: List[str]
    """List containing all the options of the poll as list of string ids."""

    all_schulze_rankings: List[List[List[str]]]
    """List containing all the order rankings of the poll as list of string ids."""

    def get_votes(self) -> List[SchulzeVoteModel]:
        """Returns all the schulze votes of the poll as list"""
        return self.schulze_votes

    def get_sorted_options(self) -> List[List[PollOptionModel]]:
        """Returns the schulze poll results as list of List of PollOptionModel (in case of parity)"""
        return self.schulze_results

    def calculate(self) -> None:
        """Populates the class variables from the poll"""

        self.set_votes()
        self.set_options()
        self.set_all_rankings()
        # now we have all the elements to calcluate the results with the algorithm
        self.set_schulze_results()

    def set_votes(self) -> None:
        """Populates the votes list"""

        try:
            self.schulze_votes = list(SchulzeVoteModel.objects.filter(poll=self.poll))
        except ObjectDoesNotExist:
            raise VoteDoesNotExistException(f"The vote model does not exist.")

        if len(self.schulze_votes) < 1:
            raise PollNotYetVodedException()

    def set_options(self) -> None:
        """Populates options list as string of ids"""
        self.schulze_str_options = self.schulze_votes[0].get_order_as_ids()

    def set_all_rankings(self) -> None:
        """Populates the list of all ranks"""

        all_rankings: List[List[List[str]]] = []
        for vote in self.schulze_votes:
            vote_list: List[List[str]] = [[i] for i in vote.get_order()]
            all_rankings.append(vote_list)

        self.all_schulze_rankings = all_rankings

    def set_schulze_results(self) -> None:
        """Calculates all the results"""

        # from the schulze algorithm, we need to "candidate names" and "ballots"
        # respectively 'schulze_str_options' and 'all_schulze_rankings'
        result: List[List[str]] = schulze.compute_schulze_ranking(self.schulze_str_options, self.all_schulze_rankings)

        rankings: List[List[PollOptionModel]] = []
        for id in result:
            same_rank_list: List[PollOptionModel] = []
            for same_rank in id:
                option: PollOptionModel = PollOptionModel.objects.get(id=int(same_rank))
                same_rank_list.append(option)
            rankings.append(same_rank_list)

        self.schulze_results = rankings

all_schulze_rankings: List[List[List[str]]] class-attribute

List containing all the order rankings of the poll as list of string ids.

schulze_results: List[List[PollOptionModel]] class-attribute

List containing all the results in the winning order.

schulze_str_options: List[str] class-attribute

List containing all the options of the poll as list of string ids.

schulze_votes: List[SchulzeVoteModel] class-attribute

List containing all the votes for the specified schulze poll.

calculate()

Populates the class variables from the poll

Source code in apps/votes_results/classes/schulze_results/schulze_results_adapter.py
33
34
35
36
37
38
39
40
def calculate(self) -> None:
    """Populates the class variables from the poll"""

    self.set_votes()
    self.set_options()
    self.set_all_rankings()
    # now we have all the elements to calcluate the results with the algorithm
    self.set_schulze_results()

get_sorted_options()

Returns the schulze poll results as list of List of PollOptionModel (in case of parity)

Source code in apps/votes_results/classes/schulze_results/schulze_results_adapter.py
29
30
31
def get_sorted_options(self) -> List[List[PollOptionModel]]:
    """Returns the schulze poll results as list of List of PollOptionModel (in case of parity)"""
    return self.schulze_results

get_votes()

Returns all the schulze votes of the poll as list

Source code in apps/votes_results/classes/schulze_results/schulze_results_adapter.py
25
26
27
def get_votes(self) -> List[SchulzeVoteModel]:
    """Returns all the schulze votes of the poll as list"""
    return self.schulze_votes

set_all_rankings()

Populates the list of all ranks

Source code in apps/votes_results/classes/schulze_results/schulze_results_adapter.py
57
58
59
60
61
62
63
64
65
def set_all_rankings(self) -> None:
    """Populates the list of all ranks"""

    all_rankings: List[List[List[str]]] = []
    for vote in self.schulze_votes:
        vote_list: List[List[str]] = [[i] for i in vote.get_order()]
        all_rankings.append(vote_list)

    self.all_schulze_rankings = all_rankings

set_options()

Populates options list as string of ids

Source code in apps/votes_results/classes/schulze_results/schulze_results_adapter.py
53
54
55
def set_options(self) -> None:
    """Populates options list as string of ids"""
    self.schulze_str_options = self.schulze_votes[0].get_order_as_ids()

set_schulze_results()

Calculates all the results

Source code in apps/votes_results/classes/schulze_results/schulze_results_adapter.py
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def set_schulze_results(self) -> None:
    """Calculates all the results"""

    # from the schulze algorithm, we need to "candidate names" and "ballots"
    # respectively 'schulze_str_options' and 'all_schulze_rankings'
    result: List[List[str]] = schulze.compute_schulze_ranking(self.schulze_str_options, self.all_schulze_rankings)

    rankings: List[List[PollOptionModel]] = []
    for id in result:
        same_rank_list: List[PollOptionModel] = []
        for same_rank in id:
            option: PollOptionModel = PollOptionModel.objects.get(id=int(same_rank))
            same_rank_list.append(option)
        rankings.append(same_rank_list)

    self.schulze_results = rankings

set_votes()

Populates the votes list

Source code in apps/votes_results/classes/schulze_results/schulze_results_adapter.py
42
43
44
45
46
47
48
49
50
51
def set_votes(self) -> None:
    """Populates the votes list"""

    try:
        self.schulze_votes = list(SchulzeVoteModel.objects.filter(poll=self.poll))
    except ObjectDoesNotExist:
        raise VoteDoesNotExistException(f"The vote model does not exist.")

    if len(self.schulze_votes) < 1:
        raise PollNotYetVodedException()