コード例 #1
0
 def test_reporting_levels_for_election(self):
     start_date = date(2000, 3, 7)
     state = 'MD'
     expected_levels = ['precinct', 'county']
     for level in expected_levels:
         RawResultFactory(state=state, start_date=start_date,
             reporting_level=level, election_type='general')
     levels = reporting_levels_for_election(state, start_date.strftime("%Y%m%d"),
        'general', raw=True)
     self.assertEqual(len(levels), len(expected_levels))
     for level in expected_levels:
         self.assertIn(level, levels)
コード例 #2
0
 def test_reporting_levels_for_election(self):
     start_date = date(2000, 3, 7)
     state = 'MD'
     expected_levels = ['precinct', 'county']
     for level in expected_levels:
         RawResultFactory(state=state,
                          start_date=start_date,
                          reporting_level=level,
                          election_type='general')
     levels = reporting_levels_for_election(state,
                                            start_date.strftime("%Y%m%d"),
                                            'general',
                                            raw=True)
     self.assertEqual(len(levels), len(expected_levels))
     for level in expected_levels:
         self.assertIn(level, levels)
コード例 #3
0
def result_urls(election, raw=False):
    urls = {}
    state = election['state']['postal']
    datefilter = election['start_date'].replace('-', '')
    if raw:
        baker_cls = RawBaker
    else:
        baker_cls = Baker

    for level in reporting_levels_for_election(state, datefilter,
            election['race_type'], raw):
        filename = baker_cls.filename("csv", state=state, datefilter=datefilter,
            election_type=election['race_type'], reporting_level=level)
        urls[level] = published_url(state, filename, raw)

    return urls
コード例 #4
0
ファイル: bake.py プロジェクト: warwickmm/openelections-core
def election_file(state,
                  fmt='csv',
                  outputdir=None,
                  datefilter=None,
                  electiontype=None,
                  level=None,
                  raw=False):
    """
    Write election and candidate data with one election per file.
    """
    timestamp = datetime.now()

    if raw:
        baker_cls = RawBaker
    else:
        baker_cls = Baker

    if datefilter is None or re.match(r'\d{4}', datefilter):
        # No date specfied, so bake all elections or date filter
        # represents a single year, so bake all elections for that year.
        elections = get_election_dates_types(state, datefilter)
    else:
        # Date filter is for a day, grab that election specifically
        if electiontype is None:
            msg = "You must specify the election type when baking results for a single date."
            sys.exit(msg)
        elections = [(datefilter, electiontype)]

    for election_date, election_type in elections:
        if level is not None:
            reporting_levels = [level]
        else:
            reporting_levels = reporting_levels_for_election(
                state, election_date, election_type, raw)

        for reporting_level in reporting_levels:
            msg = "Baking {} level results for {} election on {}\n".format(
                reporting_level, election_type, election_date)
            sys.stdout.write(msg)
            baker = baker_cls(state=state,
                              datefilter=election_date,
                              election_type=election_type,
                              reporting_level=reporting_level)

            baker.collect_items()\
                 .write(fmt, outputdir=outputdir, timestamp=timestamp) \
                 .write_manifest(outputdir=outputdir, timestamp=timestamp)
コード例 #5
0
ファイル: bake.py プロジェクト: warwickmm/openelections-core
def result_urls(election, raw=False):
    urls = {}
    state = election['state']['postal']
    datefilter = election['start_date'].replace('-', '')
    if raw:
        baker_cls = RawBaker
    else:
        baker_cls = Baker

    for level in reporting_levels_for_election(state, datefilter,
                                               election['race_type'], raw):
        filename = baker_cls.filename("csv",
                                      state=state,
                                      datefilter=datefilter,
                                      election_type=election['race_type'],
                                      reporting_level=level)
        urls[level] = published_url(state, filename, raw)

    return urls
コード例 #6
0
def election_file(state, fmt='csv', outputdir=None, datefilter=None,
                  electiontype=None, level=None, raw=False):
    """
    Write election and candidate data with one election per file.
    """
    timestamp = datetime.now()

    if raw:
        baker_cls = RawBaker
    else:
        baker_cls = Baker

    if datefilter is None or re.match( r'\d{4}', datefilter):
        # No date specfied, so bake all elections or date filter
        # represents a single year, so bake all elections for that year.
        elections = get_election_dates_types(state, datefilter)
    else:
        # Date filter is for a day, grab that election specifically
        if electiontype is None:
            msg = "You must specify the election type when baking results for a single date."
            sys.exit(msg)
        elections = [(datefilter, electiontype)]

    for election_date, election_type in elections:
        if level is not None:
            reporting_levels = [level]
        else:
            reporting_levels = reporting_levels_for_election(state, election_date,
                election_type, raw)

        for reporting_level in reporting_levels:
            msg = "Baking {} level results for {} election on {}\n".format(
                reporting_level, election_type, election_date)
            sys.stdout.write(msg)
            baker = baker_cls(state=state, datefilter=election_date,
                  election_type=election_type, reporting_level=reporting_level)

            baker.collect_items()\
                 .write(fmt, outputdir=outputdir, timestamp=timestamp) \
                 .write_manifest(outputdir=outputdir, timestamp=timestamp)