Skip to content

Commit 630fbe1

Browse files
authored
Release 0.4.0 (#21)
* adjustments for v3 of the API * live url * v0.4.0
1 parent fb739cd commit 630fbe1

File tree

4 files changed

+65
-36
lines changed

4 files changed

+65
-36
lines changed

regcensus/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'get_document_values',
44
'get_reading_time',
55
'get_series',
6+
'get_periods',
67
'get_agencies',
78
'get_jurisdictions',
89
'get_industries',
@@ -21,6 +22,7 @@
2122
get_document_values,
2223
get_reading_time,
2324
get_series,
25+
get_periods,
2426
get_agencies,
2527
get_jurisdictions,
2628
get_industries,

regcensus/api.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def get_values(series, jurisdiction, date, documentType=None, summary=True,
109109
# and function returns empty.
110110
else:
111111
print("Valid date is required. Select from the following list:")
112-
dates = sorted(get_series(jurisdiction)['periodCode'].unique())
112+
dates = list_dates(jurisdiction)
113113
pp.pprint(dates)
114114
return
115115

@@ -182,15 +182,12 @@ def get_reading_time(*args, **kwargs):
182182
results['footNote'] = (
183183
'Reading time calculation assumes an 8 hour work-day, '
184184
'a 5 day work-week, and a 50 week work-year.')
185-
return results[[
186-
'seriesValue', 'seriesName', 'jurisdictionName', 'periodCode',
187-
'sourceCitation', 'sourceName', 'sourceOrganization', 'sourceUrl',
188-
'documentationUrl', 'footNote']]
185+
return results
189186

190187

191188
def get_series(jurisdictionID=None, documentType=None, verbose=0):
192189
"""
193-
Get series and date metadata for all or one specific jurisdiction
190+
Get series metadata for all or one specific jurisdiction
194191
195192
Args: jurisdictionID (optional): ID for the jurisdiction
196193
@@ -202,6 +199,20 @@ def get_series(jurisdictionID=None, documentType=None, verbose=0):
202199
return clean_columns(json_normalize(requests.get(url_call).json()))
203200

204201

202+
def get_periods(jurisdictionID=None, documentType=None, verbose=0):
203+
"""
204+
Get date metadata for all or one specific jurisdiction
205+
206+
Args: jurisdictionID (optional): ID for the jurisdiction
207+
208+
Returns: pandas dataframe with the metadata
209+
"""
210+
url_call = periods_url(jurisdictionID, documentType)
211+
if verbose:
212+
print(f'API call: {url_call}')
213+
return clean_columns(json_normalize(requests.get(url_call).json()))
214+
215+
205216
def get_agencies(jurisdictionID=None, keyword=None, verbose=0):
206217
"""
207218
Get metadata for all agencies of a specific jurisdiction
@@ -310,16 +321,16 @@ def list_document_types(jurisdictionID=None):
310321
for d in json if d["subtypeName"]}.items()))
311322

312323

313-
def list_series(jurisdictionID=None):
324+
def list_series(jurisdictionID=None, documentType=None):
314325
"""
315326
Args: jurisdictionID (optional): ID for the jurisdiction
316327
317328
Returns: dictionary containing names of series and associated IDs
318329
"""
319-
url_call = series_url(jurisdictionID)
330+
url_call = series_url(jurisdictionID, documentType)
320331
json = requests.get(url_call).json()
321332
return dict(sorted({
322-
s["series"]["seriesName"]: s["series"]["seriesID"]
333+
s["seriesName"]: s["seriesID"]
323334
for s in json}.items()))
324335

325336

@@ -329,7 +340,7 @@ def list_dates(jurisdictionID):
329340
330341
Returns: list of dates available for the jurisdiction
331342
"""
332-
return sorted(get_series(jurisdictionID)['periodCode'].unique())
343+
return sorted(get_periods(jurisdictionID)['periodCode'].unique())
333344

334345

335346
def list_agencies(jurisdictionID=None, keyword=None):
@@ -393,7 +404,21 @@ def list_industries(keyword=None, codeLevel=3, standard='NAICS', onlyID=False):
393404

394405
def series_url(jurisdictionID, documentType=None):
395406
"""Gets url call for series endpoint."""
396-
url_call = URL + '/series'
407+
url_call = URL + '/dataseries'
408+
if jurisdictionID and documentType:
409+
url_call += (
410+
f'?jurisdiction={jurisdictionID}&'
411+
f'documentType={documentType}')
412+
elif jurisdictionID:
413+
url_call += f'?jurisdiction={jurisdictionID}'
414+
elif documentType:
415+
url_call += f'?documentType={documentType}'
416+
return url_call
417+
418+
419+
def periods_url(jurisdictionID, documentType=None):
420+
"""Gets url call for series endpoint."""
421+
url_call = URL + '/seriesperiod'
397422
if jurisdictionID and documentType:
398423
url_call += (
399424
f'?jurisdiction={jurisdictionID}&'

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='regcensus',
7-
version='0.3.1',
7+
version='0.4.0',
88
description='Python package for accessing data from the QuantGov API',
99
url='https://github.com/QuantGov/regcensus-api-python',
1010
author='QuantGov',

tests/test_api.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,22 @@ def order_results(results, column, descending=False):
1515
# Tests for get_() functions
1616
def test_get_series():
1717
results = rc.get_series(verbose=1)
18-
assert order_results(results, 'seriesID') == [1] * 10
18+
assert order_results(results, 'seriesID') == [
19+
1, 2, 3, 4, 5, 6, 7, 9, 10, 11
20+
]
1921

2022

2123
def test_get_agencies():
2224
results = rc.get_agencies(jurisdictionID=38, verbose=1)
2325
assert order_results(results, 'agencyID') == [
24-
0, 1, 64, 65, 66, 67, 68, 69, 70, 71
26+
0, 3, 5, 6, 7, 8, 9, 11, 12, 13
2527
]
2628

2729

2830
def test_get_agencies_keyword():
2931
results = rc.get_agencies(keyword='Education', verbose=1)
3032
assert order_results(results, 'agencyID') == [
31-
225, 348, 375, 403, 475, 521, 539, 572, 595, 603
33+
52, 216, 225, 226, 238, 267, 285, 296, 356, 358
3234
]
3335

3436

@@ -204,24 +206,25 @@ def test_get_values_country():
204206

205207

206208
def test_get_values_agency():
207-
results = rc.get_values(series=13, jurisdiction=38, date=2019, agency=195)
208-
assert order_results(results, 'seriesValue') == [62.0]
209+
results = rc.get_values(series=13, jurisdiction=66, date=2021, agency=3112)
210+
assert order_results(results, 'seriesValue') == [40141.0]
209211

210212

211213
def test_get_values_all_agencies():
212214
results = rc.get_values(
213-
series=13, jurisdiction=38, date=2019
215+
series=13, jurisdiction=66, date=2021
214216
)
215217
assert order_results(results, 'seriesValue') == [
216-
0.0, 0.0, 1.0, 1.0, 5.0, 18.0, 33.0, 34.0, 50.0, 59.0
218+
555.0, 2025.0, 2035.0, 3085.0, 3849.0,
219+
4771.0, 5235.0, 5399.0, 6119.0, 6783.0
217220
]
218221

219222

220223
def test_get_values_multiple_agencies():
221224
results = rc.get_values(
222-
series=13, jurisdiction=38, date=2019, agency=[111, 99]
225+
series=13, jurisdiction=66, date=2021, agency=[3112, 3113]
223226
)
224-
assert order_results(results, 'seriesValue') == [34167.0, 91087.0]
227+
assert order_results(results, 'seriesValue') == [17304.0, 40141.0]
225228

226229

227230
def test_get_values_version():
@@ -233,7 +236,7 @@ def test_get_values_version():
233236

234237
def test_get_values_download():
235238
results = rc.get_values(
236-
series=13, jurisdiction=38, date=2019, agency=195, download='test.csv'
239+
series=13, jurisdiction=66, date=2021, agency=3112, download='test.csv'
237240
)
238241
assert not results
239242
assert os.path.exists('test.csv')
@@ -242,27 +245,25 @@ def test_get_values_download():
242245

243246
def test_get_values_incorrect_download(capsys):
244247
results = rc.get_values(
245-
series=13, jurisdiction=38, date=2019, agency=195, download=True
248+
series=13, jurisdiction=66, date=2021, agency=3112, download=True
246249
)
247250
assert not results
248251
assert capsys.readouterr().out == 'Valid outpath required to download.\n'
249252

250253

251254
def test_get_values_error(capsys):
252-
results = rc.get_values(series=1, jurisdiction=38, date=1900, verbose=1)
255+
results = rc.get_values(series=1, jurisdiction=38, date=1900)
253256
assert not results
254257
assert capsys.readouterr().out == (
255-
'API call: https://api.quantgov.org/summary'
256-
'?series=1&jurisdiction=38&date=1900\n'
257258
'WARNING: SeriesValue was not found for the specified parameters. '
258259
'Please check that you have selected the right combination of '
259-
'parameters. When in doubt, please use the /periods endpoint to find '
260-
'out the combinations of series, jurisdiction, periods, agencies, '
261-
'document types for which there are data available.{parameters='
262-
'{jurisdiction=[US_UNITED_STATES], date=[1900], labelLevel=[3], '
263-
'agency=null, dateIsRange=false, filteredOnly=true, label=null, '
264-
'series=[SERIES_1], documentType=null, national=false}}\n'
265-
)
260+
'parameters. When in doubt, please use the /periods endpoint to '
261+
'find out the combinations of series, jurisdiction, periods, '
262+
'agencies, document types for which there are data available.'
263+
'{parameters={jurisdiction=[US_UNITED_STATES], date=[1900], '
264+
'labelLevel=[3], agency=null, dateIsRange=false, filteredOnly=false, '
265+
'label=null, series=[SERIES_1], documentType=null, '
266+
'national=false, cluster=null}}\n')
266267

267268

268269
# Tests for list_() functions
@@ -295,13 +296,14 @@ def test_list_dates():
295296

296297

297298
def test_list_agencies():
298-
results = rc.list_agencies(jurisdictionID=38)
299-
assert results['Administrative Conference of the United States'] == 195
299+
results = rc.list_agencies(jurisdictionID=66)
300+
assert results['department of health'] == 3112
300301

301302

302303
def test_list_agencies_keyword():
303304
results = rc.list_agencies(keyword='Education')
304-
assert results['California Department of Education (California)'] == 770
305+
assert results[
306+
'california educational facilities authority (California)'] == 2094
305307

306308

307309
def test_list_agencies_error(capsys):

0 commit comments

Comments
 (0)