Skip to content

I-Schulze Results

Abstract class used to create a schulze results object instance for schulze polls.

ISchulzeResults

Bases: abc.ABC

The expected schema for Schulze voting results. It provides a method to calculate the results + some information getters that will be used in template to render results.

It is the type expected by the Schulze results service.

Source code in apps/votes_results/classes/schulze_results/i_schulze_results.py
 8
 9
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
class ISchulzeResults(abc.ABC):
    """The expected schema for Schulze voting results. It 
    provides a method to calculate the results + some information 
    getters that will be used in template to render results.

    It is the type expected by the Schulze results service."""

    def __init__(self, poll: PollModel) -> None:
        self.poll = poll        

    @abc.abstractmethod
    def calculate(self) -> None:
        """Do once the calculation of the results and store all
        information locally (so you avoid to repeat DB queries 
        and heavy calculations)"""
        pass

    @abc.abstractmethod
    def get_votes(self) -> List[SchulzeVoteModel]:
        """Return the list of all submitted votes."""
        pass

    @abc.abstractmethod
    def get_sorted_options(self) -> List[List[PollOptionModel]]:
        """The actual sorted algorithm results, from the better option
        to the worse. We use a list of list because there could be equal 
        scores."""
        pass

    def get_preference_matrix_cell(self, a: PollOptionModel, b: PollOptionModel) -> List[int]:
        """Return the number of times a is prefered to b."""

        if a == b:
            return "/"

        count = 0
        for vote in self.get_votes():
            order = vote.get_order()
            if order.index(str(a.id)) < order.index(str(b.id)):
                count += 1
        return count

calculate() abstractmethod

Do once the calculation of the results and store all information locally (so you avoid to repeat DB queries and heavy calculations)

Source code in apps/votes_results/classes/schulze_results/i_schulze_results.py
18
19
20
21
22
23
@abc.abstractmethod
def calculate(self) -> None:
    """Do once the calculation of the results and store all
    information locally (so you avoid to repeat DB queries 
    and heavy calculations)"""
    pass

get_preference_matrix_cell(a, b)

Return the number of times a is prefered to b.

Source code in apps/votes_results/classes/schulze_results/i_schulze_results.py
37
38
39
40
41
42
43
44
45
46
47
48
def get_preference_matrix_cell(self, a: PollOptionModel, b: PollOptionModel) -> List[int]:
    """Return the number of times a is prefered to b."""

    if a == b:
        return "/"

    count = 0
    for vote in self.get_votes():
        order = vote.get_order()
        if order.index(str(a.id)) < order.index(str(b.id)):
            count += 1
    return count

get_sorted_options() abstractmethod

The actual sorted algorithm results, from the better option to the worse. We use a list of list because there could be equal scores.

Source code in apps/votes_results/classes/schulze_results/i_schulze_results.py
30
31
32
33
34
35
@abc.abstractmethod
def get_sorted_options(self) -> List[List[PollOptionModel]]:
    """The actual sorted algorithm results, from the better option
    to the worse. We use a list of list because there could be equal 
    scores."""
    pass

get_votes() abstractmethod

Return the list of all submitted votes.

Source code in apps/votes_results/classes/schulze_results/i_schulze_results.py
25
26
27
28
@abc.abstractmethod
def get_votes(self) -> List[SchulzeVoteModel]:
    """Return the list of all submitted votes."""
    pass