Skip to content

Commit 26fa490

Browse files
authored
Merge pull request #15 from QuantGov/add-series
Version 0.2.1
2 parents d995c07 + 9461329 commit 26fa490

File tree

3 files changed

+89
-69
lines changed

3 files changed

+89
-69
lines changed

regcensus/api.py

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def get_values(series, jurisdiction, date, filtered=True, summary=True,
102102
# and function returns empty.
103103
else:
104104
print("Valid date is required.")
105-
pp.pprint(get_periods(jurisdiction))
105+
pp.pprint(get_periods(jurisdiction, documentType=3))
106106
return
107107

108108
if dateIsRange:
@@ -151,82 +151,88 @@ def get_values(series, jurisdiction, date, filtered=True, summary=True,
151151
return clean_columns(output)
152152

153153

154-
def get_series(seriesID=''):
154+
def get_series(seriesID='', verbose=0):
155155
"""
156156
Get metadata for all or one specific series
157157
158158
Args: seriesID (optional): ID for the series
159159
160160
Returns: pandas dataframe with the metadata
161161
"""
162-
output = json_normalize(
163-
requests.get(URL + f'/series/{seriesID}').json())
164-
return clean_columns(output)
162+
url_call = URL + f'/series/{seriesID}'
163+
if verbose:
164+
print(f'API call: {url_call}')
165+
return clean_columns(json_normalize(requests.get(url_call).json()))
165166

166167

167-
def get_agencies(jurisdictionID):
168+
def get_agencies(jurisdictionID, verbose=0):
168169
"""
169170
Get metadata for all agencies of a specific jurisdiction
170171
171172
Args: jurisdictionID: ID for the jurisdiction
172173
173174
Returns: pandas dataframe with the metadata
174175
"""
175-
output = json_normalize(
176-
requests.get(
177-
URL + (f'/agencies/jurisdiction?'
178-
f'jurisdictions={jurisdictionID}')).json())
179-
return clean_columns(output)
176+
url_call = URL + (f'/agencies/jurisdiction?'
177+
f'jurisdictions={jurisdictionID}')
178+
if verbose:
179+
print(f'API call: {url_call}')
180+
return clean_columns(json_normalize(requests.get(url_call).json()))
180181

181182

182-
def get_jurisdictions(jurisdictionID=''):
183+
def get_jurisdictions(jurisdictionID='', verbose=0):
183184
"""
184185
Get metadata for all or one specific jurisdiction
185186
186187
Args: jurisdictionID (optional): ID for the jurisdiction
187188
188189
Returns: pandas dataframe with the metadata
189190
"""
190-
output = json_normalize(
191-
requests.get(URL + f'/jurisdictions/{jurisdictionID}').json())
192-
return clean_columns(output)
191+
url_call = URL + f'/jurisdictions/{jurisdictionID}'
192+
if verbose:
193+
print(f'API call: {url_call}')
194+
return clean_columns(json_normalize(requests.get(url_call).json()))
193195

194196

195-
def get_periods(jurisdictionID='', documentType=3):
197+
def get_periods(jurisdictionID, documentType, seriesID='', verbose=0):
196198
"""
197-
Get dates available for all or one specific jurisdiction
198-
and compatible series IDs
199+
Get dates available for all or one specific series for a jurisdiction
199200
200-
Args: jurisdictionID (optional): ID for the jurisdiction
201+
Args:
202+
jurisdictionID: ID for the jurisdiction
203+
documentType: document type (regulations, statutes, etc.),
204+
see list_document_types()
205+
seriesID (optional): ID for the series
201206
202207
Returns: pandas dataframe with the dates
203208
"""
204-
if jurisdictionID:
205-
output = json_normalize(
206-
requests.get(
207-
URL + (f'/periods?jurisdiction={jurisdictionID}&'
208-
f'documentType={documentType}')).json())
209+
if seriesID:
210+
url_call = (URL + (f'/periods?jurisdiction={jurisdictionID}&'
211+
f'series={seriesID}&'
212+
f'documentType={documentType}'))
209213
else:
210-
output = json_normalize(
211-
requests.get(URL + f'/periods/available').json())
212-
return clean_columns(output)
214+
url_call = (URL + (f'/periods?jurisdiction={jurisdictionID}&'
215+
f'documentType={documentType}'))
216+
if verbose:
217+
print(f'API call: {url_call}')
218+
return clean_columns(json_normalize(requests.get(url_call).json()))
213219

214220

215-
def get_industries(jurisdictionID):
221+
def get_industries(jurisdictionID, verbose=0):
216222
"""
217223
Get metadata for all industries available in a specific jurisdiction
218224
219225
Args: jurisdictionID: ID for the jurisdiction
220226
221227
Returns: pandas dataframe with the metadata
222228
"""
223-
output = json_normalize(
224-
requests.get(
225-
URL + f'/industries?jurisdiction={jurisdictionID}').json())
226-
return clean_columns(output)
229+
url_call = URL + f'/industries?jurisdiction={jurisdictionID}'
230+
if verbose:
231+
print(f'API call: {url_call}')
232+
return clean_columns(json_normalize(requests.get(url_call).json()))
227233

228234

229-
def get_documents(jurisdictionID, documentType=3):
235+
def get_documents(jurisdictionID, documentType=3, verbose=0):
230236
"""
231237
Get metadata for documents available in a specific jurisdiction, optional
232238
filtering by document type (see list_document_types() for options)
@@ -237,12 +243,11 @@ def get_documents(jurisdictionID, documentType=3):
237243
238244
Returns: pandas dataframe with the metadata
239245
"""
240-
output = json_normalize(
241-
requests.get(
242-
URL + (f'/documents?jurisdiction={jurisdictionID}&'
243-
f'documentType={documentType}')
244-
).json())
245-
return clean_columns(output)
246+
url_call = URL + (f'/documents?jurisdiction={jurisdictionID}&'
247+
f'documentType={documentType}')
248+
if verbose:
249+
print(f'API call: {url_call}')
250+
return clean_columns(json_normalize(requests.get(url_call).json()))
246251

247252

248253
def list_document_types():

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.2.0',
7+
version='0.2.1',
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: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,60 @@
44

55

66
# UTILITY FUNCTIONS
7-
def order_results(results, column):
8-
return list(results[column].sort_values().head(10).values)
7+
def order_results(results, column, descending=False):
8+
if descending:
9+
return list(reversed(results[column].sort_values().tail(10).values))
10+
else:
11+
return list(results[column].sort_values().head(10).values)
912

1013

1114
# TEST FUNCTIONS
1215
def test_get_series():
13-
results = rc.get_series()
16+
results = rc.get_series(verbose=1)
1417
assert order_results(results, 'seriesCode') == [
1518
'NY.GDP.MKTP.CD', 'NY.GDP.MKTP.KD', 'NY.GDP.MKTP.KD.ZG',
16-
'NY.GDP.PCAP.KD.ZG', 'RG_OCL1000002A', 'RG_OCLI1000001A',
17-
'RG_QLTY1000470Z', 'RG_QLTY1000471Z',
18-
'RG_QLTY1000472Z', 'RG_QLTY1000473Z'
19+
'NY.GDP.PCAP.KD.ZG', 'RG_DRST0000001A', 'RG_HLC1000002A',
20+
'RG_OCL1000002A', 'RG_OCL1000002B', 'RG_OCL1000002C', 'RG_OCLI1000001A'
1921
]
2022

2123

2224
def test_get_agencies():
23-
results = rc.get_agencies(38)
25+
results = rc.get_agencies(38, verbose=1)
2426
assert order_results(results, 'agencyID') == [
25-
64, 65, 66, 67, 68, 69, 70, 71, 72, 73
27+
64, 65, 66, 67, 68, 70, 71, 72, 73, 74
2628
]
2729

2830

2931
def test_get_jurisdictions():
30-
results = rc.get_jurisdictions()
32+
results = rc.get_jurisdictions(verbose=1)
3133
assert order_results(results, 'jurisdictionID') == [
3234
2, 4, 10, 11, 14, 15, 17, 20, 23, 24
3335
]
3436

3537

3638
def test_get_periods():
37-
results = rc.get_periods()
38-
assert order_results(results, 'seresYearID') == [
39-
102994, 102995, 102996, 102997, 102998,
40-
102999, 103000, 103001, 103002, 103003
39+
results = rc.get_periods(38, documentType=3, verbose=1)
40+
assert order_results(results, 'recordsAvailable', descending=True) == [
41+
30696278, 30696278, 30696278, 30696278, 30696278,
42+
30696278, 30696278, 30696278, 30696278, 30696278
4143
]
4244

4345

46+
def test_get_periods_one_series():
47+
results = rc.get_periods(20, documentType=3, seriesID=19, verbose=1)
48+
assert order_results(results, 'recordsAvailable') == [13]
49+
50+
4451
def test_get_industries():
45-
results = rc.get_industries(jurisdictionID=38)
52+
results = rc.get_industries(jurisdictionID=38, verbose=1)
4653
assert order_results(results, 'industryCode') == [
4754
'0', '11', '111', '1111', '11111', '111110',
4855
'11112', '111120', '11113', '111130'
4956
]
5057

5158

5259
def test_get_documents():
53-
results = rc.get_documents(jurisdictionID=44)
60+
results = rc.get_documents(jurisdictionID=44, verbose=1)
5461
assert order_results(results, 'documentID') == [
5562
4441363, 4441364, 4441365, 4441366, 4441367,
5663
4441368, 4441369, 4441370, 4441371, 4441372
@@ -82,10 +89,10 @@ def test_get_values_all_industries():
8289
series=9, jurisdiction=58, date=2019, industry='all', filtered=False
8390
)
8491
assert order_results(results, 'seriesValue') == [
85-
16.487800191811402, 28.080800290597836, 36.27130037093593,
86-
36.53810011051246, 40.113500030507566, 45.02970027324045,
87-
48.842899827621295, 50.17920009633963, 72.05880061667267,
88-
82.19629916545819
92+
16.487800191811402, 28.080800290597836, 32.408500283963804,
93+
36.27130037093593, 36.53810011051246, 36.72170047096006,
94+
40.113500030507566, 44.19190023323608, 45.02970027324045,
95+
48.842899827621295
8996
]
9097

9198

@@ -100,11 +107,14 @@ def test_get_values_multiple_industries():
100107

101108
def test_get_values_one_industry():
102109
results = rc.get_values(
103-
series=9, jurisdiction=58, date='2019-05-15',
110+
series=9, jurisdiction=20, date='2020-06-02',
104111
industry='111', summary=False
105112
)
106-
assert order_results(results, 'seriesValue') == [
107-
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
113+
assert order_results(results, 'seriesValue', descending=True) == [
114+
208.5117055773735, 177.00449323654175, 86.01110327243805,
115+
76.680001989007, 66.70080256462097, 61.46759942173958,
116+
58.94939732551575, 41.14539943635464, 36.53759956359863,
117+
33.51419833302498
108118
]
109119

110120

@@ -140,7 +150,7 @@ def test_get_values_incorrect_dates(capsys):
140150
def test_get_values_country():
141151
results = rc.get_values(series=1, jurisdiction=38, date=2019, country=True)
142152
assert order_results(results, 'seriesValue') == [
143-
43940.0, 52569.0, 60086.0, 63203.0, 63735.0,
153+
43940.0, 51925.0, 52569.0, 60086.0, 63735.0,
144154
70969.0, 78676.0, 92522.0, 104562.0, 107063.0
145155
]
146156

@@ -187,21 +197,26 @@ def test_get_values_error(capsys):
187197
results = rc.get_values(series=1, jurisdiction=38, date=1900)
188198
assert not results
189199
assert capsys.readouterr().out == (
190-
'WARNING: SeriesValue was not found for the specified parameters'
191-
'{parameters={jurisdiction=[38], date=[1900], industry=null, '
192-
'agency=null, dateIsRange=false, filteredOnly=true, summary=true, '
193-
'documentType=3, documentID=null}}\n'
200+
'WARNING: SeriesValue was not found for the specified parameters. '
201+
'Please check that you have selected the right combination of '
202+
'parameters. When in doubt, please use the /periods endpoint to '
203+
'find out the combinations of series, jurisdiction, periods, '
204+
'agencies, document types for which there are data available.'
205+
'{parameters={jurisdiction=[US_UNITED_STATES], date=[1900], '
206+
'industry=null, agency=null, dateIsRange=false, filteredOnly=true, '
207+
'summary=true, series=[SERIES_1], documentType=ALL_REGULATIONS, '
208+
'documentID=null}}\n'
194209
)
195210

196211

197212
def test_list_document_types():
198213
results = rc.list_document_types()
199-
assert results['All Regulations'] == 3
214+
assert results['Regulation text: All regulations'] == 3
200215

201216

202217
def test_list_series():
203218
results = rc.list_series()
204-
assert results['Conditionals'] == 135
219+
assert results['Complexity: Conditionals analysis'] == 135
205220

206221

207222
def test_list_agencies():

0 commit comments

Comments
 (0)