/
usa_today.py
84 lines (62 loc) · 2.59 KB
/
usa_today.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python
"""
Python wrapper for USA Today's Census API.
USA Today Documentation: http://developer.usatoday.com/docs/read/Census
"""
from api import API
from usa_today_api_key import API_KEY
class Census(API):
"""Wrapper for USA Today's Census API."""
def __init__(self, api_key=''):
super(Census, self).__init__(api_key)
self.base_url = 'http://api.usatoday.com/open/census'
if not self.api_key:
self.api_key = API_KEY
self.required_params = {'api_key': self.api_key}
def _resolve_url(self, directory, keypat=None, **kwargs):
"""Internal method to resolve URL structure."""
if keypat:
kwargs.update({'keypat': keypat})
return self.call_api(directory, **kwargs)
def locations(self, keypat=None, **kwargs):
"""
Returns all available ethnicity, housing, population and race
information for specified area.
>>> Census().locations()
"""
self._resolve_url('locations', keypat, **kwargs)
def ethnicity(self, keypat=None, **kwargs):
"""
Returns an area's ethnic data. Information includes how much of the
population identifies as Hispanic or non-Hispanic white, and the USA
TODAY Diversity Index.
>>> Census().ethnicity('CA')
>>> Census().ethnicity('CA', sumlevid=6)
"""
self._resolve_url('ethnicity', keypat, **kwargs)
def housing(self, keypat=None, **kwargs):
"""
Returns an area's housing data. Information includes the number of
housing units, and the percentage of those that are vacant.
>>> Census().housing('TX')
>>> Census().housing('TX', sumlevid=3)
"""
self._resolve_url('housing', keypat, **kwargs)
def population(self, keypat=None, **kwargs):
"""
Returns an area's population data. Information includes the total
population of an area, average population per square mile, and the
percent by which that population has changed since the last census.
>>> Census().population()
>>> Census().population('RI')
"""
self._resolve_url('population', keypat, **kwargs)
def race(self, keypat=None, **kwargs):
"""
Returns an area's racial data. Information includes the percentage
of an area's population that identifies as White, Black, American
Indian, Asian, native Hawaiian/Pacific Islander, or mixed race.
>>> Census().race()
>>> Census().race('NY', sumlevid=3)
"""
self._resolve_url('race', keypat, **kwargs)