-
Notifications
You must be signed in to change notification settings - Fork 1
/
vote.py
executable file
·50 lines (42 loc) · 1.51 KB
/
vote.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
#!/usr/bin/env python
# votes values into groups
import sys
import argparse
# ----- command line parsing -----
parser = argparse.ArgumentParser(
prog="vote",
description="Votes values into groups.")
parser.add_argument("-c", "--data_column", type=int, default=1,
help="Column to group with.")
parser.add_argument("-v", "--vote_column", type=int, default=2,
help="Column to vote with.")
parser.add_argument("-t", "--threshold", type=float, default=0.5,
help="Threshold for making a call.")
parser.add_argument("-m", "--minimum_votes", type=int, default=1,
help="Minimum number of votes required to call.")
parser.add_argument("-d", "--delimiter", type=str, default=None,
help="Column delimiter.")
args = parser.parse_args()
# ----- end command line parsing -----
votes = {}
for line in sys.stdin:
split = line.split(args.delimiter)
data = split[args.data_column - 1]
vote = split[args.vote_column - 1]
if data in votes:
if vote in votes[data]:
votes[data][vote] += 1
else:
votes[data][vote] = 1
else:
votes[data] = {vote : 1}
for data in votes:
maxn = 0
sumn = 0
for vote in votes[data]:
if votes[data][vote] > maxn:
maxv = vote
maxn = votes[data][vote]
sumn += votes[data][vote]
if sumn > args.minimum_votes and maxn > sumn * args.threshold:
sys.stdout.write("{:s}\t{:s}\n".format(data, maxv))