Example #1
0
def state_file(state,
               place=None,
               fmt='csv',
               outputdir=None,
               datefilter=None,
               electiontype=None,
               level=None,
               raw=False):
    """
    Writes election and candidate data, along with a manifest to structured
    files.

    Args:
        state: Required. Postal code for a state.  For example, "md".
        place: A place within the state. Default is to bake the entire state. 
                For example, "chicago"
        fmt: Format of output files.  This can be "csv" or "json".  Defaults
          to "csv".
        outputdir: Directory where output files will be written. Defaults to 
            "openelections/us/bakery"
        datefilter: Date specified in "YYYY" or "YYYY-MM-DD" used to filter
            elections before they are baked.
        electiontype: Election type. For example, general, primary, etc. 
        level: Reporting level of the election results.  For example, "state",
            "county", "precinct", etc. Value must be one of the options
            specified in openelex.models.Result.REPORTING_LEVEL_CHOICES.
        raw: Bake RawResult records instead of cleaned and transformed results.

    """
    # TODO: Decide if datefilter should be required due to performance
    # considerations.

    # TODO: Implement filtering by office, district and party after the
    # the data is standardized

    # TODO: Filtering by election type and level

    timestamp = datetime.now()

    filter_kwargs = {}
    if electiontype:
        filter_kwargs['election_type'] = electiontype

    if level:
        filter_kwargs['reporting_level'] = level
    if place:
        filter_kwargs['place'] = place

    if raw:
        baker = RawBaker(state=state, datefilter=datefilter, **filter_kwargs)
    else:
        baker = Baker(state=state, datefilter=datefilter, **filter_kwargs)

    baker.collect_items() \
         .write(fmt, outputdir=outputdir, timestamp=timestamp) \
         .write_manifest(outputdir=outputdir, timestamp=timestamp)
Example #2
0
 def test_default_outputdir(self):
     baker = Baker(state='md')
     path = os.path.join(os.path.join('openelex', 'us', 'bakery'))
     outputdir = baker.default_outputdir()
     self.assertTrue(outputdir.endswith(path))
Example #3
0
 def test_write_unsupported_format(self):
     baker = Baker(state='md')
     self.assertRaises(UnsupportedFormatError, baker.write, 'xml')
Example #4
0
 def test_manifest_filename(self):
     baker = Baker(state='md')
     ts = datetime(2014, 2, 11, 10, 56, 15)
     filename = baker.manifest_filename(timestamp=ts, state='md')
     self.assertEqual(filename, 'md_20140211T105615_manifest.txt')
Example #5
0
 def test_filename(self):
     baker = Baker(state='md')
     ts = datetime(2014, 2, 11, 10, 56, 15)
     fmt = 'json'
     filename = baker.filename(fmt=fmt, timestamp=ts, state='md')
     self.assertEqual(filename, 'md_20140211T105615.json')