Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_Census_initialized_without_api_key(self):
census = Census()
self.assertEquals(census.api_key, 'my_fake_api_key')
def test_call_api_without_output_formatting(self):
Census().call_api('test', output_format=None)
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'test?api_key=my_fake_api_key')
self.assertEquals(url, expected_url)
def test_population_method_with_keypat_arg(self):
Census().population('TX')
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'population?api_key=my_fake_api_key&keypat=TX')
self.assertEquals(url, expected_url)
def test_empty_housing_method_url(self):
Census().population()
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'population?api_key=my_fake_api_key')
self.assertEquals(url, expected_url)
def test_empty_call_api_method_fails(self):
self.assertRaises(TypeError, Census().call_api)
def test_housing_method_with_keypat_arg(self):
Census().housing('TX')
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'housing?api_key=my_fake_api_key&keypat=TX')
self.assertEquals(url, expected_url)
def test_call_api_method_with_new_api_key(self):
Census('new_api_key').call_api('testing', hello='world')
url = called_url()
expected_url = ('http://api.usatoday.com/open/census/'
'testing?api_key=new_api_key&hello=world')
self.assertEquals(url, expected_url)
#
# * What's the purpose of an API key?
# * What is `pip` and how to use it?
# * Remember the issues of sometimes having to filter to Puerto Rico
#
# set up your census object
# example from https://github.com/sunlightlabs/census
from census import Census
from us import states
import settings
c = Census(settings.CENSUS_KEY)
for (i, state) in enumerate(states.STATES):
print i, state.name, state.fips
#
# [Formulating-URL-requests-to-the-API-explicitly](http://nbviewer.ipython.org/github/rdhyee/working-open-data-2014/blob/master/notebooks/Day_02_A_US_Census_API.ipynb#Formulating-URL-requests-to-the-API-explicitly)
#
import requests
# get the total population of all states
url = "http://api.census.gov/data/2010/sf1?key={key}&get=P0010001,NAME&for=state:*".format(key=settings.CENSUS_KEY)
r = requests.get(url)
r.json()[:5]
def __init__(self, key, state_abbr):
self.api = census.Census(key)
self.fips = getattr(states, state_abbr).fips
self.state_abbr = state_abbr
# [Day_06_D_Assignment.ipynb](http://nbviewer.ipython.org/github/rdhyee/working-open-data-2014/blob/master/notebooks/Day_06_D_Assignment.ipynb): exercise to write a generator for Census Places (answer: [Day_06_E_Assignment_Answers.ipynb](http://nbviewer.ipython.org/github/rdhyee/working-open-data-2014/blob/master/notebooks/Day_06_E_Assignment_Answers.ipynb))
#
# You should understand how this works.
import pandas as pd
from pandas import DataFrame
import census
import settings
import us
from itertools import islice
c=census.Census(settings.CENSUS_KEY)
def places(variables="NAME"):
for state in us.states.STATES:
print state
geo = {'for':'place:*', 'in':'state:{s_fips}'.format(s_fips=state.fips)}
for place in c.sf1.get(variables, geo=geo):
yield place
r = list(islice(places("NAME,P0010001"), None))
places_df = DataFrame(r)
places_df.P0010001 = places_df.P0010001.astype('int')
places_df['FIPS'] = places_df.apply(lambda s: s['state']+s['place'], axis=1)
print "number of places", len(places_df)