def elections(self, year=None): """ Retrieve election metadata for this state. Args: year: Only return metadata for elections from the specified year, provided as an integer. Defaults to returning elections for all years. Returns: A dictionary, keyed by year. Each value is a list of dictonariess, each representing an election and its metadata for that year. The election dictionaries match the output of the Metadata API (http://docs.openelections.net/metadata-api/). The election dictionaries have an additional ``slug`` key that can be used as an election identifier. """ # Fetch all elections initially and stash on instance if not hasattr(self, '_elections'): # Store elections by year self._elections = {} for elec in elec_api.find(self.state): yr = int(elec['start_date'][:4]) # Add elec slug elec['slug'] = self._election_slug(elec) self._elections.setdefault(yr, []).append(elec) if year: year_int = int(year) return {year_int: self._elections[year_int]} return self._elections
def elections(self, year=None): # Fetch all elections initially and stash on instance if not hasattr(self, '_elections'): # Store elections by year self._elections = {} for elec in elec_api.find(self.state): rtype = elec['race_type'].lower() elec['slug'] = "-".join((self.state, elec['start_date'], rtype)) yr = int(elec['start_date'][:4]) self._elections.setdefault(yr, []).append(elec) if year: year_int = int(year) return {year_int: self._elections[year_int]} return self._elections
def elections(self, year=None): # Fetch all elections initially and stash on instance if not hasattr(self, '_elections'): # Store elections by year self._elections = {} for elec in elec_api.find(self.state): yr = int(elec['start_date'][:4]) # Add elec slug elec['slug'] = self._elec_slug(elec) self._elections.setdefault(yr, []).append(elec) if year: year_int = int(year) return {year_int: self._elections[year_int]} return self._elections
def get_elections(state, datefilter=None): """ Get all elections. Args: state: Required. Postal code for a state. For example, "md". datefilter: Date specified in "YYYY" or "YYYYMMDD" used to filter elections before they are baked. Returns: A list of dictionaries, each describing an election for the specified state. The elections are sorted by date. """ elections = elec_api.find(state.upper()) if datefilter: date_prefix = format_date(datefilter) elections = [elec for elec in elections if elec['start_date'].startswith(date_prefix)] return sorted(elections, key=lambda x: x['start_date'])
def get_elections(state, datefilter): """ Get all elections. Args: state: Required. Postal code for a state. For example, "md". datefilter: Date specified in "YYYY" or "YYYYMMDD" used to filter elections before they are baked. Returns: A list of dictionaries, each describing an election for the specified state. The elections are sorted by date. """ elections = elec_api.find(state.upper(), datefilter) if datefilter: date_prefix = datefilter[0:4] #format_date(datefilter) elections = [ elec for elec in elections if elec['start_date'].startswith(date_prefix) ] return sorted(elections, key=lambda x: x['start_date'])