-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added acs1st and variable functions #152
Open
acnromanmtz
wants to merge
3
commits into
datamade:master
Choose a base branch
from
acnromanmtz:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,14 +116,46 @@ def tables(self, year=None): | |
|
||
# Pass it out | ||
return resp.json()['groups'] | ||
|
||
def variables(self, year=None) -> list: # 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: | ||
""" | ||
Returns a list of partial matches of sub variables | ||
""" | ||
|
||
variables = self.variables(year) | ||
keys = list( | ||
filter( | ||
lambda row: variables[row]['group']==group and label 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): | ||
if year is None: | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add the supported years to this class? For example: class ACS1DpClient(ACS1Client):
dataset = 'acs1/profile'
years = (2022, 2021, 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012)
... |
||
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) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea here. We do plan on taking on a more general project of fetching API metadata from the
Census Data API Discovery Tool. Would you mind separating this out into another PR?