-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoutput.py
95 lines (79 loc) · 2.59 KB
/
output.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
import sys, datetime, json
class LogEntry:
def __init__(self, output):
self.lines = []
self.output_to = output
def __enter__(self):
return self
def log(self, line, echo=False, end=None):
self.lines.append(line)
if echo:
print(line, file=sys.stderr, end=end)
def __exit__(self, type, value, tb):
self.output_to.add_note('\n'.join(self.lines))
class RoundLog(LogEntry):
def __init__(self, number):
self.number = number
self.note = []
self.elected = []
self.excluded = None
self.distribution = None
self.count = None
def json(self):
return {
'number': self.number,
'note': self.note,
'elected': self.elected,
'excluded': self.excluded,
'distribution': self.distribution,
'count' : self.count
}
def set_count(self, count):
self.count = count
def set_distribution(self, distribution):
self.distribution = distribution
def add_elected(self, candidate_id, pos, transfer):
if transfer is not None:
t = { 'excess' : transfer[0], 'value' : transfer[1] }
else:
t = None
self.elected.append({
'id' : candidate_id,
'pos' : pos,
'transfer' : t
})
def set_excluded(self, candidate_id, next_candidates, margin, transfer_values):
self.excluded = {
'id' : candidate_id,
'margin' : margin,
'next_candidates' : next_candidates,
'transfers' : transfer_values
}
def add_note(self, message):
self.note.append(message)
class JsonOutput:
def __init__(self, fname):
self.rounds = []
self.fname = fname
self.summary = None
def add_round(self, round):
self.rounds.append(round)
def set_summary(self, summary_info):
self.summary = summary_info
def render(self, counter, template_vars):
params = {
'total_papers': counter.total_papers,
'quota': counter.quota,
'vacancies': counter.vacancies,
'dt' : datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
}
params.update(template_vars)
obj = {
'candidates' : counter.candidate_json(),
'parties' : counter.party_json(),
'parameters' : params,
'rounds' : [t.json() for t in self.rounds],
'summary' : self.summary,
}
with open(self.fname, 'w') as fd:
json.dump(obj, fd)