This repository has been archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpdfprep.py
141 lines (110 loc) · 3.78 KB
/
pdfprep.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
#!/usr/bin/env python
import json
import os
import sys
try:
import cPickle as pickle
except ImportError:
import pickle
ballots = {
'act': [],
'nsw': [],
'nt': [],
'qld': [],
'sa': [],
'tas': [],
'vic': [],
'wa': [],
}
people = {}
parties = {}
divisions = {}
def sorted_candidates(candidates):
return [c[:-1] for c in sorted(candidates, key=lambda x: x[3])]
for filename in os.listdir('data/people'):
if not filename.endswith('.json'):
continue
person_id = filename[:-5]
people[person_id] = json.loads(open('data/people/' + filename).read())
for filename in os.listdir('data/parties'):
if not filename.endswith('.json'):
continue
party_id = filename[:-5]
parties[party_id] = json.loads(open('data/parties/' + filename).read())
for filename in os.listdir('data/division'):
if not filename.endswith('.json'):
continue
division_id = filename[:-5]
divisions[division_id] = json.loads(open('data/division/' + filename).read())
def group_sort(a, b):
a_group = a.split('-', 1)[1][:-5]
b_group = b.split('-', 1)[1][:-5]
if len(a_group) != len(b_group):
return cmp(len(a_group), len(b_group))
return cmp(a_group, b_group)
for state in ('act', 'nsw', 'nt', 'qld', 'sa', 'tas', 'vic', 'wa'):
ungrouped = {
'label': 'UG',
'name': 'Ungrouped',
'candidates': [],
}
groups = [f for f in os.listdir('data/groups') if f.startswith(state) and f.endswith('.json')]
groups.sort(cmp=group_sort)
for filename in groups:
print "Processing:", filename
group = json.loads(open('data/groups/' + filename).read())
group_label = filename[:-5].split('-', 1)[1]
ballot = ballots[state]
if group_label.startswith('UG'):
ballot_group = ungrouped
else:
ballot_group = {
'label': group_label,
'name': group['name'],
}
ballot.append(ballot_group)
candidates = []
for candidate in group['candidates']:
person = people[candidate]
if person.get('party', None) is not None:
c = (person['last_name'], person['first_name'],
parties[person['party']]['name'], person['ballot_position'])
else:
c = (person['last_name'], person['first_name'], None,
person['ballot_position'])
candidates.append(c)
if group_label.startswith('UG'):
ungrouped['candidates'].append(candidates[0][:-1])
else:
ballot_group['candidates'] = sorted_candidates(candidates)
if ungrouped['candidates']:
ballots[state].append(ungrouped)
division_ballots = {}
for person_id, person in people.items():
if 'candidate' not in person:
continue
if not person['candidate'].startswith('division/'):
continue
division = person['candidate'].split('/')[1]
if division not in division_ballots:
division_ballots[division] = []
if person.get('party', None) is not None:
c = (person['last_name'], person['first_name'],
parties[person['party']]['name'], person['ballot_position'])
else:
c = (person['last_name'], person['first_name'], None,
person['ballot_position'])
division_ballots[division].append(c)
def group_sort(a, b):
a = a['label']
b = b['label']
if len(a) != len(b):
return cmp(len(a), len(b))
else:
return cmp(a, b)
for state in ballots:
ballots[state].sort(group_sort)
for division, candidates in division_ballots.items():
ballots[division] = (divisions[division]['name'],
sorted_candidates(candidates))
pickle.dump(ballots, open('ballots.pck', 'w'))