From 6819ebe1e53b23bb7a71ad94dda1ac705fd8b1c2 Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 16 Dec 2024 21:50:09 -0500 Subject: [PATCH 1/3] added acs1st class and variable getting and match --- census/core.py | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/census/core.py b/census/core.py index 5beeb4e..cdde590 100644 --- a/census/core.py +++ b/census/core.py @@ -116,6 +116,39 @@ def tables(self, year=None): # Pass it out return resp.json()['groups'] + + def variables(self, year=None): # added + """ + Returns a list of the data variables available from this source. + """ + # Set the default year if one hasn't been passed + if year is None: + year = self.default_year + + # Query the table metadata as raw JSON + tables_url = self.definitions_url % (year, 'acs/'+self.dataset) + resp = self.session.get(tables_url) + + # Pass it out + return resp.json()['variables'] + + def find_variables(self, label: str, group: str, year:str=None) -> list[[str, str]]: + """ + Returns a list of partial matches of sub variables + """ + + variables = self.variables(year) + keys = list( + filter( + lambda row: variables[row]['group']=='S1811' and 'Below' in variables[row]['label'], + variables + ) + ) + + matches = [[key, variables[key]['label']] for key in keys] + + return matches + @supported_years() def fields(self, year=None, flat=False): @@ -123,7 +156,6 @@ def fields(self, year=None, flat=False): year = self.default_year data = {} - fields_url = self.definitions_url % (year, self.dataset) resp = self.session.get(fields_url) @@ -398,6 +430,16 @@ def _switch_endpoints(self, year): dataset = 'acs5/subject' +class ACS1StClient(ACSClient): # added + default_year = 2019 + def _switch_endpoints(self, year): + self.endpoint_url = 'https://api.census.gov/data/%s/acs/%s' + self.definitions_url = 'https://api.census.gov/data/%s/acs/%s/variables.json' + self.definition_url = 'https://api.census.gov/data/%s/acs/%s/variables/%s.json' + self.groups_url = 'https://api.census.gov/data/%s/acs/%s/groups.json' + + dataset = 'acs1/subject' + class ACS3Client(ACSClient): @@ -595,7 +637,8 @@ def __init__(self, key, year=None, session=None): self.acs5 = ACS5Client(key, year, session) self.acs3 = ACS3Client(key, year, session) self.acs1 = ACS1Client(key, year, session) - self.acs5st = ACS5StClient(key, year, session) + self.acs1st = ACS1StClient(key, year, session) + self.acs5st = ACS5StClient(key, year, session) # added self.acs5dp = ACS5DpClient(key, year, session) self.acs3dp = ACS3DpClient(key, year, session) self.acs1dp = ACS1DpClient(key, year, session) From d6f0f4576828ff1a2a9c397d008bac42d8a0e11b Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 16 Dec 2024 21:57:54 -0500 Subject: [PATCH 2/3] fixed typo --- census/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/census/core.py b/census/core.py index cdde590..95c1afb 100644 --- a/census/core.py +++ b/census/core.py @@ -140,7 +140,7 @@ def find_variables(self, label: str, group: str, year:str=None) -> list[[str, st variables = self.variables(year) keys = list( filter( - lambda row: variables[row]['group']=='S1811' and 'Below' in variables[row]['label'], + lambda row: variables[row]['group']==group and label in variables[row]['label'], variables ) ) From d7de5adee96007f5a67129c87c526919424da5ea Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 16 Dec 2024 21:59:02 -0500 Subject: [PATCH 3/3] cleaned up --- census/core.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/census/core.py b/census/core.py index 95c1afb..7dfe4b1 100644 --- a/census/core.py +++ b/census/core.py @@ -117,7 +117,7 @@ def tables(self, year=None): # Pass it out return resp.json()['groups'] - def variables(self, year=None): # added + def variables(self, year=None) -> list: # added """ Returns a list of the data variables available from this source. """ @@ -132,7 +132,7 @@ def variables(self, year=None): # added # Pass it out return resp.json()['variables'] - def find_variables(self, label: str, group: str, year:str=None) -> list[[str, str]]: + def find_variables(self, label: str, group: str, year:str=None) -> list: """ Returns a list of partial matches of sub variables """