Last active
January 6, 2021 04:20
-
-
Save intentionally-left-nil/6e821e8e7520ccfdaaedd7199dfb6f42 to your computer and use it in GitHub Desktop.
Takes the current vote counts per-county and extrapolates them to 100%
This file contains hidden or 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
import urllib.request | |
import json | |
headers = {'Referrer': 'https://results.decisiondeskhq.com/', 'Accept': 'application/json', | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.16; rv:84.0) Gecko/20100101 Firefox/84.0'} | |
url = 'https://embeds.ddhq.io/api/v2/2020general_results/2021senate_ga' | |
request = urllib.request.Request(url, headers=headers) | |
response = json.load(urllib.request.urlopen(request)) | |
for data in response['data']: | |
candidates = data['candidates'] | |
c1_id = str(candidates[0]['cand_id']) | |
c2_id = str(candidates[1]['cand_id']) | |
counts = [] | |
for county in data['countyResults']['counties']: | |
c1_votes = county['votes'][c1_id] | |
c2_votes = county['votes'][c2_id] | |
counts.append( | |
(c1_votes, c2_votes, county['estimated_votes']['estimated_votes_mid'])) | |
scaled_counts = [] | |
for vote1, vote2, expected in counts: | |
total = vote1 + vote2 | |
ratio = 1 if total >= expected else total / expected | |
scaled_counts.append((round(vote1 / ratio), round(vote2 / ratio))) | |
def get_winner(c1_votes, c2_votes): | |
c1_sum = sum(c1_votes) | |
c2_sum = sum(c2_votes) | |
winner = candidates[0]['last_name'] if c1_sum > c2_sum else candidates[1]['last_name'] | |
delta = (abs(c1_sum - c2_sum)) | |
return ((winner, delta)) | |
[c1_votes, c2_votes] = list(zip(*scaled_counts)) | |
expected_winner, expected_delta = get_winner(c1_votes, c2_votes) | |
[c1_actual, c2_actual, _] = list(zip(*counts)) | |
current_winner, current_delta = get_winner(c1_votes, c2_votes) | |
print( | |
f"{current_winner} is currently winning by {current_delta:,} votes") | |
print( | |
f"If HQ's per-county totals are extrapolated to 100%, {expected_winner} would win by {expected_delta:,} votes") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment