-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTabulator.py
304 lines (258 loc) · 11.6 KB
/
Tabulator.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#############################################################
# Tabulator.py #
# Created by: Alton Zheng & Yun Park #
# Edited by; Lisa Lee #
# Copyright Elections Council 2014 #
#############################################################
import json
import csv
import copy
from pprint import pprint
from Race import *
class Candidate:
def __init__(self, number, name, position, party):
self.number = number
self.name = name
self.position = position
self.party = party
self.score = 0
self.ballots = []
self.state = RUNNING
self.quotaPlace = 0 # Maintains the order of candidates to quota'd
def __eq__(self, other):
return (other.number == self.number) and (other.name == self.name) and (other.position == self.position) and (other.party == self.party)
def __str__(self):
return str(self.number) + ". " + self.name + " " + str(self.position) + " " + self.party + " SCORE: " + str(self.score) + " STATE: " + STATES[self.state]
def __hash__(self):
return self.number
def getName(self):
return self.name
class Election:
def __init__(self, frame):
# self.ballots is an array of Ballot objects
self.ballots = []
# self.candidates is a dictionary mapping positions to an array of Candidate objects
self.candidates = {}
# self.remove is a list of Candidate names who have been disqualified or withdrawn before the election
self.remove = []
# self.frame is the GUI frame that's displaying the election
self.frame = frame
self.race = None
self.finished = False
def loadBallotsFromJSONFile(self, filepath):
self.ballots = []
with open(filepath) as data_file:
data = json.load(data_file)
for json_ballot in data["ballots"]:
# Create a new dictionary that has keys as integers instead of strings
votes = {}
for key in json_ballot.keys():
try:
votes[int(key)] = json_ballot[key]
except ValueError:
print('Invalid key in json: ' + key)
ballot = Ballot(votes)
self.ballots.append(ballot)
# Same as loadBallotsFromJSONFile, except reading from CSV
def loadBallotsFromCSVFile(self, filepath):
self.ballots = []
with open(filepath) as csv_data:
data = csv.reader(csv_data)
rownum = 0
for row in data:
votes = {}
# Skip through two empty rows on top and save header row.
if rownum == 0:
header = row
rownum += 1
continue
else:
colnum = 0
for col in row:
# print '%-8s: %s' % (header[colnum], col)
# Look for the position asked in the question
position = self.findPositionValue(header[colnum])
if position not in range(1, 7):
colnum += 1
continue
# Find the candidates number based on the answer
# Returns None if candidate not found
number = self.findCandidateNumber(position, col.split('|', 1)[0][:-1])
# print number
if number == None:
colnum += 1
continue
# Include candidate in vote
print(number)
if position not in votes.keys():
votes[position] = [number]
else:
votes[position].append(number)
colnum += 1
# Create a new ballot with the votes and add it to overall ballots
ballot = Ballot(votes)
self.ballots.append(ballot)
rownum += 1
def findCandidateNumber(self, position, name):
candidateList = self.candidates[position]
# Remove all characters that aren't letters, numbers, '', -, !
newName = ''
for c in name:
if (c >= 'A' and c <= 'Z') or (c >= 'a' and c <= 'z') or (c >= '0' and c <= '9') or c == '-' or c == '"' or c == ' ':
newName += c
for c in candidateList:
if c.getName() == newName:
return c.number
return None
def loadCandidatesFromJSONFile(self, filepath):
with open(filepath) as data_file:
data = json.load(data_file)
self.candidates[SENATOR] = []
for candidate in data["senator"]:
self.__addJSONCandidate__(candidate, SENATOR)
self.candidates[PRESIDENT] = []
for candidate in data["president"]:
self.__addJSONCandidate__(candidate, PRESIDENT)
self.candidates[EXECUTIVE_VP] = []
for candidate in data["executive_vp"]:
self.__addJSONCandidate__(candidate, EXECUTIVE_VP)
self.candidates[EXTERNAL_VP] = []
for candidate in data["external_vp"]:
self.__addJSONCandidate__(candidate, EXTERNAL_VP)
self.candidates[ACADEMIC_VP] = []
for candidate in data["academic_vp"]:
self.__addJSONCandidate__(candidate, ACADEMIC_VP)
self.candidates[STUDENT_ADVOCATE] = []
for candidate in data["student_advocate"]:
self.__addJSONCandidate__(candidate, STUDENT_ADVOCATE)
# Reads candidate information from a textfile, assuming the format of
# Position:
# candidate name | candidate party
def loadCandidatesFromTextFile(self, filepath):
data = open(filepath)
line = data.readline()
number = 1
oldPosition = 0
while not line == '':
position = self.findPositionValue(line.split(':')[0])
# If position is invalid, it means it read in a name
# print position
if position == 0:
name = line.split('|', 1)[0][:-1]
party = line.split('|', 2)[1][1:-1]
c = Candidate(number, name, oldPosition, party)
if oldPosition not in self.candidates.keys():
self.candidates[oldPosition] = [c]
else:
self.candidates[oldPosition].append(c)
number += 1
else:
oldPosition = position
line = data.readline()
# Returns position value or 0 if invalid position
def findPositionValue(self, position):
if "Executive Vice President" in position or "EVP" in position:
return EXECUTIVE_VP
elif "External Affairs Vice President" in position or "EAVP" in position:
return EXTERNAL_VP
elif "Academic Affairs Vice President" in position or "AAVP" in position:
return ACADEMIC_VP
elif "President" in position:
return PRESIDENT
elif "Student Advocate" in position:
return STUDENT_ADVOCATE
elif "Senate" in position:
return SENATOR
else:
return 0
def __addJSONCandidate__(self, candidate, position):
self.candidates[position].append(Candidate(int(candidate["number"]), candidate["name"], position, candidate["party"]))
# Used for debugging
def displayCandidates(self):
for position in self.candidates:
print(position)
print("-------------------------------------------------")
candidates = self.candidates[position]
for candidate in candidates:
print(candidate)
print("")
# Used for debugging
def finishRace(self):
if self.race:
status = CONTINUE
while status != FINISHED:
status = self.stepFunction()
if status == STOP:
self.race.candidates.sort(key=lambda x: -1 * x.score)
for cand in self.race.candidates:
print(cand)
raw_input()
pass
def iterateRace(self):
if self.race:
return self.stepFunction()
def resetRace(self):
if self.race:
for candidate in self.race.candidates:
candidate.score = 0
candidate.ballots = []
candidate.state = RUNNING
candidate.quotaPlace = 0
self.race = None
def startRace(self, position):
candidates = self.candidates[position]
if not self.ballots: raise ElectionError("No ballots have been entered.")
ballot_copy = copy.deepcopy(self.ballots)
print("start race", self.remove)
self.race = Race(self, position, candidates, ballot_copy, toRemove=self.remove, tabulator=self)
if (position != SENATOR):
self.stepFunction = self.race.runStepExecutives
else:
self.stepFunction = self.race.runStepSenator
return self.race.quota
def startResignationRace(self, originalRace, removeAfter, resignee_ballots, numToCand, nameToCand):
print "Starting second race"
candidates = self.candidates[SENATOR]
original_winners = originalRace.winner
original_losers = []
for candidate in candidates:
if candidate not in original_winners:
original_losers.append(candidate)
print "ORIGINAL LOSERS: " + repr([loser.name for loser in original_losers])
print "ORIGINAL WINNERS: " + repr([winner.name for winner in original_winners])
self.resetRace()
if not self.ballots: raise ElectionError("No ballots have been entered.")
print "Copying ballot for the resignation election"
ballot_copy = copy.deepcopy(resignee_ballots)
for ballot in ballot_copy:
ballot.reset_rankings()
self.race = Race(self, SENATOR, original_losers, ballot_copy, pastWinners=original_winners, toRemove=removeAfter, tabulator=self, numToC=numToCand, nameToC=nameToCand)
self.stepFunction = self.race.runStepSenator
value = -1
while value != FINISHED:
value = self.race.runStepSenator()
print "Completed resignation election"
return self.race.quota
def startResignationRaceExec(self, originalRace, position, removeAfter, resignee_ballots, numToCand, nameToCand):
print "Starting second race"
candidates = self.candidates[position]
original_winners = originalRace.winner
original_losers = []
for candidate in candidates:
if candidate not in original_winners:
original_losers.append(candidate)
print "ORIGINAL LOSERS: " + repr([loser.name for loser in original_losers])
print "ORIGINAL WINNERS: " + repr([winner.name for winner in original_winners])
self.resetRace()
if not self.ballots: raise ElectionError("No ballots have been entered.")
print "Copying ballot for the resignation election"
ballot_copy = copy.deepcopy(resignee_ballots)
for ballot in ballot_copy:
ballot.reset_rankings()
self.race = Race(self, position, original_losers, ballot_copy, tabulator=self, numToC=numToCand, nameToC=nameToCand)
self.stepFunction = self.race.runStepExecutives
value = -1
while value != FINISHED:
value = self.race.runStepExecutives()
print "Completed resignation election"
return self.race.quota