Esempio n. 1
0
 def test_margin_of_victory(self):
     self.assertEqual(
         calculate.margin_of_victory([3285, 2804, 7170]),
         3885
     )
     self.assertEqual(
         calculate.margin_of_victory([50708, 20639]),
         50708 - 20639
     )
Esempio n. 2
0
    def write(self):
        print "- Writing {} rows to {}".format(len(self.clean_presidential),
                                               self.out_path)
        writer = csv.DictWriter(open(self.out_path, 'wb'),
                                fieldnames=[
                                    'fips', 'name', 'dem_total', 'gop_total',
                                    'other_total', 'novote_total',
                                    'grand_total', 'leader', 'leader_total',
                                    'margin'
                                ])
        writer.writeheader()
        sorted_data = sorted(self.clean_presidential.items(),
                             key=lambda x: x[0])
        fips_dict = dict(
            (d['name'].upper(), d['state'] + d['county'])
            for d in csv.DictReader(
                open(os.path.join(self.data_dir, 'fips.csv'), 'rbU')))
        json_dict = {}
        for name, data in sorted_data:
            data.update({'name': name})
            fips = fips_dict[name.upper()]
            data.update({
                'margin':
                calculate.margin_of_victory(
                    [data['dem_total'], data['gop_total']])
            })
            if data['dem_total'] > data['gop_total']:
                data.update({
                    'leader': 'dem',
                    'leader_total': data['dem_total'],
                })
            elif data['dem_total'] < data['gop_total']:
                data.update({
                    'leader': 'gop',
                    'leader_total': data['gop_total'],
                })
            elif data['dem_total'] == data['gop_total']:
                data.update({
                    'leader': 'tie',
                    'leader_total': 0,
                })

            json_dict[fips] = copy.deepcopy(data)

            data.update({'fips': fips})
            writer.writerow(data)

        print "- Writing {} rows to {}".format(len(self.clean_presidential),
                                               self.json_path)
        json.dump(json_dict, open(self.json_path, 'wb'), indent=4)
Esempio n. 3
0
 def transform(self):
     """
     Transform the results file so that there is only one row
     for each precinct, with some summary stats calculated.
     """
     # Open the CSV
     general_csv = csv.DictReader(open(self.general_csv_path, 'r'))
     # Loop through the rows
     grouped_by_precinct = {}
     for row in general_csv:
         # And regroup them so each fake id is keyed to
         # all of the list totals for that precinct
         fake_id = "%s-%s" % (row['mesa_desde'], row['mesa_hasta'])
         try:
             grouped_by_precinct[fake_id][row['vot_parcodigo']] = row['total']
         except KeyError:
             grouped_by_precinct[fake_id] = {
                 row['vot_parcodigo']: row['total']
             }
     # Now loop through that
     outrows = []
     for fake_id, totals in grouped_by_precinct.items():
         # Figure out the overall total of votes
         overall_total = sum(map(int, totals.values()))
         # Rank the lists from highest to lowest votes
         ranking = sorted(
             totals.items(),
             key=lambda x:int(x[1]),
             reverse=True
         )
         # Pull out the leader and their vote total
         leader, leader_total = ranking[0]
         # Calculate their margin of victory over the second place list
         margin_of_victory = calculate.margin_of_victory([int(i[1]) for i in ranking])
         # Start up a row to print out
         outrow = [fake_id,]
         # Load in the lists in the same "alphabetical" order
         for list_, total in sorted(totals.items(), key=lambda x:x[0]):
             outrow.append(int(total))
         # Load in the extra stuff we've calculated
         outrow.append(int(overall_total))
         outrow.append(leader)
         outrow.append(int(leader_total))
         outrow.append(int(margin_of_victory))
         # Add this row to the global list outside the loop
         outrows.append(outrow)
     # Open up a text file and write out all the data
     outcsv = csv.writer(open(self.outcsv_path, 'w'))
     outcsv.writerow(self.outheaders)
     outcsv.writerows(outrows)
Esempio n. 4
0
 def transform(self):
     """
     Transform the results file so that there is only one row
     for each precinct, with some summary stats calculated.
     """
     # Open the CSV
     general_csv = csv.DictReader(open(self.general_csv_path, "r"))
     # Loop through the rows
     grouped_by_precinct = {}
     for row in general_csv:
         # And regroup them so each fake id is keyed to
         # all of the list totals for that precinct
         fake_id = "%s-%s" % (row["mesa_desde"], row["mesa_hasta"])
         try:
             grouped_by_precinct[fake_id][row["vot_parcodigo"]] = row["total"]
         except KeyError:
             grouped_by_precinct[fake_id] = {row["vot_parcodigo"]: row["total"]}
     # Now loop through that
     outrows = []
     for fake_id, totals in grouped_by_precinct.items():
         # Figure out the overall total of votes
         overall_total = sum(map(int, totals.values()))
         # Rank the lists from highest to lowest votes
         ranking = sorted(totals.items(), key=lambda x: int(x[1]), reverse=True)
         # Pull out the leader and their vote total
         leader, leader_total = ranking[0]
         # Calculate their margin of victory over the second place list
         margin_of_victory = calculate.margin_of_victory([int(i[1]) for i in ranking])
         # Start up a row to print out
         outrow = [fake_id]
         # Load in the lists in the same "alphabetical" order
         for list_, total in sorted(totals.items(), key=lambda x: x[0]):
             outrow.append(int(total))
         # Load in the extra stuff we've calculated
         outrow.append(int(overall_total))
         outrow.append(leader)
         outrow.append(int(leader_total))
         outrow.append(int(margin_of_victory))
         # Add this row to the global list outside the loop
         outrows.append(outrow)
     # Open up a text file and write out all the data
     outcsv = csv.writer(open(self.outcsv_path, "w"))
     outcsv.writerow(self.outheaders)
     outcsv.writerows(outrows)
Esempio n. 5
0
 def test_margin_of_victory(self):
     self.assertEqual(calculate.margin_of_victory([3285, 2804, 7170]), 3885)
     self.assertEqual(calculate.margin_of_victory([50708, 20639]),
                      50708 - 20639)