def _standardized_filename(self, election, bits=None, **kwargs):
        """
        Standardize a result filename for an election.

        Arguments:

        election - Dictionary containing election metadata as returned by
                   the elections API. Required.
        bits - List of filename elements.  These will be prepended to the
               filename.  List items will be separated by "__".

        Keyword arguments:

        reporting_level
        jurisdiction
        office
        office_district
        extension - Filename extension, including the leading '.'. 
                    Defaults to extension of first file in elections
                    'direct-links'.
        """
        reporting_level = kwargs.get('reporting_level')
        jurisdiction = kwargs.get('jurisdiction')
        office = kwargs.get('office')
        office_district = kwargs.get('office_district')
        extension = kwargs.get('extension')
        if extension is None:
            extension =  self._filename_extension(election)

        if bits is None:
            bits = []

        bits.extend([
            election['start_date'].replace('-', ''),
            self.state,
        ])

        if election['special']:
            bits.append('special')

        bits.append(election['race_type'].replace('-', '_'))

        if jurisdiction:
            bits.append(slugify(jurisdiction))

        if office:
            bits.append(slugify(office))

        if office_district:
            bits.append(slugify(office_district))

        if reporting_level:
            bits.append(reporting_level)

        return "__".join(bits) + extension 
Пример #2
0
    def _standardized_filename(self, election, bits=None, **kwargs):
        """
        Standardize a result filename for an election.

        Arguments:

        election - Dictionary containing election metadata as returned by
                   the elections API. Required.
        bits - List of filename elements.  These will be prepended to the
               filename.  List items will be separated by "__".

        Keyword arguments:

        reporting_level
        jurisdiction
        office
        office_district
        extension - Filename extension, including the leading '.'. 
                    Defaults to extension of first file in elections
                    'direct-links'.
        """
        reporting_level = kwargs.get('reporting_level')
        jurisdiction = kwargs.get('jurisdiction')
        office = kwargs.get('office')
        office_district = kwargs.get('office_district')
        extension = kwargs.get('extension')
        if extension is None:
            extension = self._filename_extension(election)

        if bits is None:
            bits = []

        bits.extend([
            election['start_date'].replace('-', ''),
            self.state,
        ])

        if election['special']:
            bits.append('special')

        bits.append(election['race_type'].replace('-', '_'))

        if jurisdiction:
            bits.append(slugify(jurisdiction))

        if office:
            bits.append(slugify(office))

        if office_district:
            bits.append(slugify(office_district))

        if reporting_level:
            bits.append(reporting_level)

        return "__".join(bits) + extension
Пример #3
0
    def contest_slug(self):
        """
        A slugified version of the raw contest information.

        This will not neccesarily match the slug on canonical Contest records.
        """
        slug = "%s" % slugify(self.office)
        if self.district:
            slug += "-%s" % slugify(self.district.lstrip('0'))
        if self.primary_party:
            slug += "-%s" % slugify(self.primary_party)
        return slug
Пример #4
0
    def contest_slug(self):
        """
        A slugified version of the raw contest information.

        This will not neccesarily match the slug on canonical Contest records.
        """
        slug = "%s" % slugify(self.office)
        if self.district:
            slug += "-%s" % slugify(self.district.lstrip('0'))
        if self.primary_party:
            slug += "-%s" % slugify(self.primary_party)
        return slug
Пример #5
0
    def slug(self):
        """
        Returns slugified office in format "name-district".

        Example: 

        >>> office = Office(state="MD", name="House of Delegates", district="35B", chamber="lower")
        >>> office.slug
        u'house-of-delegates-35b'
        """
        slug = slugify(self.name, '-')
        if self.district:
            slug += "-%s" % slugify(self.district, '-') 
        return slug
Пример #6
0
    def slug(self):
        """
        Returns slugified office in format "name-district".

        Example:

        >>> office = Office(state="MD", name="House of Delegates", district="35B", chamber="lower")
        >>> office.slug
        u'house-of-delegates-35b'
        """
        slug = slugify(self.name, '-')
        if self.district:
            slug += "-%s" % slugify(self.district, '-')
        return slug
Пример #7
0
 def post_init(cls, sender, document, **kwargs):
     if not document.slug:
         if document.office:
             document.slug = document.make_slug(
                 office=document.office,
                 primary_party=document.primary_party)
         else:
             document.slug = slugify(document.contest_name, '-')
Пример #8
0
 def _build_candidate_kwargs(self, row):
     full_name = row['choice'].strip()
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         'name_slug': slug,
     }
     return kwargs
Пример #9
0
 def _build_candidate_kwargs(self, row):
     full_name = row['choice'].strip()
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         'name_slug': slug,
     }
     return kwargs
Пример #10
0
 def _build_candidate_kwargs(self, row):
     full_name = row['Name'].strip()
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         #TODO: QUESTION: Do we need this? if so, needs a matching model field on RawResult
         'name_slug': slug,
     }
     return kwargs
Пример #11
0
 def _build_candidate_kwargs(self, row):
     full_name = row['Name'].strip()
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         #TODO: QUESTION: Do we need this? if so, needs a matching model field on RawResult
         'name_slug': slug,
     }
     return kwargs
Пример #12
0
 def make_slug(cls, **kwargs):
     bits = (
         kwargs['election_id'],
         kwargs['contest_slug'],
         kwargs['candidate_slug'],
         kwargs['reporting_level'],
         slugify(kwargs['jurisdiction']),
     )
     return u'%s-%s-%s-%s-%s' % bits
Пример #13
0
 def _build_contest_slug(self, row):
     office = row['Office Name'].strip()
     bits = [office]
     district = row['Office District'].strip()
     if district and 'pres' not in office.lower():
         bits.append(district)
     if 'primary' in self.source:
         bits.append(row['Party'].lower())
     return slugify(" ".join(bits), substitute='-')
Пример #14
0
 def make_slug(cls, **kwargs):
     bits = (
         kwargs['election_id'],
         kwargs['contest_slug'],
         kwargs['candidate_slug'],
         kwargs['reporting_level'],
         slugify(kwargs['jurisdiction']),
     )
     return u'%s-%s-%s-%s-%s' % bits
Пример #15
0
 def _build_candidate_kwargs(self, candidateName, partyName):
     slug = slugify(candidateName, substitute='-')
     kwargs = {
         'party': partyName,
         'full_name': candidateName,
         #TODO: QUESTION: Do we need this? if so, needs a matching model field on RawResult
         'name_slug': slug,
     }
     return kwargs
Пример #16
0
 def _build_candidate_kwargs(self, candidate, party):
     # check if party is in name, extract if so
     full_name = candidate
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         'name_slug': slug,
         'party': party,
     }
     return kwargs
Пример #17
0
 def _build_candidate_kwargs(self, candidate):
     if ", " in candidate:
         cand, party = candidate.split(", ")
         party = party.upper()
     else:
         cand = candidate
         party = None
     slug = slugify(cand, substitute='-')
     kwargs = {'full_name': cand, 'name_slug': slug, 'party': party}
     if 'Scatter' in cand:
         kwargs.update({'write_in': True})
     return kwargs
Пример #18
0
 def _build_candidate_kwargs(self, row):
     try:
         full_name = row['Candidate Name'].strip()
     except KeyError:
         # 2000 results use "Candidate" for the column name
         full_name = row['Candidate'].strip()
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         #TODO: QUESTION: Do we need this? if so, needs a matching model field on RawResult
         'name_slug': slug,
     }
     return kwargs
Пример #19
0
 def _build_candidate_kwargs(self, row):
     try:
         full_name = row['Candidate Name'].strip()
     except KeyError:
         # 2000 results use "Candidate" for the column name
         full_name = row['Candidate'].strip()
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         #TODO: QUESTION: Do we need this? if so, needs a matching model field on RawResult
         'name_slug': slug,
     }
     return kwargs
Пример #20
0
 def _build_candidate_kwargs(self, row):
     if row['candidate'].strip() == 'UnderVotes':
         row['candidate'] = 'Under Votes'
     elif row['candidate'].strip() == 'OverVotes':
         row['candidate'] = 'Over Votes'
     elif row['candidate'].strip() == 'WriteinVotes':
         row['candidate'] = 'Write-ins'
     full_name = row['candidate'].strip()
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         'name_slug': slug,
     }
     return kwargs
Пример #21
0
 def _build_candidate_kwargs(self, row):
     if row['candidate'].strip() == 'UnderVotes':
         row['candidate'] = 'Under Votes'
     elif row['candidate'].strip() == 'OverVotes':
         row['candidate'] = 'Over Votes'
     elif row['candidate'].strip() == 'WriteinVotes':
         row['candidate'] = 'Write-ins'
     full_name = row['candidate'].strip()
     slug = slugify(full_name, substitute='-')
     kwargs = {
         'full_name': full_name,
         'name_slug': slug,
     }
     return kwargs
Пример #22
0
 def _build_candidate_kwargs(self, candidate):
     if ", " in candidate:
         cand, party = candidate.split(", ")
         party = party.upper()
     else:
         cand = candidate
         party = None
     slug = slugify(cand, substitute='-')
     kwargs = {
         'full_name': cand,
         'name_slug': slug,
         'party': party
     }
     if 'Scatter' in cand:
         kwargs.update({'write_in': True})
     return kwargs
Пример #23
0
    def candidate_slug(self):
        """
        A slugified version of the raw candidate information.

        This will not neccesarily match the slug on canonical Candidate records.
        """
        if self.full_name:
            name = self.full_name
        else:
            name = self.family_name
            if self.given_name:
                name += " %s" % self.given_name
            if self.additional_name:
                name += " %s" % self.additional_name
            if self.suffix:
                name +=  " %s" % self.suffix
        return slugify(name)
Пример #24
0
    def candidate_slug(self):
        """
        A slugified version of the raw candidate information.

        This will not neccesarily match the slug on canonical Candidate records.
        """
        if self.full_name:
            name = self.full_name
        else:
            name = self.given_name
            if self.additional_name:
                name += " %s" % self.additional_name
            if self.family_name:
                name += " %s" % self.family_name
            if self.suffix:
                name += " %s" % self.suffix
        return slugify(name)
Пример #25
0
    def _key(self, rawresult):
        """
        Returns a string that uniquely identifies a raw result from a particular
        source.
        """
        bits = [rawresult.contest_slug, rawresult.candidate_slug,
                slugify(rawresult.jurisdiction)]

        if rawresult.district:
            bits.append(rawresult.district)

        try:
            bits.append(rawresult.reporting_district)
        except AttributeError:
            pass

        return '-'.join(bits)
Пример #26
0
    def _key(self, rawresult):
        """
        Returns a string that uniquely identifies a raw result from a particular
        source.
        """
        bits = [rawresult.contest_slug, rawresult.candidate_slug,
                slugify(rawresult.jurisdiction)]

        if rawresult.district:
            bits.append(rawresult.district)

        try:
            bits.append(rawresult.reporting_district)
        except AttributeError:
            pass

        return '-'.join(bits)
Пример #27
0
 def load(self):
     workbook = xlrd.open_workbook(self._xls_file_path)
     results = []
     #        workbook = xlrd.open_workbook(xlsfile)
     worksheet = workbook.sheet_by_name('Master')
     raw_offices = [c.value.strip() for c in worksheet.row(0)[5:]]
     last_office_column = self._get_last_office_column(raw_offices)
     offices = self._get_offices(raw_offices, last_office_column)
     candidates = [
         c.value for c in worksheet.row(1)[5:last_office_column + 7]
     ]
     combined = list(zip(offices, candidates))
     for i in range(4, worksheet.nrows):
         row = [c.value for c in worksheet.row(i)[:last_office_column + 5]]
         county = row[0]
         county_ocd_id = [
             c for c in self.datasource._jurisdictions()
             if c['county'].upper() == county.upper()
         ][0]['ocd_id']
         results = row[5:last_office_column + 5]
         for result in zip(combined, results):
             if result[1] == 0.0:
                 continue
             office, candidate = result[0]
             cand, party = candidate.split(' (')
             party = party.replace(')', '')
             votes = result[1]
             kwargs = self._base_kwargs(row)
             slug = slugify(cand, substitute='-')
             kwargs.update({
                 'office': office,
                 'district': district,
                 'full_name': cand,
                 'slug': slug,
                 'party': party,
                 'votes': votes,
                 'reporting_level': 'county',
                 'jurisdiction': county,
                 'ocd_id': county_ocd_id,
             })
             results.append(kwargs)
         RawResult.objects.insert(results)
Пример #28
0
 def _get_or_create_candidate(self, row, contest):
     raw_full_name = row['Candidate Name'].strip()
     slug = slugify(raw_full_name, substitute='-')
     key = (self.election_id, contest.slug, slug)
     try:
         candidate = self.cand_lkup[key]
     except KeyError:
         cand_kwargs = {
             'source': self.source,
             'election_id': self.election_id,
             'contest': contest,
             'contest_slug': contest.slug,
             'state': self.state.upper(),
             'raw_full_name': raw_full_name,
             'slug': slug,
         }
         candidate = Candidate.objects.create(**cand_kwargs)
         candidate.save()
         self.cand_lkup[key] = candidate
     return candidate
Пример #29
0
    def load(self):
        workbook = xlrd.open_workbook(self._xls_file_path)
        results = []
#        workbook = xlrd.open_workbook(xlsfile)
        worksheet = workbook.sheet_by_name('Master')
        raw_offices = [c.value.strip() for c in worksheet.row(0)[5:]]
        last_office_column = self._get_last_office_column(raw_offices)
        offices = self._get_offices(raw_offices, last_office_column)
        candidates = [c.value for c in worksheet.row(1)[5:last_office_column+7]]
        combined = list(zip(offices, candidates))
        for i in range(4, worksheet.nrows):
            row = [c.value for c in worksheet.row(i)[:last_office_column+5]]
            county = row[0]
            county_ocd_id = [c for c in self.datasource._jurisdictions() if c['county'].upper() == county.upper()][0]['ocd_id']
            results = row[5:last_office_column+5]
            for result in zip(combined, results):
                if result[1] == 0.0:
                    continue
                office, candidate = result[0]
                cand, party = candidate.split(' (')
                party = party.replace(')','')
                votes = result[1]
                kwargs = self._base_kwargs(row)
                slug = slugify(cand, substitute='-')
                kwargs.update(
                    {'office': office,
                    'district': district,
                    'full_name': cand,
                    'slug': slug,
                    'party': party,
                    'votes': votes,
                    'reporting_level': 'county',
                    'jurisdiction': county,
                    'ocd_id': county_ocd_id,
                })
                results.append(kwargs)
            RawResult.objects.insert(results)
Пример #30
0
    def _standardized_filename(self, election, bits=None, **kwargs):
        """
        Standardize a result filename for an election.

        For more on filename standardization conventsions, see
        http://docs.openelections.net/archive-standardization/.

        Args:
            election: Dictionary containing election metadata as returned by
            the elections API. Required.
            bits: List of filename elements.  These will be prepended to the
                filename.  List items will be separated by "__".
            reporting_level: Slug string representing the reporting level of
                the data file.  This could be something like 'county' or
                'precinct'.
            jurisdiction: String representing the jurisdiction of the data
                covered in the file.
            office: String representing the office if results are for a single
                office.
            office_district: String representing the office district numver if
               the results in the file are for a single office.
            extension: Filename extension, including the leading '.'.
                  Defaults to extension of first file in election's
                  ``direct_links``.

        Returns:
            A string representing the standardized filename for
            an election's data file.

        """
        # TODO([email protected]) Delegate to
        # openelex.lib.standardized_filename()
        reporting_level = kwargs.get('reporting_level')
        jurisdiction = kwargs.get('jurisdiction')
        office = kwargs.get('office')
        office_district = kwargs.get('office_district')
        extension = kwargs.get('extension')
        if extension is None:
            extension =  self._filename_extension(election['direct_links'][0])

        if bits is None:
            bits = []

        bits.extend([
            election['start_date'].replace('-', ''),
            self.state,
        ])

        if election['special']:
            bits.append('special')

        bits.append(election['race_type'].replace('-', '_'))

        if jurisdiction:
            bits.append(slugify(jurisdiction))

        if office:
            bits.append(slugify(office))

        if office_district:
            bits.append(slugify(office_district))

        if reporting_level:
            bits.append(reporting_level)

        return "__".join(bits) + extension
Пример #31
0
 def make_slug(cls, **kwargs):
     return slugify(kwargs.get('full_name'), '-')
Пример #32
0
 def slug(self):
     return slugify(self.abbrev)
Пример #33
0
def standardized_filename(state, start_date, extension,
    party=None, special=False, race_type=None, reporting_level=None,
    jurisdiction=None, office=None, office_district=None,
    prefix_bits=[], suffix_bits=[], sep="__"):
    """
    Standardize an election-related filename.

    For more on filename standardization conventions, see
    http://docs.openelections.net/archive-standardization/.

    Args:
        state (string): State abbreviation, for example "md".
        start_date (string): String representing the election's start date
            in YYYY-MM-DD format.
        extension (string): Filename extension, including the leading '.'.  For
            example, ".csv".
        party (string, optional): Slug representing the political party of
            information in the file.
        special (boolean, optional): Whether the file contains data related to a
            special election.  Default is False.
        race_type (string, optional): Slug representing the type of election
            contest.  For example, "general", "primary", etc.
        reporting_level (string, optional): Slug representing the reporting level of
            the data file.  This could be something like 'county' or
            'precinct'.
        jurisdiction (string, optional): The jurisdiction of the data
            covered in the file.
        office (string, optional): Office if results are for a single office.
        office_district (string, optional): Office district number if
           the data in the file are for a single office.
        prefix_bits (list, optional): List of strings that will be prepended to
            the generated filename and separated by the value of ``sep``.
        suffix_bits (list, optional): List of strings that will be appended
            to the generated filename and separated by the value of ``sep``.
        sep (string, optional): Separator between filename elements.  Default
            is "__".

    Returns:
        A string representing the standardized filename for
        election-related data.

    """
    # Store filename components in a list that we'll eventually join together
    bits = []

    # Filename starts with the prefix bits, if any
    bits.extend(prefix_bits)

    # All filenames need a date and a state
    bits.extend([
        start_date.replace('-', ''),
        state.lower(),
    ])

    if special:
        bits.append('special')

    if party:
        bits.append(slugify(party))

    if race_type:
        bits.append(race_type.replace('-', '_'))

    if jurisdiction:
        bits.append(slugify(jurisdiction))

    if office:
        bits.append(slugify(office))

    if office_district:
        bits.append(slugify(office_district))

    if reporting_level:
        bits.append(reporting_level)

    bits.extend(suffix_bits)

    return sep.join(bits) + extension
Пример #34
0
 def slug(self):
     return slugify(self.raw_full_name, '-')
Пример #35
0
 def slug(self):
     return slugify(self.abbrev)
Пример #36
0
 def slug(self):
     return slugify(self.raw_office, "-") 
Пример #37
0
 def slug(self):
     return slugify(self.raw_full_name, '-')
Пример #38
0
def standardized_filename(state, start_date, extension,
    party=None, special=False, race_type=None, reporting_level=None,
    jurisdiction=None, office=None, office_district=None,
    prefix_bits=[], suffix_bits=[], sep="__"):
    """
    Standardize an election-related filename.

    For more on filename standardization conventions, see
    http://docs.openelections.net/archive-standardization/.

    Args:
        state (string): State abbreviation, for example "md".
        start_date (string): String representing the election's start date
            in YYYY-MM-DD format.
        extension (string): Filename extension, including the leading '.'.  For
            example, ".csv".
        party (string, optional): Slug representing the political party of
            information in the file.
        special (boolean, optional): Whether the file contains data related to a
            special election.  Default is False.
        race_type (string, optional): Slug representing the type of election
            contest.  For example, "general", "primary", etc.
        reporting_level (string, optional): Slug representing the reporting level of
            the data file.  This could be something like 'county' or
            'precinct'.
        jurisdiction (string, optional): The jurisdiction of the data
            covered in the file.
        office (string, optional): Office if results are for a single office.
        office_district (string, optional): Office district number if
           the data in the file are for a single office.
        prefix_bits (list, optional): List of strings that will be prepended to
            the generated filename and separated by the value of ``sep``.
        suffix_bits (list, optional): List of strings that will be appended
            to the generated filename and separated by the value of ``sep``.
        sep (string, optional): Separator between filename elements.  Default
            is "__".

    Returns:
        A string representing the standardized filename for
        election-related data.

    """
    # Store filename components in a list that we'll eventually join together
    bits = []

    # Filename starts with the prefix bits, if any
    bits.extend(prefix_bits)

    # All filenames need a date and a state
    bits.extend([
        start_date.replace('-', ''),
        state.lower(),
    ])

    if special:
        bits.append('special')

    if party:
        bits.append(slugify(party))

    if race_type:
        bits.append(race_type.replace('-', '_'))

    if jurisdiction:
        bits.append(slugify(jurisdiction))

    if office:
        bits.append(slugify(office))

    if office_district:
        bits.append(slugify(office_district))

    if reporting_level:
        bits.append(reporting_level)

    bits.extend(suffix_bits)

    return sep.join(bits) + extension
Пример #39
0
 def make_slug(cls, **kwargs):
     return slugify(kwargs.get('full_name'), '-')
Пример #40
0
 def slug(self):
     return slugify(self.raw_office, "-")