Skip to content

Commit

Permalink
cast fields to write type, where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
fgregg committed Jul 16, 2017
1 parent 55220ac commit 179c9d5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
31 changes: 29 additions & 2 deletions census/core.py
@@ -1,6 +1,11 @@
import warnings
from functools import wraps

try:
from functools import lru_cache
except ImportError:
from backports.functools_lru_cache import lru_cache

__version__ = "0.8.3"

ALL = '*'
Expand Down Expand Up @@ -62,6 +67,7 @@ class UnsupportedYearException(CensusException):
class Client(object):
endpoint_url = 'https://api.census.gov/data/%s/%s'
definitions_url = 'https://api.census.gov/data/%s/%s/variables.json'
definition_url = 'https://api.census.gov/data/%s/%s/variables/%s.json'

def __init__(self, key, year=None, session=None):
self._key = key
Expand Down Expand Up @@ -135,15 +141,36 @@ def query(self, fields, geo, year=None, **kwargs):
else:
raise ex

headers = data[0]
return [dict(zip(headers, d)) for d in data[1:]]
headers = data.pop(0)
types = [self._field_type(header, year) for header in headers]
results = [{header : cast(item)
for header, cast, item
in zip(headers, types, d)}
for d in data]
return results

elif resp.status_code == 204:
return []

else:
raise CensusException(resp.text)

@lru_cache(maxsize=1024)
def _field_type(self, field, year):
url = self.definition_url % (year, self.dataset, field)
resp = self.session.get(url)

types = {"fips-for" : str,
"fips-in" : str,
"int" : int,
"string": str}

if resp.status_code == 200:
predicate_type = resp.json().get("predicateType", "string")
return types[predicate_type]
else:
return str

@supported_years()
def us(self, fields, **kwargs):
return self.get(fields, geo={'for': 'us:1'}, **kwargs)
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -25,5 +25,6 @@
"Operating System :: OS Independent",
"Programming Language :: Python",
],
install_requires=['requests>=1.1.0'],
install_requires=['requests>=1.1.0',
'backports.functools_lru_cache;python_version<"3.3"'],
)

0 comments on commit 179c9d5

Please sign in to comment.