diff --git a/README.rst b/README.rst index ad98b2f..5cf5fd4 100644 --- a/README.rst +++ b/README.rst @@ -74,6 +74,7 @@ For each dataset, the first year listed is the default. * acs3dp: `ACS 3 Year Estimates, Data Profiles `_ (2013, 2012) * acs1dp: `ACS 1 Year Estimates, Data Profiles `_ (2023, 2022, 2021, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011) * acs5st: `ACS 5 Year Estimates, Subject Tables `_ (2023, 2022, 2021, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009) +* dhc: `Demographic and Housing Characteristics File `_ (2020) * sf1: `Census Summary File 1 `_ (2010) * pl: `Redistricting Data Summary File `_ (2020, 2010, 2000) diff --git a/census/core.py b/census/core.py index 1c80b96..c2f20a3 100644 --- a/census/core.py +++ b/census/core.py @@ -442,6 +442,86 @@ class ACS1DpClient(ACS1Client): years = (2023, 2022, 2021, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012) +class DHCClient(Client): + + default_year = 2020 + dataset = 'dhc' + + years = (2020) + + def _switch_endpoints(self, year): + + self.endpoint_url = 'https://api.census.gov/data/%s/dec/%s' + self.definitions_url = 'https://api.census.gov/data/%s/dec/%s/variables.json' + self.definition_url = 'https://api.census.gov/data/%s/dec/%s/variables/%s.json' + self.groups_url = 'https://api.census.gov/data/%s/dec/%s/groups.json' + + def tables(self, *args, **kwargs): + self._switch_endpoints(kwargs.get('year', self.default_year)) + return super(DHCClient, self).tables(*args, **kwargs) + + def get(self, *args, **kwargs): + self._switch_endpoints(kwargs.get('year', self.default_year)) + + return super(DHCClient, self).get(*args, **kwargs) + + @supported_years() + def state_county_subdivision(self, fields, state_fips, + county_fips, subdiv_fips, **kwargs): + return self.get(fields, geo={ + 'for': 'county subdivision:{}'.format(subdiv_fips), + 'in': 'state:{} county:{}'.format(state_fips, county_fips), + }, **kwargs) + + @supported_years() + def state_county_tract(self, fields, state_fips, + county_fips, tract, **kwargs): + return self.get(fields, geo={ + 'for': 'tract:{}'.format(tract), + 'in': 'state:{} county:{}'.format(state_fips, county_fips), + }, **kwargs) + + @supported_years() + def state_county_blockgroup(self, fields, state_fips, county_fips, + blockgroup, tract=None, **kwargs): + geo = { + 'for': 'block group:{}'.format(blockgroup), + 'in': 'state:{} county:{}'.format(state_fips, county_fips), + } + if tract: + geo['in'] += ' tract:{}'.format(tract) + return self.get(fields, geo=geo, **kwargs) + + @supported_years(2020) + def state_msa(self, fields, state_fips, msa, **kwargs): + return self.get(fields, geo={ + 'for': ('metropolitan statistical area/' + + 'micropolitan statistical area (or part):{}'.format(msa)), + 'in': 'state:{}'.format(state_fips), + }, **kwargs) + + @supported_years(2020) + def state_csa(self, fields, state_fips, csa, **kwargs): + return self.get(fields, geo={ + 'for': 'combined statistical area (or part):{}'.format(csa), + 'in': 'state:{}'.format(state_fips), + }, **kwargs) + + @supported_years(2020) + def state_district_place(self, fields, state_fips, + district, place, **kwargs): + return self.get(fields, geo={ + 'for': 'place/remainder (or part):{}'.format(place), + 'in': 'state:{} congressional district:{}'.format( + state_fips, district), + }, **kwargs) + + @supported_years(2020) + def state_zipcode(self, fields, state_fips, zcta, **kwargs): + return self.get(fields, geo={ + 'for': 'zip code tabulation area (or part):{}'.format(zcta), + 'in': 'state:{}'.format(state_fips), + }, **kwargs) class SF1Client(Client): @@ -599,6 +679,7 @@ def __init__(self, key, year=None, session=None): self.acs5dp = ACS5DpClient(key, year, session) self.acs3dp = ACS3DpClient(key, year, session) self.acs1dp = ACS1DpClient(key, year, session) + self.dhc = DHCClient(key, year, session) self.sf1 = SF1Client(key, year, session) self.pl = PLClient(key, year, session)