Skip to content

Commit e105296

Browse files
authored
Handle statvar api no data. (#164)
1 parent 6162019 commit e105296

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

datacommons/examples/stat_vars.py

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ def call_str(pvs):
126126
dc.get_stat_all(["badPlaceId", "country/FRA"],
127127
["Median_Age_Person", "Count_Person"]))
128128

129+
130+
print('\nWhen no data for get_stat_value')
131+
pp.pprint(dc.get_stat_value('foooo', 'barrrr'))
132+
133+
print('\nWhen no data for get_stat_series')
134+
pp.pprint(dc.get_stat_series('foobarbar', 'barfoo'))
135+
129136
print('\nSTRESS TEST FOR GET_STAT_ALL')
130137
try:
131138
dc.get_stat_all(

datacommons/stat_vars.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_stat_value(place,
5151
scaling_factor (`int`): Optional, the preferred `scalingFactor` value.
5252
Returns:
5353
A `float` the value of `stat_var` for `place`, filtered
54-
by optional args.
54+
by optional args. If no data, returns nan.
5555
5656
Raises:
5757
ValueError: If the payload returned by the Data Commons REST API is
@@ -77,7 +77,9 @@ def get_stat_value(place,
7777
try:
7878
res_json = utils._send_request(url, post=False, use_payload=False)
7979
except ValueError:
80-
raise ValueError('No data in response.')
80+
return float('nan')
81+
if 'value' not in res_json:
82+
return float('nan')
8183
return res_json['value']
8284

8385

@@ -120,11 +122,14 @@ def get_stat_series(place,
120122
url += '&unit={}'.format(unit)
121123
if scaling_factor:
122124
url += '&scaling_factor={}'.format(scaling_factor)
123-
124-
res_json = utils._send_request(url, post=False, use_payload=False)
125+
126+
try:
127+
res_json = utils._send_request(url, post=False, use_payload=False)
128+
except ValueError:
129+
return {}
125130

126131
if 'series' not in res_json:
127-
raise ValueError('No data in response.')
132+
return {}
128133
return res_json['series']
129134

130135

datacommons/test/stat_vars_test.py

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import datacommons as dc
2929
import datacommons.utils as utils
30+
import math
3031
import json
3132
import unittest
3233
import six
@@ -273,6 +274,9 @@ def test_opt_args(self, urlopen):
273274
'CensusPEPSurvey', 'P1Y', 'RealPeople', 100)
274275
self.assertEqual(stat, 103)
275276

277+
# Call get_stat_series with bogus required args
278+
stat = dc.get_stat_value('foofoo', 'barrbar')
279+
self.assertTrue(math.isnan(stat))
276280

277281
class TestGetStatSeries(unittest.TestCase):
278282
"""Unit tests for get_stat_series."""
@@ -293,6 +297,10 @@ def test_opt_args(self, urlopen):
293297
'CensusPEPSurvey', 'P1Y', 'RealPeople', 100)
294298
self.assertEqual(stats, {"2000": 3, "2001": 42})
295299

300+
# Call get_stat_series with bogus required args
301+
stats = dc.get_stat_series('foofoofoo', 'barfoobar')
302+
self.assertEqual(stats, {})
303+
296304
# Call get_stat_series with non-satisfiable optional args
297305
stats = dc.get_stat_series('geoId/06', 'Count_Person', 'DNE')
298306
self.assertEqual(stats, {})

0 commit comments

Comments
 (0)