Run update21, if you haven't already, to create the cs21/labs/09 directory. Then cd into your cs21/labs/09 directory and create the python program for lab 09 in this directory (handin21 looks for your lab 09 assignments in your cs21/labs/09 directory).
$ update21 $ cd cs21/labs/09 $ vim contributions.py
The 2012 Presidential Election was the most expensive election in the history of the United states. In this lab we are going to look at individual contributions for Barack Obama and Mitt Romney during the 2012 election cycle (which is for the years 2011 and 2012). Your assignment this week is to create a program (contributions.py) that allows the user to explore our campaign contribution database.
The contribution database is contained in a single file, /usr/local/doc/contributions2012.txt, which contains individual contribution data for both candidates aggregated by zip code. Don't copy this file to your 09 directory, just use "/usr/local/doc/contributions2012.txt" in your python program. Each line of the file contains nine fields separated by commas:
19081,SWARTHMORE,PA,39.89672470,-75.34742737,125,51018,14,10060
If you want, you can look at the file with the less command:
$ less /usr/local/doc/contributions2012.txt
Your program should prompt the user for either a zip code or at least part of a city name. Depending on what the user types, your program should do one of the following:
A sample run is shown here:
$ python contributions.py Welcome to contributions.py v1.0 Enter a zip code or the name of a city and I'll tell you campaign contribution information about it. Enter a blank line when you're finished... zip or city: 19081 Contribution information for SWARTHMORE, PA 19081 Obama: 125 contributions totaling $ 51018 Romney: 14 contributions totaling $ 10060 Top ten contribution zip codes for Obama in PA: PHILADELPHIA, PA 19103: 361 contributions totaling $ 209770 PHILADELPHIA, PA 19119: 384 contributions totaling $ 178534 PITTSBURGH, PA 15217: 310 contributions totaling $ 153372 PHILADELPHIA, PA 19106: 212 contributions totaling $ 114749 BRYN MAWR, PA 19010: 223 contributions totaling $ 108930 WAYNE, PA 19087: 239 contributions totaling $ 107962 PHILADELPHIA, PA 19118: 221 contributions totaling $ 104645 WYNNEWOOD, PA 19096: 190 contributions totaling $ 96328 PHILADELPHIA, PA 19147: 214 contributions totaling $ 90611 PHILADELPHIA, PA 19130: 181 contributions totaling $ 90516 Top ten contribution zip codes for Romney in PA: BRYN MAWR, PA 19010: 184 contributions totaling $ 236000 WAYNE, PA 19087: 164 contributions totaling $ 150680 VILLANOVA, PA 19085: 123 contributions totaling $ 139125 HAVERFORD, PA 19041: 93 contributions totaling $ 131430 MALVERN, PA 19355: 137 contributions totaling $ 121189 SEWICKLEY, PA 15143: 133 contributions totaling $ 117870 WEST CHESTER, PA 19382: 120 contributions totaling $ 114207 PHILADELPHIA, PA 19103: 94 contributions totaling $ 105400 GLADWYNE, PA 19035: 92 contributions totaling $ 103400 NEW HOPE, PA 18938: 107 contributions totaling $ 102150
zip or city: 8765309 Please enter a 5-digit zip code... zip or city: london Here are all the cities I can find that start with that and have contributions, sorted by zip code... LONDONDERRY, NH 03053, Obama: $ 8250, Romney $ 8420 LONDONDERRY, VT 05148, Obama: $ 450, Romney $ 2000 LONDON, KY 40741, Obama: $ 250, Romney $ 8800 LONDON, KY 40743, Obama: $ 0, Romney $ 7250 LONDON, KY 40744, Obama: $ 2875, Romney $ 5211 LONDON, OH 43140, Obama: $ 250, Romney $ 493 LONDONDERRY, OH 45647, Obama: $ 0, Romney $ 750 LONDON, TX 76854, Obama: $ 0, Romney $ 3000 zip or city: 02474 Contribution information for ARLINGTON, MA 02474 Obama: 181 contributions totaling $ 82137 Romney: 22 contributions totaling $ 18300 Top ten contribution zip codes for Obama in MA: CAMBRIDGE, MA 02138: 1146 contributions totaling $ 686051 CAMBRIDGE, MA 02139: 555 contributions totaling $ 273167 BROOKLINE, MA 02446: 549 contributions totaling $ 267010 BROOKLINE, MA 02445: 480 contributions totaling $ 253217 BOSTON, MA 02116: 384 contributions totaling $ 229269 BELMONT, MA 02478: 431 contributions totaling $ 220564 CAMBRIDGE, MA 02140: 389 contributions totaling $ 219822 JAMAICA PLAIN, MA 02130: 444 contributions totaling $ 212523 CONCORD, MA 01742: 424 contributions totaling $ 212011 LEXINGTON, MA 02421: 385 contributions totaling $ 194054 Top ten contribution zip codes for Romney in MA: WELLESLEY HILLS, MA 02481: 238 contributions totaling $ 320100 WESTON, MA 02493: 246 contributions totaling $ 313576 BOSTON, MA 02116: 204 contributions totaling $ 301444 BELMONT, MA 02478: 188 contributions totaling $ 232584 CONCORD, MA 01742: 106 contributions totaling $ 128356 WAYLAND, MA 01778: 100 contributions totaling $ 117120 WELLESLEY, MA 02482: 89 contributions totaling $ 115555 BOSTON, MA 02108: 81 contributions totaling $ 109700 CHESTNUT HILL, MA 02467: 82 contributions totaling $ 104910 DOVER, MA 02030: 77 contributions totaling $ 103450
zip or city: pickles Sorry...I can't find anything for that city... zip or city: vien Here are all the cities I can find that start with that and have contributions... VIENNA, ME 04360, Obama: 1000, Romney 0 VIENNA, VA 22180, Obama: 42545, Romney 47995 VIENNA, VA 22181, Obama: 87293, Romney 66798 VIENNA, VA 22182, Obama: 55355, Romney 127409 VIENNA, VA 22183, Obama: 0, Romney 1925 VIENNA, WV 26105, Obama: 1750, Romney 11750 VIENNA, GA 31092, Obama: 1000, Romney 500 VIENNA, OH 44473, Obama: 500, Romney 3250 VIENNA, IL 62995, Obama: 0, Romney 2500
zip or city: <Enter> Goodbye!
You should practice good top-down design, incrementally implement and test your solution, and document your code with comments. While much of the design is up to you, the requirements below are designed to avoid some headaches in the initial design.
[[contribution data from line 1], [contribution data from line 2], ..., [contribution data from last line]]
from boundaries import * state = "VA" w = getStateGraphWin(state) if w != None: # plot all zip codes for this state here...use lat/long for each zip code w.getMouse() w.close()
The contribution data used in this lab was compiled
from
opensecrets.org. We tried to compile this data as accurately as
possible, but there are likely inaccuracies and omissions, so use
the data for entertainment purposes only. :) If you find an error
with our data, let us know and we will be happy to fix it, but we
are not trying to maintain a comprehensive list. If you are
interested in learning more about money in politics, opensecrets.org
is a great resource to help you to learn more.
Once you are satisfied with your program, hand it in by typing handin21 in a terminal window.