def _parse_url_path(self, row): """ Perform data cleaning and type conversion on a URL path file entry. """ clean_row = row.copy() # Convert the special flag from string to boolean clean_row['special'] = row['special'].lower() == 'true' # Add an election_slug entry if it doesn't already exist if 'election_slug' not in clean_row: clean_row['election_slug'] = election_slug(self.state, clean_row['date'], clean_row['race_type'], clean_row['special']) return clean_row
def _election_slug(self, election): """ Generate a slug for an election. Arguments: * election - Dictionary of election attributes as returned by the metadata API. """ # Delete the 'state' key in the election attrs, because its a # dict with multiple values we don't care about and we want # to just pass the value of self.state to election_slug. # We can probably delete the key from argument without consequence, # but to be safe and avoid side effects,copy the argument first. election_attrs = election.copy() try: del election_attrs['state'] except KeyError: pass return election_slug(self.state, **election_attrs)
def _parse_url_path(self, row): """ Perform data cleaning and type conversion on a URL path file entry. Args: row: Dictionary representing a single row of data in the CSV file. Returns: A dictionary with cleaned and transformed values. The ``special`` value is converted from a string to a boolean and an ``election_slug`` value is added if not already present. """ clean_row = row.copy() # Convert the special flag from string to boolean clean_row['special'] = row['special'].lower() == 'true' # Add an election_slug entry if it doesn't already exist if 'election_slug' not in clean_row: clean_row['election_slug'] = election_slug(self.state, clean_row['date'], clean_row['race_type'], clean_row['special']) return clean_row
def test_election_slug(self): elec_attrs = { 'md-2012-11-06-general': { 'state': 'md', 'start_date': '2012-11-06', 'special': False, 'race_type': 'general', }, 'md-2008-06-17-special-general': { 'state': 'md', 'start_date': '2008-06-17', 'special': True, 'race_type': 'general', }, 'ar-2011-05-10-special-primary-runoff': { 'state': 'ar', 'start_date': '2011-05-10', 'special': True, 'race_type': 'primary-runoff', }, } for slug, attrs in elec_attrs.items(): self.assertEqual(election_slug(**attrs), slug)
def test_election_slug(self): elec_attrs = { 'md-2012-11-06-general': { 'state': 'md', 'start_date': '2012-11-06', 'special': False, 'race_type': 'general', }, 'md-2008-06-17-special-general': { 'state': 'md', 'start_date': '2008-06-17', 'special': True, 'race_type': 'general', }, 'ar-2011-05-10-special-primary-runoff': { 'state': 'ar', 'start_date': '2011-05-10', 'special': True, 'race_type': 'primary-runoff', }, } for slug, attrs in list(elec_attrs.items()): self.assertEqual(election_slug(**attrs), slug)
def _election_slug(self, election): """ Generate a slug for an election. Args: election: Dictionary of election attributes as returned by the metadata API. Returns: A string containing a unique identifier for an election. For example, "ar-2012-05-22-primary". """ # Delete the 'state' key in the election attrs, because its a # dict with multiple values we don't care about and we want # to just pass the value of self.state to election_slug. # We can probably delete the key from argument without consequence, # but to be safe and avoid side effects,copy the argument first. # try: # del election_attrs['state'] # except KeyError: # pass return election_slug(self.state, election['start_date'], election['race_type'], election['special'])