Пример #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):
     # 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']
Пример #4
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", "")
Пример #5
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", "")
Пример #6
0
 def test_clean_office_other(self):
     race = Race("2012-11-06", "President")
     self.assertEquals(race.office, "President")
     self.assertEquals(race.district, "")
Пример #7
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)
Пример #8
0
class TestRace(TestCase):

    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", "")

    def test_total_votes_default(self):
        "Race total votes should default to zero"
        self.assertEquals(self.race.total_votes, 0)

    def test_total_votes_update(self):
        "Race.add_result should update racewide vote count"
        self.race.add_result(self.smith_result)
        self.assertEquals(self.race.total_votes, 2000)

    def test_add_result_to_candidate(self):
        "Race.add_result should update a unique candidate instance"
        # Add a vote twice. If it's the same candidate, vote total should be sum of results
        self.race.add_result(self.smith_result)
        self.race.add_result(self.smith_result)
        cand_key = (self.smith_result['party'], self.smith_result['candidate'])
        candidate = self.race.candidates[cand_key]
        self.assertEquals(candidate.votes, 4000)

    def test_winner_has_flag(self):
        "Winner flag should be assigned to candidates with most votes"
        self.race.add_result(self.doe_result)
        self.race.add_result(self.smith_result)
        self.race.assign_winner()
        smith = [cand for cand in self.race.candidates.values() if cand.last_name == 'Smith'][0]
        self.assertEqual(smith.winner, 'X')

    def test_loser_has_no_winner_flag(self):
        "Winner flag should not be assigned to candidate who does not have highest vote total"
        self.race.add_result(self.doe_result)
        self.race.add_result(self.smith_result)
        self.race.assign_winner()
        doe = [cand for cand in self.race.candidates.values() if cand.last_name == 'Doe'][0]
        self.assertEqual(doe.winner, '')

    def test_tie_race(self):
        "Winner flag should not be assigned to any candidate in a tie race"
        # Modify Doe vote count to make it a tie
        self.doe_result['votes'] = 2000
        self.race.add_result(self.doe_result)
        self.race.add_result(self.smith_result)
        self.race.assign_winner()
        for cand in self.race.candidates.values():
            self.assertEqual(cand.winner, '')
Пример #9
0
class TestRace(TestCase):
    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", "")

    def test_total_votes_default(self):
        "Race total votes should default to zero"
        self.assertEquals(self.race.total_votes, 0)

    def test_total_votes_update(self):
        "Race.add_result should update racewide vote count"
        self.race.add_result(self.smith_result)
        self.assertEquals(self.race.total_votes, 2000)

    def test_add_result_to_candidate(self):
        "Race.add_result should update a unique candidate instance"
        # Add a vote twice. If it's the same candidate, vote total should be sum of results
        self.race.add_result(self.smith_result)
        self.race.add_result(self.smith_result)
        cand_key = (self.smith_result['party'], self.smith_result['candidate'])
        candidate = self.race.candidates[cand_key]
        self.assertEquals(candidate.votes, 4000)

    def test_winner_has_flag(self):
        "Winner flag should be assigned to candidates with most votes"
        self.race.add_result(self.doe_result)
        self.race.add_result(self.smith_result)
        self.race.assign_winner()
        smith = [
            cand for cand in self.race.candidates.values()
            if cand.last_name == 'Smith'
        ][0]
        self.assertEqual(smith.winner, 'X')

    def test_loser_has_no_winner_flag(self):
        "Winner flag should not be assigned to candidate who does not have highest vote total"
        self.race.add_result(self.doe_result)
        self.race.add_result(self.smith_result)
        self.race.assign_winner()
        doe = [
            cand for cand in self.race.candidates.values()
            if cand.last_name == 'Doe'
        ][0]
        self.assertEqual(doe.winner, '')

    def test_tie_race(self):
        "Winner flag should not be assigned to any candidate in a tie race"
        # Modify Doe vote count to make it a tie
        self.doe_result['votes'] = 2000
        self.race.add_result(self.doe_result)
        self.race.add_result(self.smith_result)
        self.race.assign_winner()
        for cand in self.race.candidates.values():
            self.assertEqual(cand.winner, '')