-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_files.py
193 lines (136 loc) · 5.31 KB
/
build_files.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import csv
import json
import glob
from datetime import date
import os
def get_elex_lookup():
with open('elex-lookup.json', 'r') as infile:
return json.load(infile)
def get_fips_lookup():
with open('us-county-fips.csv', 'r') as infile:
reader = csv.DictReader(infile)
sd = [x for x in reader if x['state_fips'] == '46']
lookup = {}
for county in sd:
fips = f'{county.get("state_fips")}{county.get("county_fips")}'
name = county.get('county_name').replace(' County', '')
lookup[name] = fips
lookup['Oglala Lakota'] = '46102'
lookup['Washabaugh'] = '46131'
return lookup
def build_files():
lookup_fips = get_fips_lookup()
lookup_elex = get_elex_lookup()
data_filepath = 'sd-voter-registration-data.csv'
data_filepath_simplified = 'sd-voter-registration-data-simplified.csv'
files = glob.glob('data/*.csv')
data_out = []
data_out_simplified = {}
parties = set()
for file in files:
elex = lookup_elex.get(file.split('/')[-1].split('.')[0])
with open(file, 'r') as infile:
reader = csv.DictReader(infile)
for row in reader:
snapshot_date = row.get('date')
county = row.get('county')
fips = lookup_fips[county]
if not data_out_simplified.get(snapshot_date):
data_out_simplified[snapshot_date] = {}
if not data_out_simplified.get(snapshot_date).get(fips):
data_out_simplified[snapshot_date][fips] = {
'county': county,
'election': elex,
'republican': 0,
'democratic': 0,
'other': 0
}
for header in list(row.keys()):
if header not in ['date', 'county']:
votes = row.get(header) or 0
votes = int(votes)
parties.add(header)
data_out.append({
'date': snapshot_date,
'county': county,
'county_fips': fips,
'party': header,
'voters': votes,
'election': elex
})
# simplified file doesn't need inactive records
if header == 'inactive':
continue
# collapse non-R/D parties into "other"
if header not in ['republican', 'democratic']:
header = 'other'
data_out_simplified[snapshot_date][fips][header] += votes
row['county_fips'] = fips
data_sorted = sorted(
data_out,
key=lambda x: (
x['date'],
x['county_fips'],
x['party']
)
)
with open(data_filepath, 'w') as outfile:
writer = csv.DictWriter(
outfile,
fieldnames=list(data_sorted[0].keys())
)
writer.writeheader()
writer.writerows(data_sorted)
print(f'Party list: {", ".join(sorted(parties))}\n{"-"*20}')
print(f'Wrote {data_filepath}')
data_out_simplified_records = []
for snapshot_date in data_out_simplified:
fips_codes = data_out_simplified[snapshot_date]
for fips in fips_codes:
rec = fips_codes[fips]
county = rec.get('county')
elex = rec.get('election')
# skip Washabaugh records
if county == 'Washabaugh':
continue
# update shannon/oglala
if county == 'Shannon' and int(fips) == 46113:
county = 'Oglala Lakota'
fips = '46102'
for header in list(rec.keys()):
if header not in ['county', 'election']:
data_out_simplified_records.append({
'date': snapshot_date,
'county': county,
'county_fips': fips,
'party': header,
'voters': int(rec.get(header)),
'election': elex
})
data_out_simplified_sorted = sorted(
data_out_simplified_records,
key=lambda x: (
x['date'],
x['county_fips'],
x['party']
)
)
with open(data_filepath_simplified, 'w') as outfile:
writer = csv.DictWriter(
outfile,
fieldnames=list(data_out_simplified_sorted[0].keys())
)
writer.writeheader()
writer.writerows(data_out_simplified_sorted)
print(f'Wrote {data_filepath_simplified}')
# update readme
with open('README-template.md', 'r') as infile:
content = infile.read()
updated = date.today().strftime('%B %-d, %Y')
snapshot_count = len([x for x in os.listdir('data') if x.endswith('.csv')])
content = content.replace('{% updated %}', updated).replace('{% snapshot_count %}', str(snapshot_count))
with open('README.md', 'w') as outfile:
outfile.write(content)
print('Wrote README.md')
if __name__ == '__main__':
build_files()