#!/usr/bin/env python ############################################################################ # # This script produces an HTML table which displays the relative influence # voters in different swing states have over the outcome of the election. # The influence statistic is normalized so that NJ voters, living in a # non-swing state have power 1.0, hence the term "jerseyvotes." The statistic # is calculated by the MATLAB script EV_jerseyvotes.m # # Author: Andrew Ferguson # # Script written for election.princeton.edu run by Samuel S.-H. Wang under # noncommercial-use-only license: # You may use or modify this software, but only for noncommericial purposes. # To seek a commercial-use license, contact sswang@princeton.edu # # Update History: # Aug 19, 2012 -- moved to GitHub; future updates in commit messages # Jul 7, 2012 -- Initial port from 2008 version # ############################################################################ from decimal import Decimal from ev_update_polls import state_names huffpo_url_pattern = "http://elections.huffingtonpost.com/pollster/2016-%s-president-trump-vs-clinton" num_states_to_display = 15 jerseyvotes = {} statemargins = {} districts = { 'N1': 'Nebraska', 'N2': 'Nebraska', 'N3': 'Nebraska', 'M1': 'Maine', 'M2': 'Maine' } names = { 'N1': 'NE CD 1', 'N2': 'NE CD 2', 'N3': 'NE CD 3', 'M1': 'ME CD 1', 'M2': 'ME CD 2' } offsets = { 'N1': 5, 'N2': 15.5, 'N3': -22, 'M1': 6, 'M2': -6 } def display_state(jvdisplay, state): jvdisplay.write("\n") if state in districts: jvdisplay.write("\t%s" % names[state]) else: jvdisplay.write("\t%s" % state) margin = statemargins[state] if state in districts : data_url = huffpo_url_pattern % districts[state].lower().replace(" ", "-") else: data_url = huffpo_url_pattern % state_names[state].lower().replace(" ", "-") if state == "FL": data_url = "http://elections.huffingtonpost.com/pollster/2016-florida-presidential-general-election-trump-vs-clinton" if margin > 0: jvdisplay.write('' 'Clinton +%s%%' % (data_url, margin)) elif margin < 0: jvdisplay.write('' 'Trump +%s%%' % (data_url, -margin)) else: jvdisplay.write('' 'Tied' % data_url) if state != "NJ": jvdisplay.write("%#.1f\n" % jerseyvotes[state]) else: jvdisplay.write("%s\n" % jerseyvotes[state]) # NJ power is not rounded jvdisplay.write("\n") # The jerseyvotes.csv file has the following format: # index, state abbreviation, voter power (most powerful = 100) jvfile = open("EV_jerseyvotes.csv") for line in jvfile: values = line[:-1].split(",") jerseyvotes[values[1]] = Decimal(values[2]) jvfile.close() # The stateprobs.csv file has the following format: # prob. of Dem win, margin, prob. of Dem win with Dem 2% boost, prob. # of Dem win with Rep 2% boost, state abbreviation spfile = open("EV_stateprobs.csv") for line in spfile: values = line[:-1].split(",") statemargins[values[4]] = Decimal(values[1]) spfile.close() # Output the HTML table displaying the jerseyvotes ranking. It will be # included into the blog sidebar by a WordPress widget. jvdisplay = open("EV_jerseyvotes.html", "w") jvdisplay.write('\n') jvdisplay.write("\n") jvdisplay.write("\t\n") jvdisplay.write("\n") top_states = sorted(jerseyvotes, key=lambda x: jerseyvotes[x], reverse=True)[:num_states_to_display] for state in top_states: display_state(jvdisplay, state) if "NJ" not in top_states: display_state(jvdisplay, "NJ") jvdisplay.write("
StateMarginPower
\n") jvdisplay.close()