コード例 #1
0
def parse_and_clean(path):
    """Parse downloaded results file.


    RETURNS:

        A dictionary containing race key and Race instances as values.

    """
    # Create reader for ingesting CSV as array of dicts
    reader = csv.DictReader(open(path, 'rb'))

    results = {}

    # Initial data clean-up
    for row in reader:
        # Convert votes to integer
        row['votes'] = int(row['votes'])

        # Store races by slugified office and district (if there is one)
        race_key = row['office'] 
        if row['district']:
            race_key += "-%s" % row['district']

        try:
            race = results[race_key]
        except KeyError:
            race = Race(row['date'], row['office'], row['district'])
            results[race_key] = race

        race.add_result(row)

    return results
コード例 #2
0
 def setUp(self):
     # Recall that sample data only has a single Presidential race
     race = Race('2012-11-06', 'President', '')
     for result in self.SAMPLE_RESULTS:
         race.add_result(result)
     # summarize function expects a dict, keyed by race
     summary = summarize({'President': race})
     self.race = summary['President']
コード例 #3
0
 def setUp(self):
     self.smith_result = {
         'date': '2012-11-06',
         'candidate': 'Smith, Joe',
         'party': 'Dem',
         'office': 'President',
         'county': 'Fairfax',
         'votes': 2000,
     }
     self.doe_result = {
         'date': '2012-11-06',
         'candidate': 'Doe, Jane',
         'party': 'GOP',
         'office': 'President',
         'county': 'Fairfax',
         'votes': 1000,
     }
     self.race = Race("2012-11-06", "President", "")
コード例 #4
0
 def test_clean_office_other(self):
     race = Race("2012-11-06", "President")
     self.assertEquals(race.office, "President")
     self.assertEquals(race.district, "")
コード例 #5
0
 def test_clean_office_rep(self):
     race = Race("2012-11-06", "U.S. Rep - 1")
     self.assertEquals(race.office, "U.S. House of Representatives")
     self.assertEquals(race.district, 1)