Created
May 9, 2012 17:34
-
-
Save abock/2647020 to your computer and use it in GitHub Desktop.
NC Amendment One Outcome Visualization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Written by Aaron Bockover <[email protected]> | |
# Released into the Public Domain | |
import json | |
import urllib.request | |
uri = 'http://results.enr.clarityelections.com/NC/36596/80750/json/details.json' | |
response = urllib.request.urlopen(uri) | |
data = json.loads(response.read().decode('utf8')) | |
counties = [] | |
max_ignorance = 1 | |
yay_total = 0 | |
nay_total = 0 | |
# this is a really retardedly organized json structure | |
for contest in data['Contests']: | |
if contest['K'] == '425000010': | |
i = 0 | |
for yay, nay in contest['V']: | |
yay_total += yay | |
nay_total += nay | |
ignorance = yay / nay | |
max_ignorance = max(max_ignorance, ignorance) | |
counties.append({ | |
'name': contest['P'][i], | |
'yay': yay, | |
'nay': nay, | |
'ignorance': ignorance | |
}) | |
i += 1 | |
print('''<!DOCTYPE html> | |
<html> | |
<head> | |
<title>NC Amendment One Outcome</title> | |
<style type="text/css"> | |
body {{ | |
font-family: sans-serif; | |
background: #fff; | |
color: #222; | |
}} | |
tr.for {{ | |
color: #fff; | |
}} | |
tr.against {{ | |
background: #0f0; | |
}} | |
td, th {{ | |
padding: 5px 10px 5px 5px; | |
}} | |
th {{ | |
text-align: left; | |
}} | |
</style> | |
</head> | |
<body> | |
<h1>NC Amendment One Outcome</h1> | |
<p>Results from the May 8, 2012 vote on North Carolina's Constitutional | |
Amendment One. Sorted by least to most ignorant counties. | |
<a href="{0}" />Source</a>. <a href="https://gist.github.com/2647020" />Generated By</a>.</p> | |
<table> | |
<tr> | |
<th>County</th> | |
<th>For</th> | |
<th>Against</th> | |
<th>Ignorance Ratio</th> | |
</tr>'''.format(uri)) | |
for county in sorted(counties, key = lambda c: c['ignorance']): | |
ignorance = county['ignorance'] | |
relative_ignorance = ignorance / max_ignorance | |
if ignorance < 1: | |
css = 'class="against"' | |
else: | |
css = 'class="for" style="background: hsl(0, {0}%, 50%)"'.format( | |
int(relative_ignorance * 100)) | |
print(('<tr {0}>' + | |
'<th>{1[name]}</th>' + | |
'<td>{1[yay]}</td>' + | |
'<td>{1[nay]}</td>' + | |
'<td>{1[ignorance]:#0.4g}</td>').format(css, county)) | |
print(''' | |
<tr> | |
<th> </th> | |
<th>Total For</th> | |
<th>Total Against</th> | |
<th>Percent For</th> | |
</tr> | |
<tr> | |
<td> </td> | |
<td>{0}</td> | |
<td>{1}</td> | |
<td style="font-size: 300%">{2}%</td> | |
</tr> | |
</table> | |
<p><img src="http://static.someecards.com/someecards/usercards/1336534238647_1401578.png" alt="North Carolina: where you can marry your cousin. Just not your gay cousin." /></p> | |
</body> | |
</html>'''.format(yay_total, nay_total, int((nay_total / yay_total) * 100))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment