Skip to content

Commit

Permalink
pep8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Sonderegger committed Jan 17, 2019
1 parent a12d305 commit 039794f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
9 changes: 7 additions & 2 deletions fecfile/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
TYPE_CACHE_KEY = "%s:%s:%s"
TYPE_CACHE = {}


class FecParserMissingMappingError(Exception):
"""when a line in an FEC filing doesn't have a form/version mapping"""
def __init__(self, opts, msg=None):
Expand All @@ -18,10 +19,11 @@ def __init__(self, opts, msg=None):
)
super(FecParserMissingMappingError, self).__init__(msg)


def getMapping_from_regex(mappings, form, version):
""" Raises FecParserMissingMappingError if missing"""

for mapping in mappings.keys():
for mapping in mappings.keys():
if re.match(mapping, form, re.IGNORECASE):
versions = mappings[mapping].keys()
for v in versions:
Expand All @@ -33,16 +35,18 @@ def getMapping_from_regex(mappings, form, version):
'version': version,
})


def getMapping(mappings, form, version):
""" Tries to find the mapping from cache before looking it up w regex """
key = MAPPING_CACHE_KEY % (form,version)
key = MAPPING_CACHE_KEY % (form, version)
try:
mapping = MAPPING_CACHE[key]
except KeyError:
mapping = getMapping_from_regex(mappings, form, version)
MAPPING_CACHE[key] = mapping
return mapping


def getTypeMapping_from_regex(types, form, version, field):
""" Tries to find the mapping from cache before looking it up w regex """
for mapping in types.keys():
Expand All @@ -58,6 +62,7 @@ def getTypeMapping_from_regex(types, form, version, field):
return prop
return None


def getTypeMapping(types, form, version, field):
""" caches the mapping to dict """
key = TYPE_CACHE_KEY % (form, version, field)
Expand Down
7 changes: 5 additions & 2 deletions fecfile/fecparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import csv
import json
import os
import re
import warnings

from .cache import getTypeMapping, getMapping


class FecParserTypeWarning(UserWarning):
"""when data in an FEC filing doesn't match types.json"""
pass
Expand Down Expand Up @@ -118,6 +118,7 @@ def parse_header(lines):
parsed = parse_line(lines[0], fields[1], 0)
return parsed, fields[1], 1


def parse_line(line, version, line_num=None):
ascii_separator = True
if version is None or version[0] in comma_versions:
Expand All @@ -126,7 +127,6 @@ def parse_line(line, version, line_num=None):
if len(fields) < 2:
return None
form = fields[0]

this_version_mapping = getMapping(mappings, form, version)
out = {}
for i in range(len(this_version_mapping)):
Expand All @@ -135,8 +135,10 @@ def parse_line(line, version, line_num=None):
out[k] = getTyped(form, version, k, val, line_num)
return out


nones = ['none', 'n/a']


def getTyped(form, version, field, value, line_num):
prop = getTypeMapping(types, form, version, field)
if prop:
Expand Down Expand Up @@ -175,6 +177,7 @@ def getTyped(form, version, field, value, line_num):
return None
return value


def print_example(parsed):
out = {'filing': parsed['filing'], 'itemizations': {}}
for k in parsed['itemizations'].keys():
Expand Down
9 changes: 3 additions & 6 deletions speedtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,19 @@ def speed_test(filepath):
with open(filepath) as file:
linecount = 0
version = None
header = None
for line in file:
linecount+=1
linecount += 1
if version is None:
results = fecfile.parse_header(line)
header = results[0]
version = results[1]
else:
parsed = fecfile.parse_line(line, version)

if not parsed:
print("** not parsed %s" % line)
else:
else:
# count the form type, if given
try:
formtypecount.update({parsed['form_type'].upper():1})
formtypecount.update({parsed['form_type'].upper(): 1})
except KeyError:
continue

Expand Down

0 comments on commit 039794f

Please sign in to comment.