My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday at 8am PST due to brief network maintenance.
                
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env python
# coding: utf-8

# voter.py - vote reader for 2008 general election
# Copyright (c) 2008 Michael Geary - http://mg.to/
# Free Beer and Free Speech License (MIT+GPL)
# http://freebeerfreespeech.org/
# http://www.opensource.org/licenses/mit-license.php
# http://www.opensource.org/licenses/gpl-2.0.php

import copy
import os
import os.path
import re
import sys
import time
import random
import simplejson as sj
import xml.dom.minidom

import states

candidates = {}
trends = {}

isTestData = False

#def str( text ):
# strings = {
# 'county': 'town',
# 'counties': 'towns'
# }
# return strings[text] or text

def formatNumber( number ):
return str(number)

def json( obj ):
if 0:
# Pretty print
json = sj.dumps( obj, indent=0 )
elif 1:
# Use compact format, but add some newlines in the hope of using less space for svn revisions
json = sj.dumps( obj, separators=( ',', ':' ) )
json = re.sub( '\],"', '],\n"', json )
json = re.sub( ':\[{', ':[\n{', json )
json = re.sub( '":{', '":\n{', json )
json = re.sub( '},{', '},\n{', json )
json = re.sub( '},"', '},\n"', json )
else:
# Compact JSON
json = sj.dumps( obj, separators=( ',', ':' ) )
return json

def getPrecincts( row ):
#print 'getPrecincts %s %s %s %s' %( row[0], row[1], row[2], row[3] )
return {
'reporting': int(row[3]),
'total': int(row[2])
}

def fixCountyName( state, name ):
name = re.sub( ' County$', '', name )
if name in state.get( 'fix', {} ):
name = state['fix'][name]
return name

def loadPresSummary():
result = {}
feed = datapath + 'pres_summary.xml'
print 'Processing %s' % feed
dom = xml.dom.minidom.parse( feed )
cands = dom.getElementsByTagName( 'Cand' )
for cand in cands:
name = cand.getAttribute( 'name' )
result[name] = {
'votes': cand.getAttribute( 'PopVote' ),
'electoral': cand.getAttribute( 'ElectWon' ),
'won': bool( cand.getAttribute( 'Winner' ) )
}
return result

def loadTrends( house ):
result = {}
feed = datapath + house + '.xml'
print 'Processing %s' % feed
dom = xml.dom.minidom.parse( feed )
parties = dom.getElementsByTagName( 'party' )
for party in parties:
name = party.getAttribute( 'title' )
result[name] = {}
trends = party.getElementsByTagName( 'trend' )
for trend in trends:
type = trend.getAttribute( 'name' )
result[name][type] = int( trend.getAttribute('value') )
return result

def loadElectoralVotes( usall ):
feed = datapath + 'pres_electoral.txt'
print 'Processing %s' % feed
f = open( feed, 'r' )
for line in f:
row = line.rstrip('\n').split(';')
abbr = row[2]
id = row[4]
electoral = int(row[5])
total = int(row[11])
state = usall
if abbr != 'US':
state = states.byAbbr[abbr]
votes = state['races']['President']['']['votes']
if id not in votes: votes[id] = { 'id': id, 'votes': 0 }
votes[id]['electoral'] = electoral
state['electoral'] = total
f.close()

def readVotes( report ):
feed = datapath + report
print 'Processing %s' % feed
f = open( feed, 'r' )
for line in f:
setVoteData( line.rstrip('\n').split(';') )
f.close()

def setVoteData( row ):
# why does this crash Python?
#isTestData = isTestData or row[0] == 't'
if row[0] == 't': isTestData = True
abbr = row[2]
entity = state = states.byAbbr[abbr]
race = row[10]
seat = re.sub( '^District ', '', row[11] )
if 'counties' not in state: state['counties'] = {}
counties = state['counties']
fips = row[4]
if len(fips) == 4: fips = '0' + fips # change 4-digit FIPS to 5, AP omits leading 0
if fips != '0':
countyname = fixCountyName( state, row[5] )
if countyname not in counties:
counties[countyname] = {
'fips': fips,
'name': countyname
}
entity = counties[countyname]
if 'races' not in entity: entity['races'] = {}
races = entity['races']
if race not in races: races[race] = {}
seats = races[race]
if seat not in seats: seats[seat] = { 'votes': {} }
if 'precincts' not in entity:
entity['precincts'] = {
'reporting': int(row[17]),
'total': int(row[18])
}

for col in xrange( 19, len(row), 12 ):
can = row[col:col+12]
if len(can) < 12: continue
id = can[11] or abbr + can[0]
if id not in candidates:
party = can[2]
if party == 'LTP': party = 'GOP' # AP has Ron Paul listed in a nonexistent party!
first = can[3]
last = can[5]
jr = ''
if can[7] != '0': jr = ', ' + can[6]
name = first + ' ' + last + jr
#candidates[id] = {
# 'party': party,
# 'last': last,
# 'full': name
#}
candidates[id] = '|'.join([ party, last, name ]);
print 'Added %s candidate %s' %( party, name )
candidate = candidates[id]
votes = int(can[9])
seats[seat]['votes'][id] = { 'id': id, 'votes': votes }
if can[10]: seats[seat]['final'] = id

def percentage( n ):
pct = int( round( 100.0 * float(n) ) )
if pct == 100 and n < 1: pct = 99
return pct

def sortVotes( entity ):
for race in entity['races'].itervalues():
for seat in race.itervalues():
if not seat.get('votes'): seat['votes'] = {}
tally = seat['votes'].values()
tally.sort( lambda a, b: b['votes'] - a['votes'] )
seat['votes'] = tally

def cleanNum( n ):
return int( re.sub( '[^0-9]', '', n ) or 0 )

def makeJson( type ):
ustotal = 0
usvotes = {}
usprecincts = { 'total': 0, 'reporting': 0 }
usall = {
'races': {
'President': {
'': { 'votes': usvotes, 'precincts': usprecincts }
}
}
}
statevotes = {}
#leaders = {}
#def addLeader( party ):
# if len(party['votes']):
# leaders[ party['votes'][0]['name'] ] = True
loadElectoralVotes( usall )
for st in states.array:
state = copy.deepcopy( st )
statetotal = 0
sortVotes( state )
statevotes[ state['name'] ] = state
#print 'Loading %s' %( state['name'] )
cands = {}
for key, race in state['races'].iteritems():
for seat in race.itervalues():
for vote in seat['votes']:
id = vote['id']
if id not in cands: cands[id] = candidates[id]
if key == 'President':
count = vote['votes']
if id not in usvotes:
usvotes[id] = { 'id': id, 'votes': 0 }
usvotes[id]['votes'] += count
ustotal += count
statetotal += count
countyvotes = {}
counties = state.get( 'counties', {} )
for countyname, county in counties.iteritems():
sortVotes( county )
#addLeader( county )
countytotal = 0
for vote in county['races']['President']['']['votes']:
countytotal += vote['votes']
county['races']['President']['']['total'] = countytotal
countyvotes[countyname] = county
#setPins( countyvotes )
del state['counties']
if type == 'all':
writeFile(
'%s%s-%s.json' %( jsonpath, state['abbr'].lower(), type ),
json({
'state': state['abbr'],
'candidates': cands,
'total': statetotal,
'totals': state,
'locals': countyvotes
}) )
sortVotes( usall )
#setPins( statevotes )
j = {
'state': 'US',
'candidates': candidates,
'total': ustotal,
'totals': usall,
'locals': statevotes
}
if type == 'all':
j['trends'] = trends
writeFile(
'%s%s-%s.json' %( jsonpath, 'us', type ),
json( j )
)
#print '%s of %s precincts reporting' %( state['precincts']['reporting'], state['precincts']['total'] )
#print '%s leaders:' % party
#for leader in leaders.iterkeys():
# print leader

def readFile( filename ):
f = open( filename, 'rb' )
data = f.read()
f.close()
return data

def writeFile( filename, data ):
print 'Writing %s' % filename
f = open( filename, 'wb' )
f.write( data )
f.close()

def update():
#fetchData( feed )
trends['President'] = loadPresSummary()
trends['U.S. House'] = loadTrends( 'h' )
trends['U.S. Senate'] = loadTrends( 's' )
readVotes( 'pres_county.txt' )
print 'Creating presidential votes JSON...'
makeJson( 'pres' )
readVotes( 'US.txt' )
print 'Creating top of ticket votes JSON...'
makeJson( 'all' )
print 'Checking in votes JSON...'
os.system( 'svn ci -m "Vote update" %s' % jsonpath )
print 'Done!'

def main():
#while 1:
update()
#print 'Waiting 10 minutes...'
#time.sleep( 600 )

if __name__ == "__main__":
datapath = '../general-election-private/ap/2008/'
jsonpath = '../general-election-data/json/votes/2008/'
main()
Show details Hide details

Change log

r207 by m...@mg.to on Nov 06, 2008   Diff
Update path
Go to: 
Project members, sign in to write a code review

Older revisions

r203 by m...@mg.to on Nov 06, 2008   Diff
Use Google Code instead of S3
r198 by m...@mg.to on Nov 05, 2008   Diff
Fix Ron Paul party error, fix AP early
calls
r176 by m...@mg.to on Nov 04, 2008   Diff
Ready to go live
All revisions of this file

File info

Size: 8141 bytes, 310 lines
Hosted by Google Code