#!/usr/bin/env python ############################################################################ # # This script rewrites the stateprobs.csv file generated by the MATLAB # scripts into three separate lists of Java Applet parameters. There is # a list for the current state-by-state probabilities, a second for the # scenario in which the Dem. candidate gets a 2% boost, and a third for the # scenario in which the Rep. candidate gets a 2% boost. # # 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 1, 2008 -- Initial version # Sep 13, 2008 -- Added code to write ev_map_runner.sh # ############################################################################ stateprobs = {} stateprobsdem = {} stateprobsrep = {} # 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("../matlab/stateprobs.csv") for line in spfile: values = line[:-1].split(",") stateprobs[values[4]] = values[0] stateprobsdem[values[4]] = values[2] stateprobsrep[values[4]] = values[3] spfile.close() # Output the current state-by-state probabilities as parameters for the # applet version ... ev_map = open("ev_map.html", "w") ev_map_dem = open("ev_map_dem.html", "w") ev_map_rep = open("ev_map_rep.html", "w") # ... and as command-line arguments for the headless version. ev_cl = open("ev_map_runner.sh", "w") ev_cl.write('#!/bin/sh\n\n# THIS IS AN AUTOMATICALLY GENERATED SCRIPT\n\n'); ev_cl.write('java -Djava.awt.headless=true pollcalc output=EV_map.png'); # Now that the files are ready, write out the data for state in sorted(stateprobs): ev_map.write('\n' % (state, stateprobs[state])) ev_map_dem.write('\n' % (state, stateprobsdem[state])) ev_map_rep.write('\n' % (state, stateprobsrep[state])) ev_cl.write(' %s=%s' % (state, stateprobs[state])) # Finally, clean up ev_map.close() ev_map_dem.close() ev_map_rep.close() ev_cl.write('\n') ev_cl.close()