Skip to content

Poll Result

Tool class used to create a results object instance for single option polls.

PollResult dataclass

Compute a result of a closed poll.

Source code in apps/votes_results/classes/poll_result.py
 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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
@dataclass
class PollResult: 
    """
    Compute a result of a closed poll. 
    """

    poll: PollModel
    """
    The poll the result is about
    """


    def __init__(self, poll: PollModel) -> None:
        self.poll: PollModel = poll
        self.__memoized_result: Optional[List[PollResultVoice]] = None

    def get_sorted_options(self) -> List[PollResultVoice]:
        """
        Result as an ordered list of pairs (choice, n_votes).

        Result is computed only once for efficiency reasons (and then memoized). 
        If you want to compute it again, create another PollResult instance.
        """

        # if it exists, return memoized result
        if self.__memoized_result is not None:
            return self.__memoized_result

        # calculate result
        self.__memoized_result = []
        for option in PollOptionModel.objects.filter(poll_fk=self.poll.id).all():
            self.__memoized_result.append(PollResultVoice(option))

        # sort by (decreasing) n votes
        def n_votes(voice: PollResultVoice):
            return voice.n_votes
        self.__memoized_result.sort(reverse=True, key=n_votes)


        index =0 
        aux_n_position=[]
        pos = 1 #temporary position
        n_pos = 1 #number of option on the same position
        for option in self.__memoized_result:
            if(index==0):
                option.position=pos
            elif(self.__memoized_result[index-1].n_votes==option.n_votes):
                option.position=pos
                n_pos+=1
            else:
                pos += n_pos
                option.position = pos
                n_pos=1
            aux_n_position.append([pos,option])
            index += 1

        index = 0
        n_position=[]
        while index <len(aux_n_position)-1:
            if(aux_n_position[index][0]!=aux_n_position[index+1][0]):
                n_position.append(aux_n_position[index])
            index+=1
        n_position.append(aux_n_position[-1])

        return {"results":self.__memoized_result,
                "positions":n_position
                }

poll: PollModel = poll class-attribute instance-attribute

The poll the result is about

get_sorted_options()

Result as an ordered list of pairs (choice, n_votes).

Result is computed only once for efficiency reasons (and then memoized). If you want to compute it again, create another PollResult instance.

Source code in apps/votes_results/classes/poll_result.py
 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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def get_sorted_options(self) -> List[PollResultVoice]:
    """
    Result as an ordered list of pairs (choice, n_votes).

    Result is computed only once for efficiency reasons (and then memoized). 
    If you want to compute it again, create another PollResult instance.
    """

    # if it exists, return memoized result
    if self.__memoized_result is not None:
        return self.__memoized_result

    # calculate result
    self.__memoized_result = []
    for option in PollOptionModel.objects.filter(poll_fk=self.poll.id).all():
        self.__memoized_result.append(PollResultVoice(option))

    # sort by (decreasing) n votes
    def n_votes(voice: PollResultVoice):
        return voice.n_votes
    self.__memoized_result.sort(reverse=True, key=n_votes)


    index =0 
    aux_n_position=[]
    pos = 1 #temporary position
    n_pos = 1 #number of option on the same position
    for option in self.__memoized_result:
        if(index==0):
            option.position=pos
        elif(self.__memoized_result[index-1].n_votes==option.n_votes):
            option.position=pos
            n_pos+=1
        else:
            pos += n_pos
            option.position = pos
            n_pos=1
        aux_n_position.append([pos,option])
        index += 1

    index = 0
    n_position=[]
    while index <len(aux_n_position)-1:
        if(aux_n_position[index][0]!=aux_n_position[index+1][0]):
            n_position.append(aux_n_position[index])
        index+=1
    n_position.append(aux_n_position[-1])

    return {"results":self.__memoized_result,
            "positions":n_position
            }

PollResultVoice dataclass

A single voice of the result. It's made of a PollOption and the number of received votes.

N votes are calculated during object building.

Source code in apps/votes_results/classes/poll_result.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
@dataclass
class PollResultVoice:
    """
    A single voice of the result. It's made of a PollOption and the number
    of received votes.

    N votes are calculated during object building.
    """

    n_votes: int
    """
    Number of votes the option received
    """

    option: PollOptionModel
    """
    The voted option
    """
    position:int
    """
    Position in results
    """


    def __init__(self, poll_option: PollOptionModel) -> None:
        self.n_votes = PollResultVoice.__count_n_votes(poll_option)
        self.position = 0
        self.option = poll_option

    @staticmethod
    def __count_n_votes(poll_option: PollOptionModel) -> int:
        return VoteModel.objects.filter(poll_option=poll_option.id).count()

n_votes = PollResultVoice.__count_n_votes(poll_option) class-attribute instance-attribute

Number of votes the option received

option = poll_option class-attribute instance-attribute

The voted option

position = 0 class-attribute instance-attribute

Position in results