-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeResults.py
178 lines (128 loc) · 4.44 KB
/
makeResults.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import csv
import math
import json
# Remember that the essay Programmer's Guide to Proportional Representation needs to be updated with the correct lines if this file is edited.
file = open("./state_allocations.json", "r")
allocations = json.load(file)
file.close()
def getElectors(state, year):
try:
census = math.floor((int(year) - 1) / 10) - 197
except ValueError:
print(year)
try:
return allocations[state][census]
except KeyError:
return 10
class List:
def __init__(self, name, votes, candidate):
self.name = name
self.votes = int(votes)
self.candidate = candidate
self.real = not(candidate == "Invalid")
class Nominee:
def __init__(self, name, party, votes):
self.name = name
self.party = party
self.votes = votes
self.extra_votes = votes
self.extra_seat = False
self.seats = 0
class Election:
def __init__(self, state, year):
self.lists: 'list[List]' = []
self.state = state
self.year = year
self.total_seats = getElectors(state, year)
self.candidates: 'list[Nominee]' = []
def parties(self):
pts = []
for candidate in self.candidates:
pts.append(
{"name": candidate.party,
"seats": candidate.seats,
"votes": candidate.votes,
"extra_votes": candidate.extra_votes,
"extra_seat": candidate.extra_seat
}
)
return pts
def stats(self):
results = {
"name": self.state + " " + self.year,
"total_seats": self.total_seats,
"total_votes": self.total_votes(),
"gallagher_index": self.gallagher_index()
}
return results
def total_votes(self):
total = 0
for list in self.lists:
if list.real:
total += int(list.votes)
return total
def gallagher_index(self):
cs = 0
for candidate in self.candidates:
cs += pow((candidate.votes / self.total_votes() * 100) -
(candidate.seats / self.total_seats * 100), 2)
return pow(0.5 * cs, 0.5)
# makeResults.py
def run(self):
nds = {}
for list in self.lists:
if list.real:
try:
nds[list.candidate] += list.votes
except KeyError:
nds[list.candidate] = list.votes
for candidate in nds:
self.candidates.append(
Nominee(candidate, self.partyOfCandidate(candidate), nds[candidate]))
quota = math.floor(self.total_votes() / self.total_seats)
assigned = 0
for candidate in self.candidates:
candidate.seats = math.floor(candidate.votes / quota)
candidate.extra_votes = candidate.votes - (candidate.seats * quota)
assigned += candidate.seats
def extra_votes(elem):
return elem.extra_votes
self.candidates.sort(key=extra_votes, reverse=True)
while assigned < self.total_seats:
c = self.candidates[0]
c.extra_seat = True
c.seats += 1
self.candidates.append(c)
del self.candidates[0]
assigned += 1
def partyOfCandidate(self, candidate: str):
party = (-1, "")
for list in self.lists:
if list.votes > party[0] and list.candidate == candidate:
party = (list.votes, list.name)
if party[1] == "Independent":
party = (party[0], "Independent - " + candidate)
return party[1]
elections = {}
with open("./data/database.csv", "r") as file:
lines = csv.reader(file)
for year, state, candidate, party, votes in lines:
if "Header" in year:
continue
try:
elections[state + "-" +
year].lists.append(List(party, votes, candidate))
except KeyError:
election = Election(state, year)
election.lists.append(List(party, votes, candidate))
elections[state + "-" + year] = election
for election in elections.values():
file = open("./data/" + election.year + "/" +
election.state + ".json", "w")
election.run()
data = {
"parties": election.parties(),
"stats": election.stats()
}
file.write(json.dumps(data))
file.close()