-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_votes_new.py
117 lines (100 loc) · 4.27 KB
/
parse_votes_new.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
import json, argparse
from io import *
# Syntax
# python3 parse_votes_new.py -f votes.txt
# python3 parse_votes_new.py -f votes.txt -d
# -d just adds the score to the end of the vote, for debugging purposes.
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--file', '-f', help='File to read votes from', required=True)
parser.add_argument('--debug', '-d', help='Debug mode', action='store_true')
parser.add_argument('--nocap', '-n', help='Don\'t cap to 3 results', action='store_true')
args = parser.parse_args()
global debug
debug = args.debug
global nocap
nocap = args.nocap
# If the file doesnt exist, exit
rawvotes = dict()
try:
with open(args.file, 'r') as votes_file:
rawvotes = parse_votes(votes_file)
# for k , v in sorted(rawvotes.items()):
for division, voting in sorted(rawvotes.items()):
produce_division_report(division, voting)
except FileNotFoundError:
print("File not found")
exit(1)
def parse_votes(votes_file: TextIOWrapper):
# Read the file by line
lines = [vote_line.strip().lower()
for vote_line in votes_file.readlines()]
# if vote_line not in ['', '\n']]
voting_divisions = {
}
i = 0
current_division = ''
for line in lines:
i+=1
if line == '': continue
if line == '\n': continue
# Process each line of the votes dump.
separator = line.split(':')
try:
question = separator[0].strip()
response = separator[1].strip()
except IndexError:
print(f'[[ERROR]]: Malformed line {i} : {line}')
print(f'[[FATAL]]: Correct this error before continuing.')
exit(1)
if question == 'your team division' or question == 'division (premier/high/intermediate/main/open)':
current_division = response
elif question == 'your team name' or question == 'team name':
# End of previous teams votes. In the event of the first team, nothing preceeded this so nothing happened.
pass
else:
# The response is a comma separated list of votes, in theory.
# Assuming that the response has been pre-santized, we can split it by commas.
if len(response.split(',')) > 1:
response = response.split(',')
else:
response = [response]
# warn if more than 3 votes are given.
if len(response) > 3:
print(f'[[WARNING]]: Too many votes given for line {i} : {line}')
# Do we know of this division?
if current_division not in voting_divisions:
voting_divisions[current_division] = {}
# What category is this vote for?
if question not in voting_divisions[current_division]:
voting_divisions[current_division][question] = []
# Add the votes to the list of votes for this category.
voting_divisions[current_division][question] += [response]
return voting_divisions
def produce_division_report(division, voting):
print(division)
print('=' * len(division))
# loop through categories
for vote_category, ballots in sorted(voting.items()):
# Voting works by scoring 3 points to the first vote, 2 to the second and 1 to the third.
# These are then summed up to give a total score.
scores = {}
for ballot in ballots:
for i, vote in enumerate(ballot):
vote = vote.strip()
if vote not in scores:
scores[vote] = 0
scores[vote] += 3 - min(i,3) # 3, 2, 1, and then don't count anything after
# sort the scores and get the top 3
winners = sorted(scores, key=scores.get, reverse=True)
if not nocap:
winners = winners[:3]
printstring = vote_category + ': '
for winner in winners:
if debug:
printstring += f'{winner} ({scores[winner]}), '
else:
printstring += f'{winner}, '
print(printstring[:-2])
if __name__ == '__main__':
main()