-
Notifications
You must be signed in to change notification settings - Fork 3
/
process-tally.py
41 lines (37 loc) · 1.17 KB
/
process-tally.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
import sys
import re
import pandas as pd
import math
vote = pd.DataFrame (columns = ['voter', 'F', 'B', 'A', 'D', 'H', 'E', 'G', 'fd'],
index = None)
for line in sys.stdin.readlines ():
m = re.match ('^V: ([-1-8]+)\s+(\w+).*', line)
if m:
## Get voter and their choices
choices = list (m.group (1))
voter = m.group (2)
## Get minimum and maximum values
minv = 8
maxv = 1
for c in choices:
try:
ival = int (c)
if ival < minv:
minv = ival
if ival > maxv:
maxv = ival
except ValueError:
pass
## Compute the rank for the unlisted options
unlist_rank = min ([8, maxv + 1])
for i in range (len (choices)):
if choices [i] == '-':
choices [i] = unlist_rank
else:
choices [i] = int (choices [i])
if math.isnan (vote.index.max ()):
idx = 0
else:
idx = vote.index.max () + 1
vote.loc [idx] = [voter] + choices
vote.to_csv ('vote-2019-002.csv', index = False)