-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
executable file
·287 lines (224 loc) · 8.86 KB
/
run.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
#!/usr/bin/env python3
"""Provides an interface to input ballots and run elections."""
import argparse
import csv
import re
import urllib.request
from election import Ballot, Candidate, Election, NoConfidence
__author__ = "Devin Gund"
__copyright__ = "Copyright 2017, Carnegie Mellon University Undergraduate Student Senate"
__credits__ = ["Sushain Cherivirala"]
__license__ = "GPLv3"
__status__ = "Production"
# String representing the input for No Confidence
NC_STRING = 'No Confidence'
# String representing the abbreviated input for No Confidence
NC_STRING_SHORT = 'NC'
def input_string_is_no_confidence(candidate_input):
"""Checks if an input string represents No Confidence.
Args:
candidate_input: String representing user input for a Candidate. The
expected format is 'uid' or optionally 'uid (name)'.
Returns:
Boolean indicating if the input string represents No Confidence or not.
"""
return (candidate_input.lower() == NC_STRING.lower() or
candidate_input.lower() == NC_STRING_SHORT.lower())
def candidate_from_input(candidate_input):
"""Returns a Candidate representing the input string.
Args:
candidate_input: String representing user input for a Candidate. The
expected format is 'uid' or optionally 'uid (name)'.
Returns:
Candidate representing the input uid (and optionally name).
"""
if input_string_is_no_confidence(candidate_input):
return NoConfidence()
else:
expr = '(.*?)\s*\((.*?)\)'
regex = re.compile(expr)
result = regex.match(candidate_input)
if result is not None:
uid = result.group(1)
name = result.group(2)
else:
uid = candidate_input
name = None
return Candidate(uid, name=name)
def ballot_from_candidate_inputs(candidate_inputs):
"""Returns a Ballot of Candidates representing the input strings.
Args:
candidate_inputs: List of Strings representing user input for a
Candidate. The expected format is 'uid' or optionally 'uid (name)'.
Returns:
Ballot representing the input Candidates.
"""
candidates = list()
for candidate_input in candidate_inputs:
if not candidate_input:
break
else:
candidate = candidate_from_input(candidate_input)
candidates.append(candidate)
ballot = Ballot(candidates=candidates)
return ballot
def ballots_from_input():
"""Return Ballots from command-line user input.
Returns:
List of Candidates representing user input.
"""
ballots = list()
ballot_number = 0
def print_ballot_instructions():
""""Prints ballot input instructions."""
print('Instructions:')
print('Enter candidates on the ballot in a comma-separated list of',
'unique identifiers, ordered from most to least preferred.')
print('The expected input format for a candidate is \'uid\' or',
'optionally \'uid (name)\'.')
print('{} may also be abbreviated as {}.'.format(NC_STRING,
NC_STRING_SHORT))
print('Press enter with an empty ballot to end input.',
'Type \'undo\' to remove the last ballot.',
'Type \'help\' to view these instructions again.')
print_ballot_instructions()
while True:
ballot_input = input('\nBallot {}: '.format(ballot_number)).strip()
if ballot_input == '':
return ballots
elif ballot_input.lower() == 'help':
print_ballot_instructions()
elif ballot_input.lower() == 'undo':
if ballot_number > 0:
ballots.pop()
ballot_number -= 1
else:
candidate_inputs = [candidate_input.strip()
for candidate_input in ballot_input.split(',')]
ballot = ballot_from_candidate_inputs(candidate_inputs)
ballots.append(ballot)
ballot_number += 1
def ballots_from_csv(filename):
"""Return Ballots from CSV user input.
Args:
filename: The filepath of the CSV file containing the user input.
Returns:
List of Ballots representing user input.
"""
ballots = list()
with open(filename) as f:
sniffer = csv.Sniffer()
dialect = csv.Sniffer().sniff(f.read(1024))
f.seek(0)
has_header = sniffer.has_header(f.read(1024))
f.seek(0)
reader = csv.reader(f, dialect)
for row in reader:
if reader.line_num > 0 or not has_header:
ballot = ballot_from_candidate_inputs(row)
ballots.append(ballot)
f.close()
return ballots
def ballots_from_txt(filename):
"""Return Ballots from TXT user input.
Args:
filename: The filepath of the TXT file containing the user input.
Returns:
List of Ballots representing user input.
"""
ballots = list()
with open(filename) as f:
for line in f:
if len(line.strip()) > 0:
candidate_inputs = [candidate_input.strip()
for candidate_input in line.split(',')]
ballot = ballot_from_candidate_inputs(candidate_inputs)
ballots.append(ballot)
f.close()
return ballots
def ballots_from_file(filename):
"""Return Ballots from file user input.
Args:
filename: The filepath of the CSV or TXT file containing the user input.
Returns:
List of Ballots representing user input.
"""
if filename.lower().endswith('.csv'):
return ballots_from_csv(filename)
elif filename.lower().endswith('.txt'):
return ballots_from_txt(filename)
else:
raise ValueError('Invalid filetype. Accepts .csv, .txt.')
def ballots_from_url(url):
"""Returns Ballots from URL pointing to CSV file.
Args:
url: The URL to a CSV file containing the user input.
Returns:
List of Ballots representing user input.
"""
filename, _ = urllib.request.urlretrieve(url)
return ballots_from_csv(filename)
def parse_args():
"""Parses command-line election arguments.
Returns:
argparse.Namespace containing election arguments.
"""
description = ('Configure and run an election. Ballots ranking candidates '
'may be imported from a CSV or TXT file, or manual input if '
'no file is specified. The expected input format for a '
'candidate is \'uid\' or optionally \'uid (name)\'.')
parser = argparse.ArgumentParser(description=description)
required_group = parser.add_argument_group('required arguments')
# Number of seats (required)
required_group.add_argument('-s', '--seats', help='Number of seats',
type=int, required=True)
# Alphanumeric string for breaking ties
parser.add_argument('-a', '--alphanumeric',
help='Alphanumeric string for breaking ties')
# File/URL containing ballots
parser.add_argument('-b', '--ballots', help='File/URL containing ballots')
# Disallow No Confidence from being eliminated
parser.add_argument('-c', '--disallow-nc-elimination',
help='No Confidence cannot be eliminated',
action='store_true')
# Name of Election
parser.add_argument('-n', '--name', help='Name of election', default='')
# Disallow random tiebreaks, ending the election instead
parser.add_argument('-r', '--disallow-random-tiebreak',
help='Halt election instead of using random tiebreak',
action='store_true')
# Verbose printing of election results
parser.add_argument('-v', '--verbose',
help='Verbose printing of election results',
action='store_true')
args = parser.parse_args()
return args
def process_args(args):
"""Processes command-line election arguments and runs election.
Args:
argparse.Namespace containing election arguments.
"""
if args.ballots is not None:
if args.ballots.startswith('http'):
ballots = ballots_from_url(args.ballots)
else:
ballots = ballots_from_file(args.ballots)
else:
ballots = ballots_from_input()
election = Election(
ballots,
args.seats,
can_eliminate_no_confidence=not(args.disallow_nc_elimination),
can_random_tiebreak=not(args.disallow_random_tiebreak),
name=args.name,
random_alphanumeric=args.alphanumeric
)
results = election.compute_results()
if args.verbose:
print(results.description())
else:
for candidate in results.candidates_elected:
print(candidate)
if __name__ == '__main__':
election_args = parse_args()
process_args(election_args)