Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 3babd2f

Browse files
committed
Update consistency between cmd and script, and for the wiki pages. Add additional checks for parameters
1 parent 50a7625 commit 3babd2f

File tree

6 files changed

+93
-52
lines changed

6 files changed

+93
-52
lines changed

firststreet/__main__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,47 @@
177177
extra_param=argument.extra_param)
178178

179179
elif argument.product == 'tile.get_probability_depth':
180+
if not argument.year:
181+
logging.error("get_probability_depth is missing the year argument")
182+
sys.exit()
183+
184+
try:
185+
int(argument.year)
186+
except ValueError:
187+
logging.error("The year argument could not be converted to an int. "
188+
"Provided argument: {}".format(argument.year))
189+
sys.exit()
190+
191+
if not argument.return_period:
192+
logging.error("get_probability_depth is missing the return_period argument")
193+
sys.exit()
194+
195+
try:
196+
int(argument.return_period)
197+
except ValueError:
198+
logging.error("The return_period argument could not be converted to an int. "
199+
"Provided argument: {}".format(argument.return_period))
200+
sys.exit()
201+
180202
fs.tile.get_probability_depth(year=int(argument.year),
181203
return_period=int(argument.return_period),
182204
search_items=search_items,
183205
output_dir=argument.output_dir,
184206
image=True)
185207

186208
elif argument.product == 'tile.get_historic_event':
209+
210+
if not argument.event_id:
211+
logging.error("get_probability_depth is missing the event_id argument")
212+
sys.exit()
213+
214+
try:
215+
int(argument.event_id)
216+
except ValueError:
217+
logging.error("The event_id argument could not be converted to an int. "
218+
"Provided argument: {}".format(argument.event_id))
219+
sys.exit()
220+
187221
fs.tile.get_historic_event(event_id=int(argument.event_id),
188222
search_items=search_items,
189223
output_dir=argument.output_dir,

firststreet/api/api.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,35 +43,42 @@ def call_api(self, search_item, product, product_subtype, location=None, tile_pr
4343
Returns:
4444
A list of JSON responses
4545
"""
46-
# Not a list. This means it's a file
46+
47+
# Not a list. This means it's should be a file
4748
if not isinstance(search_item, list):
4849

4950
# Check if it's a file
50-
if isinstance(search_item, str):
51-
if os.path.isfile(search_item):
51+
if isinstance(search_item, str) and os.path.isfile(search_item):
5252

53-
# Get search items from file
54-
search_item = read_search_items_from_file(search_item)
53+
# Get search items from file
54+
search_item = read_search_items_from_file(search_item)
55+
56+
else:
57+
raise InvalidArgument("File provided is not a list or a valid file. "
58+
"Please check the file name and path. '{}'".format(str(search_item)))
59+
60+
else:
61+
62+
# Check tile product
63+
if tile_product:
64+
if not all(isinstance(t, tuple) for t in search_item):
65+
raise TypeError("Input must be a list of coordinates in a tuple of (z, x, y). "
66+
"Provided Arg: {}".format(search_item))
67+
68+
if not all(isinstance(coord, int) for t in search_item for coord in t):
69+
raise TypeError("Each coordinate in the tuple must be an integer. Provided Arg: {}".format(search_item))
70+
71+
if not all(0 < t[0] <= 18 for t in search_item):
72+
raise TypeError("Max zoom is 18. Provided Arg: {}".format(search_item))
5573

56-
else:
57-
raise InvalidArgument("File provided is not a valid file. "
58-
"Please check the file name and path. '{}'".format(str(search_item)))
59-
60-
# # Check tuple
61-
# elif tile_product and not os.path.isfile(search_item):
62-
# if not all(isinstance(t, tuple) for t in search_item):
63-
# raise TypeError("Input must be a list of coordinates in a tuple of (z, x, y). "
64-
# "Provided Arg: {}".format(search_item))
65-
#
66-
# if not all(isinstance(coord, int) for t in search_item for coord in t):
67-
# raise TypeError("Each coordinate in the tuple must be an integer. Provided Arg: {}".format(search_item))
68-
#
69-
# if not all(0 < t[0] <= 18 for t in search_item):
70-
# raise TypeError("Max zoom is 18. Provided Arg: {}".format(search_item))
71-
#
7274
# else:
73-
# raise InvalidArgument("File provided is not a list or a valid file. "
74-
# "Please check the file name and path. '{}'".format(str(search_item)))
75+
76+
# Ensure for historic and adaptation the search items are EventIDs or AdaptationIDs
77+
if ((product == "adaptation" and product_subtype == "detail") or
78+
(product == "historic" and product_subtype == "event")) and \
79+
not all(isinstance(t, int) for t in search_item):
80+
raise TypeError("Input must be an integer for this product. "
81+
"Provided Arg: {}".format(search_item))
7582

7683
# No items found
7784
if not search_item:

firststreet/api/csv_format.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def format_adaptation_summary(data):
203203
df = pd.json_normalize([vars(o) for o in data]).explode('adaptation').reset_index(drop=True)
204204
df['fsid'] = df['fsid'].apply(str)
205205
df['adaptation'] = df['adaptation'].astype('Int64').apply(str)
206-
return df[['fsid', 'valid_id', 'adaptation', 'properties']]
206+
return df[['fsid', 'valid_id', 'adaptation']]
207207

208208

209209
def format_adaptation_summary_detail(data):
@@ -904,7 +904,7 @@ def format_location_detail_county(data):
904904
"""
905905
df = pd.DataFrame([vars(o) for o in data]).explode('city').explode('zcta') \
906906
.explode('cd').reset_index(drop=True)
907-
df.rename(columns={'fsid': 'fsid_placeholder'}, inplace=True)
907+
df.rename(columns={'fsid': 'fsid_placeholder', 'name': 'name_placeholder'}, inplace=True)
908908

909909
if not df['city'].isna().values.all():
910910
df = pd.concat([df.drop(['city'], axis=1), df['city'].apply(pd.Series)], axis=1)
@@ -938,15 +938,15 @@ def format_location_detail_county(data):
938938
df['state_fips'] = pd.NA
939939
df['state_name'] = pd.NA
940940

941-
df.rename(columns={'fsid_placeholder': 'fsid'}, inplace=True)
941+
df.rename(columns={'fsid_placeholder': 'fsid', 'name_placeholder': 'name'}, inplace=True)
942942
df['fsid'] = df['fsid'].apply(str)
943943
df['city_fips'] = df['city_fips'].astype('Int64').apply(str)
944944
df['zipCode'] = df['zipCode'].astype('Int64').apply(str)
945945
df['cd_fips'] = df['cd_fips'].astype('Int64').apply(str)
946946
df['state_fips'] = df['state_fips'].astype('Int64').apply(str).apply(lambda x: x.zfill(2))
947947
df['geometry'] = df['geometry'].apply(get_geom_center)
948948
df = pd.concat([df.drop(['geometry'], axis=1), df['geometry'].apply(pd.Series)], axis=1)
949-
return df[['fsid', 'valid_id', 'city_fips', 'city_name', 'zipCode', 'fips', 'isCoastal', 'cd_fips',
949+
return df[['fsid', 'valid_id', 'name', 'isCoastal', 'city_fips', 'city_name', 'zipCode', 'fips', 'cd_fips',
950950
'cd_name', 'state_fips', 'state_name', 'latitude', 'longitude']]
951951

952952

firststreet/http_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import ssl
1414
import certifi
1515
from asyncio_throttle import Throttler
16-
from itertools import islice
1716

1817
# Internal Imports
1918
import firststreet.errors as e
@@ -102,6 +101,7 @@ async def execute(self, endpoint, session, throttler):
102101
Raises:
103102
_network_error: if an error occurs
104103
"""
104+
105105
headers = self.options.get('headers')
106106

107107
# Retry loop

setup.py

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

1212
setup(
1313
name='fsf-api-access_python',
14-
version='2.1.2',
14+
version='2.2.0',
1515
description='A Python API Access Client for the First Street Foundation API',
1616
url='https://github.com/FirstStreet/fsf_api_access_python',
1717
project_urls={

tests/api/test_tiles.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,51 @@ class TestProbabilityTiles:
1919

2020
def test_empty(self):
2121
with pytest.raises(InvalidArgument):
22-
fs.tile.get_probability_depth(year=2050, return_period=5, coordinate=[])
22+
fs.tile.get_probability_depth(year=2050, return_period=5, search_items=[])
2323

2424
def test_wrong_coord_type(self):
2525
with pytest.raises(InvalidArgument):
26-
fs.tile.get_probability_depth(year=2050, return_period=5, coordinate=(12, 942, 1715))
26+
fs.tile.get_probability_depth(year=2050, return_period=5, search_items=(12, 942, 1715))
2727

2828
def test_wrong_coord_tuple_type(self):
2929
with pytest.raises(TypeError):
30-
fs.tile.get_probability_depth(year=2050, return_period=500, coordinate=[500])
30+
fs.tile.get_probability_depth(year=2050, return_period=500, search_items=[500])
3131

3232
def test_invalid(self):
3333
coord = [(1, 1, 1)]
34-
tile = fs.tile.get_probability_depth(year=2050, return_period=5, coordinate=coord)
34+
tile = fs.tile.get_probability_depth(year=2050, return_period=5, search_items=coord)
3535
assert len(tile) == 1
3636
assert tile[0].coordinate == coord[0]
3737
assert tile[0].image is None
3838
assert tile[0].valid_id is False
3939

4040
def test_wrong_year_type(self):
4141
with pytest.raises(TypeError):
42-
fs.tile.get_probability_depth(year="year", return_period=5, coordinate=[(12, 942, 1715)])
42+
fs.tile.get_probability_depth(year="year", return_period=5, search_items=[(12, 942, 1715)])
4343

4444
def test_wrong_return_period_type(self):
4545
with pytest.raises(TypeError):
46-
fs.tile.get_probability_depth(year=2050, return_period="rp", coordinate=[(12, 942, 1715)])
46+
fs.tile.get_probability_depth(year=2050, return_period="rp", search_items=[(12, 942, 1715)])
4747

4848
def test_bad_year(self):
4949
with pytest.raises(InvalidArgument):
50-
fs.tile.get_probability_depth(year=1000, return_period=5, coordinate=[(12, 942, 1715)])
50+
fs.tile.get_probability_depth(year=1000, return_period=5, search_items=[(12, 942, 1715)])
5151

5252
def test_bad_return_period(self):
5353
with pytest.raises(InvalidArgument):
54-
fs.tile.get_probability_depth(year=1000, return_period=5, coordinate=[(12, 942, 1715)])
54+
fs.tile.get_probability_depth(year=1000, return_period=5, search_items=[(12, 942, 1715)])
5555

5656
def test_single(self):
5757
coord = [(12, 942, 1715)]
58-
tile = fs.tile.get_probability_depth(year=2050, return_period=5, coordinate=coord)
58+
tile = fs.tile.get_probability_depth(year=2050, return_period=5, search_items=coord)
5959
assert len(tile) == 1
6060
assert tile[0].coordinate == coord[0]
6161
assert tile[0].image is not None
6262
assert tile[0].valid_id is True
6363

6464
def test_multiple(self):
6565
coord = [(12, 942, 1715), (17, 30990, 54379)]
66-
tile = fs.tile.get_probability_depth(year=2050, return_period=5, coordinate=coord)
66+
tile = fs.tile.get_probability_depth(year=2050, return_period=5, search_items=coord)
6767
assert len(tile) == 2
6868
tile.sort(key=lambda x: x.coordinate)
6969
assert tile[0].coordinate == coord[0]
@@ -75,15 +75,15 @@ def test_multiple(self):
7575

7676
def test_single_image(self):
7777
coord = [(12, 942, 1715)]
78-
tile = fs.tile.get_probability_depth(year=2050, return_period=5, coordinate=coord, image=True)
78+
tile = fs.tile.get_probability_depth(year=2050, return_period=5, search_items=coord, image=True)
7979
assert len(tile) == 1
8080
assert tile[0].coordinate == coord[0]
8181
assert tile[0].image is not None
8282
assert tile[0].valid_id is True
8383

8484
def test_mixed_invalid(self):
8585
coord = [(12, 942, 1715), (1, 1, 1)]
86-
tile = fs.tile.get_probability_depth(year=2050, return_period=5, coordinate=coord)
86+
tile = fs.tile.get_probability_depth(year=2050, return_period=5, search_items=coord)
8787
assert len(tile) == 2
8888
tile.sort(key=lambda x: x.coordinate, reverse=True)
8989
assert tile[0].coordinate == coord[0]
@@ -94,7 +94,7 @@ def test_mixed_invalid(self):
9494
assert tile[1].valid_id is False
9595

9696
def test_one_of_each(self):
97-
tile = fs.tile.get_probability_depth(year=2050, return_period=5, coordinate=[(12, 942, 1715)])
97+
tile = fs.tile.get_probability_depth(year=2050, return_period=5, search_items=[(12, 942, 1715)])
9898
assert len(tile) == 1
9999
assert tile[0].valid_id is True
100100
assert tile[0].coordinate == (12, 942, 1715)
@@ -107,41 +107,41 @@ class TestHistoricTiles:
107107

108108
def test_empty(self):
109109
with pytest.raises(InvalidArgument):
110-
fs.tile.get_historic_event(event_id=2, coordinate=[])
110+
fs.tile.get_historic_event(event_id=2, search_items=[])
111111

112112
def test_wrong_coord_type(self):
113113
with pytest.raises(InvalidArgument):
114-
fs.tile.get_historic_event(event_id=2, coordinate=(12, 942, 1715))
114+
fs.tile.get_historic_event(event_id=2, search_items=(12, 942, 1715))
115115

116116
def test_invalid(self):
117117
coord = [(12, 1, 1)]
118-
tile = fs.tile.get_historic_event(event_id=2, coordinate=coord)
118+
tile = fs.tile.get_historic_event(event_id=2, search_items=coord)
119119
assert len(tile) == 1
120120
assert tile[0].coordinate == coord[0]
121121
# No way to test if image is bad
122122

123123
def test_wrong_event_id_type(self):
124124
with pytest.raises(TypeError):
125-
fs.tile.get_historic_event(event_id="event_id", coordinate=[(12, 942, 1715)])
125+
fs.tile.get_historic_event(event_id="event_id", search_items=[(12, 942, 1715)])
126126

127127
def test_bad_event(self):
128128
coord = [(12, 942, 1715)]
129-
tile = fs.tile.get_historic_event(event_id=99999, coordinate=coord)
129+
tile = fs.tile.get_historic_event(event_id=99999, search_items=coord)
130130
assert len(tile) == 1
131131
assert tile[0].coordinate == coord[0]
132132
# No way to test if image is bad
133133

134134
def test_single(self):
135135
coord = [(12, 942, 1715)]
136-
tile = fs.tile.get_historic_event(event_id=2, coordinate=coord)
136+
tile = fs.tile.get_historic_event(event_id=2, search_items=coord)
137137
assert len(tile) == 1
138138
assert tile[0].coordinate == coord[0]
139139
assert tile[0].image is not None
140140
assert tile[0].valid_id is True
141141

142142
def test_multiple(self):
143143
coord = [(12, 942, 1715), (17, 30990, 54379)]
144-
tile = fs.tile.get_historic_event(event_id=2, coordinate=coord)
144+
tile = fs.tile.get_historic_event(event_id=2, search_items=coord)
145145
assert len(tile) == 2
146146
tile.sort(key=lambda x: x.coordinate)
147147
assert tile[0].coordinate == coord[0]
@@ -153,15 +153,15 @@ def test_multiple(self):
153153

154154
def test_single_image(self):
155155
coord = [(12, 942, 1715)]
156-
tile = fs.tile.get_historic_event(event_id=2, coordinate=coord, image=True)
156+
tile = fs.tile.get_historic_event(event_id=2, search_items=coord, image=True)
157157
assert len(tile) == 1
158158
assert tile[0].coordinate == coord[0]
159159
assert tile[0].image is not None
160160
assert tile[0].valid_id is True
161161

162162
def test_mixed_invalid(self):
163163
coord = [(12, 942, 1715), (2, 1, 1)]
164-
tile = fs.tile.get_historic_event(event_id=2, coordinate=coord)
164+
tile = fs.tile.get_historic_event(event_id=2, search_items=coord)
165165
assert len(tile) == 2
166166
tile.sort(key=lambda x: x.coordinate, reverse=True)
167167
assert tile[0].coordinate == coord[0]
@@ -171,7 +171,7 @@ def test_mixed_invalid(self):
171171
# No way to test if image is bad
172172

173173
def test_one_of_each(self):
174-
tile = fs.tile.get_historic_event(event_id=2, coordinate=[(12, 942, 1715)])
174+
tile = fs.tile.get_historic_event(event_id=2, search_items=[(12, 942, 1715)])
175175
assert len(tile) == 1
176176
assert tile[0].valid_id is True
177177
assert tile[0].coordinate == (12, 942, 1715)

0 commit comments

Comments
 (0)