Skip to content

Instantly share code, notes, and snippets.

@CaptainZidgel
Last active November 4, 2020 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CaptainZidgel/4441a69aae4d4ef0476561e5d1a49724 to your computer and use it in GitHub Desktop.
Save CaptainZidgel/4441a69aae4d4ef0476561e5d1a49724 to your computer and use it in GitHub Desktop.
Party Votes by State 1976-2016 as a dict:
#There have been 538 electors since 1964
#I am operating on the critical - but possibly wrong - assumption that the number of electors per state is static
electors = {
"California": 55,
"Nevada": 6,
"Oregon": 7,
"Washington": 12,
"Idaho": 4,
"Utah": 6,
"Arizona": 11,
"Montana": 3,
"Wyoming": 3,
"Colorado": 9,
"New Mexico": 5,
"North Dakota": 3,
"South Dakota": 3,
"Nebraska": 5,
"Kansas": 6,
"Oklahoma": 7,
"Texas": 38,
"Minnesota": 10,
"Iowa": 6,
"Missouri": 10,
"Arkansas": 6,
"Louisiana": 8,
"Wisconsin": 10,
"Illinois": 20,
"Tennessee": 11,
"Mississippi": 6,
"Michigan": 16,
"Indiana": 11,
"Kentucky": 8,
"Alabama": 9,
"Ohio": 18,
"West Virginia": 5,
"Virginia": 13,
"North Carolina": 15,
"South Carolina": 9,
"Georgia": 16,
"Florida": 29,
"Maine": 4,
"New York": 29,
"Vermont": 3,
"New Hampshire": 4,
"Massachusetts": 11,
"Rhode Island": 4,
"Connecticut": 7,
"New Jersey": 14,
"Delaware": 3,
"Maryland": 10,
"District of Columbia": 3
}
mixed = frozenset({"Nebraska", "Maine"})
#Nebraska and Maine allocate electors by districts or something. It's not something I can account for.
download the csv from here:
https://dataverse.harvard.edu/file.xhtml?persistentId=doi:10.7910/DVN/42MVDX/MFU99O&version=5.0
#votes by state (and DC!) per candidate/party w/ total votes column
import csv
"""
Consider a dictionary with no keys:
Attempting to set d[A] = VAL works. In both Python and Lua, you will end with:
{ A = VAL }
Now, try setting d[B][C] = VAL
In Python you get a KeyError for B not existing.
Lua will simply create the intermediary table:
{ A = VAL, B = { C = { VAL } } }
This class will emulate that same behavior, but be careful!
Any errant keys will not be caught. After you create your set, enable normal behavior with stretchdict.lockSafe()
"""
class StretchDict():
def __init__(self):
self.dict = {}
self.lock = False
def __getitem__(self, item):
if self.lock:
return self.dict[item]
else:
try:
return self.dict[item]
except KeyError:
self.dict[item] = StretchDict()
return self.dict[item]
def __setitem__(self, key, val):
self.dict[key] = val
def __repr__(self):
return self.dict.__repr__()
def lockSafe(self):
self.lock = True
data = StretchDict()
with open("Z:/Bucket/datasets/1976-2016-president.csv", "r") as d:
reader = csv.DictReader(d)
for row in reader:
if row["writein"] == "TRUE":
continue
year = row["year"]
state = row["state"]
candidate = row["candidate"]
party = row["party"]
votes_per = row["candidatevotes"]
votes_total = row["totalvotes"]
data[year][state][party] = int(votes_per) #I simplify this data to just party (not candidate name) because that is the actual information you're interested in if you're comparing results over years, but you can see how easy it would be to change this to get actual candidates names.
data[year][state]["total"] = int(votes_total)
data.lockSafe() #If you don't do this, keyerrors will never raise: data["1000"]["The North Pole"] will return StretchDict (represented as {})
print(data["2016"]["California"])
"""
{'democrat': 8753788, 'total': 14181595, 'republican': 4483810, 'libertarian': 478500, 'green': 278657, 'peace & freedom': 66101}
"""
print(data["1000"]["The North Pole"])
"""
Traceback (most recent call last):
File "Z:\Projects\Electoral_Distributions\main.py", line 62, in <module>
print(data["1000"]["The North Pole"])
File "Z:\Projects\Electoral_Distributions\main.py", line 26, in __getitem__
return self.dict[item]
KeyError: '1000'
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment