Skip to content

Instantly share code, notes, and snippets.

@JonnoFTW
Last active July 5, 2016 09:58
Show Gist options
  • Save JonnoFTW/18ff2daf2a9b270c2d68f86b160f8748 to your computer and use it in GitHub Desktop.
Save JonnoFTW/18ff2daf2a9b270c2d68f86b160f8748 to your computer and use it in GitHub Desktop.
import requests
import shutil
divids = ['101', '102', '103', '104', '105', '106', '107', '108', '109', '111', '112', '113', '114', '115', '117',
'118', '119', '120', '121', '122', '124', '125', '126', '127', '128', '130', '131', '132', '133', '134',
'135', '136', '137', '138', '139', '140', '144', '145', '146', '148', '149', '150', '151', '152', '153',
'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', '182', '183', '185', '186',
'187', '188', '189', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '200', '201',
'203', '204', '205', '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', '242', '243', '244', '245', '246', '247', '248', '249',
'250', '251', '252', '302', '304', '305', '306', '307', '309', '310', '311', '312', '315', '316', '317']
outfolder = "C:/temp/shapes/votes/"
for i in divids:
csv = requests.get('http://vtr.aec.gov.au/Downloads/SenateDivisionFirstPrefsByPollingPlaceDownload-20499-{}.csv'.format(i), stream=True)
with open(outfolder+i+'.csv', 'wb') as out_file:
shutil.copyfileobj(csv.raw, out_file)
del csv
import csv
from collections import defaultdict, Counter
import pprint
from glob import glob
pp = pprint.PrettyPrinter(indent=4)
def div(x, y):
if y == 0:
return 0
else:
return x / y
def get_pct_of(d, party, divisions):
return div(divisions[d][party], divisions[d]['total'])
party = "Pauline Hanson's One Nation"
def make_divisions(col, subdiv, fnames, partycol):
divisions = defaultdict(Counter)
for fname in fnames:
with open(fname, 'r') as infile:
infile.readline()
csvReader = csv.DictReader(infile)
for row in csvReader:
division = row[subdiv]
divisions[division]['total'] += float(row[col])
divisions[division][row[partycol]] += float(row[col])
return divisions
def by_electorate():
divisions = make_divisions('TotalVotes', 'DivisionNm',
['C:/Users/Jonathan/Downloads/SenateFirstPrefsByDivisionByVoteTypeDownload-20499.csv'],
'PartyName')
print("Division,Percent")
for i in sorted(divisions, key=lambda x: get_pct_of(x, party, divisions)):
print("{0},{1:.2f}%".format(i, get_pct_of(i, party, divisions) * 100))
def by_polling_place():
divisions = make_divisions('OrdinaryVotes',
'PollingPlaceNm',
glob('C:/temp/shapes/votes/*.csv'),
'PartyNm')
print("Booth,Percent")
for i in sorted(divisions, key=lambda x: get_pct_of(x, party, divisions)):
print("{0},{1:.2f}%".format(i, get_pct_of(i, party, divisions) * 100))
if __name__ == "__main__":
by_polling_place()
# by_electorate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment