-
Notifications
You must be signed in to change notification settings - Fork 1
/
contest.py
98 lines (86 loc) · 4.4 KB
/
contest.py
1
2
3
4
5
6
7
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
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
"""Contest module for handling individual contest data."""
from enum import Enum
from typing import Dict
from typing import List
class ContestType(Enum):
"""Enum indicating what type of vote variation was used in the contest."""
# TODO: Add additional vote variations from VVSG.
PLURALITY = 0
MAJORITY = 1
class Contest:
"""Contest information from a single contest within an Election.
Attributes:
contest_ballots (int): Total number of ballots cast in the contest.
candidates (List[str]): List of candidates in the contest.
num_candidates (int): Number of candidates in the contest.
num_winners (int): Number of winners desired from contest.
reported_winners (List[str]): Reported winners from contest. Must be candidates from list
of candidates, and length should match number of winners.
contest_type (ContestType): What type of contest is this?
tally (Dict[str, int]): Reported tally from contest as a dictionary of candidates to
reported votes received.
winner_prop (float): Proportion of ballots cast for reported winner. Currently for first
winner listed in reported winners.
"""
contest_ballots: int
candidates: List[str]
num_candidates: int
num_winners: int
reported_winners: List[str]
contest_type: ContestType
tally: Dict[str, int]
winner_prop: float
def __init__(self, contest_ballots: int, tally: Dict[str, int], num_winners: int, reported_winners: List[str],
contest_type: ContestType):
if type(contest_ballots) is not int:
raise TypeError('contest_ballots must be integer.')
if contest_ballots < 1:
raise ValueError('contest_ballots must be at least 1.')
if type(tally) is not dict:
raise TypeError('tally must be a dict.')
else:
for k, v in tally.items():
if type(k) is not str or type(v) is not int:
raise TypeError('tally must contain str keys and int values')
if sum(tally.values()) > contest_ballots:
raise ValueError('tally total is greater than contest_ballots.')
if sum(tally.values()) < 1:
raise ValueError('tally must contain a total of at least 1 ballot.')
if type(num_winners) is not int:
raise TypeError('num_winners must be integer.')
if num_winn