-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_csvs.py
111 lines (97 loc) · 2.76 KB
/
output_csvs.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
from csv import DictWriter
from sqlite_utils import Database
def generate_contest_results_csv(db):
rows = db.query(
"""
SELECT
contests.id as contest_id,
contests.name as contest,
contests.[group] as contest_group,
contests.type as contest_type,
contests.non_partisan,
contests.voter_nominated,
contests.vote_for,
candidates.id as candidate_id,
candidates.name,
candidates.party,
candidates.total_votes
FROM
candidates
JOIN contests ON candidates.contest_id = contests.id
ORDER BY
contests.id, candidates.total_votes DESC
"""
)
with open("tmp/contest_results.csv", "w") as outfile:
writer = DictWriter(
outfile,
fieldnames=[
"contest_id",
"contest",
"contest_group",
"contest_type",
"non_partisan",
"voter_nominated",
"vote_for",
"candidate_id",
"name",
"party",
"total_votes",
],
)
writer.writeheader()
writer.writerows(rows)
def generate_precinct_results_csv(db):
rows = db.query(
"""
SELECT
results.precinct_id,
contests.id as contest_id,
contests.name as contest,
contests.[group] as contest_group,
contests.type as contest_type,
contests.non_partisan,
contests.voter_nominated,
contests.vote_for,
candidates.id as candidate_id,
candidates.name,
candidates.party,
results.polling_place,
results.vote_by_mail,
results.total
FROM
results
JOIN candidates ON candidates.id = results.candidate_id
JOIN contests ON contests.id = results.contest_id
ORDER BY
contests.id, results.precinct_id, candidates.id
"""
)
with open("tmp/precinct_results.csv", "w") as outfile:
writer = DictWriter(
outfile,
fieldnames=[
"precinct_id",
"contest_id",
"contest",
"contest_group",
"contest_type",
"non_partisan",
"voter_nominated",
"vote_for",
"candidate_id",
"name",
"party",
"polling_place",
"vote_by_mail",
"total",
],
)
writer.writeheader()
writer.writerows(rows)
def run():
db = Database("tmp/results.db")
generate_contest_results_csv(db)
generate_precinct_results_csv(db)
if __name__ == "__main__":
run()