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

Commit 50a7625

Browse files
committed
Add consistency for parameters
1 parent 1533367 commit 50a7625

12 files changed

Lines changed: 171 additions & 142 deletions

File tree

firststreet/__main__.py

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,41 @@
1919
parser.add_argument("-p", "--product", help="Example: adaptation_detail", required=True)
2020
parser.add_argument("-api_key", "--api_key", required=False)
2121
parser.add_argument("-v", "--version", required=False)
22-
parser.add_argument("-i", "--search_items", help="Example: 28,29", required=False,)
23-
parser.add_argument("-l", "--location", help="Example: property", required=False)
22+
# Convert log default to an actual boolean
23+
parser.add_argument("-log", "--log", help="Example: False", required=False, default="True")
2424
parser.add_argument("-connection_limit", "--connection_limit", help="Example: 100", required=False, default="100")
2525
parser.add_argument("-rate_limit", "--rate_limit", help="Example: 5000", required=False, default="20000")
2626
parser.add_argument("-rate_period", "--rate_period", help="Example: 3600", required=False, default="1")
27-
parser.add_argument("-log", "--log", help="Example: False", required=False, default="True")
27+
parser.add_argument("-o", "--output_dir", help="Example: /output", required=False)
28+
parser.add_argument("-s", "--search_items", help="Example: 28,29", required=False,)
29+
parser.add_argument("-l", "--location_type", help="Example: property", required=False)
30+
parser.add_argument("-y", "--year", required=False)
31+
parser.add_argument("-rp", "--return_period", required=False)
32+
parser.add_argument("-eid", "--event_id", required=False)
33+
# deprecated file parameter. Will be removed in a later version
2834
parser.add_argument("-f", "--file", help="Example: ./sample.txt", required=False)
35+
# deprecated extra_param parameter. Will be removed in a later version
2936
parser.add_argument("-e", "--extra_param", required=False)
30-
parser.add_argument("-year", "--year", required=False)
31-
parser.add_argument("-return_period", "--return_period", required=False)
32-
parser.add_argument("-event_id", "--event_id", required=False)
3337

3438
argument = parser.parse_args()
3539

36-
# Merge search_item from file and list input
40+
# Reads a file or converts search items into a list
3741
search_items = []
3842
if argument.search_items:
39-
for search_item in argument.search_items.strip().split(";"):
40-
try:
41-
search_items.append(ast.literal_eval(search_item))
42-
except (SyntaxError, ValueError):
43-
search_items.append(search_item)
43+
44+
# If file, read addresses from file
45+
if os.path.isfile(argument.search_items):
46+
search_items = read_search_items_from_file(argument.search_items)
47+
else:
48+
for search_item in argument.search_items.strip().split(";"):
49+
try:
50+
search_items.append(ast.literal_eval(search_item))
51+
except (SyntaxError, ValueError):
52+
search_items.append(search_item)
4453

4554
if argument.file:
55+
logging.warning("'file' argument deprecated and will be removed. Use `-s path_to_file` instead. "
56+
"Ex: `-s testing/sample.txt`")
4657
search_items += read_search_items_from_file(argument.file)
4758

4859
# Ensure there is at least a product and search item
@@ -70,97 +81,114 @@
7081
if argument.product == 'adaptation.get_detail':
7182
fs.adaptation.get_detail(search_items,
7283
csv=True,
84+
output_dir=argument.output_dir,
7385
extra_param=argument.extra_param)
7486

7587
elif argument.product == 'adaptation.get_summary':
7688
fs.adaptation.get_summary(search_items,
77-
argument.location,
89+
argument.location_type,
7890
csv=True,
91+
output_dir=argument.output_dir,
7992
extra_param=argument.extra_param)
8093

81-
elif argument.product == 'adaptation.get_details_by_location':
82-
fs.adaptation.get_details_by_location(search_items,
83-
argument.location,
84-
csv=True,
85-
extra_param=argument.extra_param)
94+
elif argument.product == 'adaptation.get_detail_by_location':
95+
fs.adaptation.get_detail_by_location(search_items,
96+
argument.location_type,
97+
csv=True,
98+
output_dir=argument.output_dir,
99+
extra_param=argument.extra_param)
86100

87101
elif argument.product == 'probability.get_depth':
88102
fs.probability.get_depth(search_items,
89103
csv=True,
104+
output_dir=argument.output_dir,
90105
extra_param=argument.extra_param)
91106

92107
elif argument.product == 'probability.get_chance':
93108
fs.probability.get_chance(search_items,
94109
csv=True,
110+
output_dir=argument.output_dir,
95111
extra_param=argument.extra_param)
96112

97113
elif argument.product == 'probability.get_count_summary':
98114
fs.probability.get_count_summary(search_items,
99115
csv=True,
116+
output_dir=argument.output_dir,
100117
extra_param=argument.extra_param)
101118

102119
elif argument.product == 'probability.get_cumulative':
103120
fs.probability.get_cumulative(search_items,
104121
csv=True,
122+
output_dir=argument.output_dir,
105123
extra_param=argument.extra_param)
106124

107125
elif argument.product == 'probability.get_count':
108126
fs.probability.get_count(search_items,
109-
argument.location,
127+
argument.location_type,
110128
csv=True,
129+
output_dir=argument.output_dir,
111130
extra_param=argument.extra_param)
112131

113132
elif argument.product == 'historic.get_event':
114133
fs.historic.get_event(search_items,
115134
csv=True,
135+
output_dir=argument.output_dir,
116136
extra_param=argument.extra_param)
117137

118138
elif argument.product == 'historic.get_summary':
119139
fs.historic.get_summary(search_items,
120-
argument.location,
140+
argument.location_type,
121141
csv=True,
142+
output_dir=argument.output_dir,
122143
extra_param=argument.extra_param)
123144

124145
elif argument.product == 'historic.get_events_by_location':
125146
fs.historic.get_events_by_location(search_items,
126-
argument.location,
147+
argument.location_type,
127148
csv=True,
149+
output_dir=argument.output_dir,
128150
extra_param=argument.extra_param)
129151

130152
elif argument.product == 'location.get_detail':
131153
fs.location.get_detail(search_items,
132-
argument.location,
154+
argument.location_type,
133155
csv=True,
156+
output_dir=argument.output_dir,
134157
extra_param=argument.extra_param)
135158

136159
elif argument.product == 'location.get_summary':
137160
fs.location.get_summary(search_items,
138-
argument.location,
161+
argument.location_type,
139162
csv=True,
163+
output_dir=argument.output_dir,
140164
extra_param=argument.extra_param)
141165

142166
elif argument.product == 'fema.get_nfip':
143167
fs.fema.get_nfip(search_items,
144-
argument.location,
168+
argument.location_type,
145169
csv=True,
170+
output_dir=argument.output_dir,
146171
extra_param=argument.extra_param)
147172

148173
elif argument.product == 'environmental.get_precipitation':
149174
fs.environmental.get_precipitation(search_items,
150175
csv=True,
176+
output_dir=argument.output_dir,
151177
extra_param=argument.extra_param)
152178

153179
elif argument.product == 'tile.get_probability_depth':
154-
fs.tile.get_probability_depth(year=int(argument.year), return_period=int(argument.return_period),
155-
coordinate=search_items,
180+
fs.tile.get_probability_depth(year=int(argument.year),
181+
return_period=int(argument.return_period),
182+
search_items=search_items,
183+
output_dir=argument.output_dir,
156184
image=True)
157185

158186
elif argument.product == 'tile.get_historic_event':
159-
fs.tile.get_historic_event(event_id=int(argument.event_id), coordinate=search_items,
187+
fs.tile.get_historic_event(event_id=int(argument.event_id),
188+
search_items=search_items,
189+
output_dir=argument.output_dir,
160190
image=True)
161191

162-
# AND FILES
163-
164192
else:
165193
logging.error("Product not found. Please check that the argument"
166194
" provided is correct: {}".format(argument.product))

firststreet/api/adaptation.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class Adaptation(Api):
1919
get_summary: Retrieves a list of Adaptation Summary for the given list of IDs
2020
"""
2121

22-
def get_detail(self, search_item, csv=False, output_dir=None, extra_param=None):
22+
def get_detail(self, search_items, csv=False, output_dir=None, extra_param=None):
2323
"""Retrieves adaptation detail product data from the First Street Foundation API given a list of search_items
2424
and returns a list of Adaptation Detail objects.
2525
2626
Args:
27-
search_item (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
27+
search_items (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
2828
file of First Street Foundation IDs
2929
csv (bool): To output extracted data to a csv or not
3030
output_dir (str): The output directory to save the generated csvs
@@ -34,7 +34,7 @@ def get_detail(self, search_item, csv=False, output_dir=None, extra_param=None):
3434
A list of Adaptation Detail
3535
"""
3636
# Get data from api and create objects
37-
api_datas = self.call_api(search_item, "adaptation", "detail", None, extra_param=extra_param)
37+
api_datas = self.call_api(search_items, "adaptation", "detail", None, extra_param=extra_param)
3838
product = [AdaptationDetail(api_data) for api_data in api_datas]
3939

4040
if csv:
@@ -44,12 +44,12 @@ def get_detail(self, search_item, csv=False, output_dir=None, extra_param=None):
4444

4545
return product
4646

47-
def get_details_by_location(self, search_item, location_type, csv=False, output_dir=None, extra_param=None):
47+
def get_detail_by_location(self, search_items, location_type, csv=False, output_dir=None, extra_param=None):
4848
"""Retrieves adaptation detail product data from the First Street Foundation API given a list of location
4949
search_items and returns a list of Adaptation Detail objects.
5050
5151
Args:
52-
search_item (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
52+
search_items (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
5353
file of First Street Foundation IDs
5454
location_type (str): The location lookup type
5555
csv (bool): To output extracted data to a csv or not
@@ -69,7 +69,7 @@ def get_details_by_location(self, search_item, location_type, csv=False, output_
6969
raise TypeError("location is not a string")
7070

7171
# Get data from api and create objects
72-
api_datas_summary = self.call_api(search_item, "adaptation", "summary", location_type, extra_param=extra_param)
72+
api_datas_summary = self.call_api(search_items, "adaptation", "summary", location_type, extra_param=extra_param)
7373
summary = [AdaptationSummary(api_data) for api_data in api_datas_summary]
7474

7575
search_items = list(set([adaptation for sum_adap in summary if sum_adap.adaptation for
@@ -91,12 +91,12 @@ def get_details_by_location(self, search_item, location_type, csv=False, output_
9191

9292
return [summary, detail]
9393

94-
def get_summary(self, search_item, location_type, csv=False, output_dir=None, extra_param=None):
94+
def get_summary(self, search_items, location_type, csv=False, output_dir=None, extra_param=None):
9595
"""Retrieves adaptation summary product data from the First Street Foundation API given a list of
9696
search_items and returns a list of Adaptation Summary objects.
9797
9898
Args:
99-
search_item (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
99+
search_items (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
100100
file of First Street Foundation IDs
101101
location_type (str): The location lookup type
102102
csv (bool): To output extracted data to a csv or not
@@ -116,7 +116,7 @@ def get_summary(self, search_item, location_type, csv=False, output_dir=None, ex
116116
raise TypeError("location is not a string")
117117

118118
# Get data from api and create objects
119-
api_datas = self.call_api(search_item, "adaptation", "summary", location_type, extra_param=extra_param)
119+
api_datas = self.call_api(search_items, "adaptation", "summary", location_type, extra_param=extra_param)
120120
product = [AdaptationSummary(api_data) for api_data in api_datas]
121121

122122
if csv:

firststreet/api/api.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,35 @@ 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
46+
# Not a list. This means it's a file
4747
if not isinstance(search_item, list):
4848

4949
# Check if it's a file
5050
if isinstance(search_item, str):
51-
if os.path.isfile(os.getcwd() + "/" + search_item):
51+
if os.path.isfile(search_item):
5252

5353
# Get search items from file
54-
search_item = read_search_items_from_file(os.getcwd() + "/" + search_item)
54+
search_item = read_search_items_from_file(search_item)
5555

5656
else:
5757
raise InvalidArgument("File provided is not a valid file. "
58-
"Please check the file name. '{}'".format(os.path.curdir + str(search_item)))
59-
else:
60-
raise InvalidArgument("File provided is not a list or a valid file. "
61-
"Please check the file name. '{}'".format(os.path.curdir + str(search_item)))
62-
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))
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+
#
72+
# 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)))
7375

7476
# No items found
7577
if not search_item:

firststreet/api/environmental.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class Environmental(Api):
1717
get_precipitation: Retrieves a list of Environmental Precipitation for the given list of IDs
1818
"""
1919

20-
def get_precipitation(self, search_item, csv=False, output_dir=None, extra_param=None):
20+
def get_precipitation(self, search_items, csv=False, output_dir=None, extra_param=None):
2121
"""Retrieves environmental precipitation product data from the First Street Foundation API given a list of
2222
search_items and returns a list of Environmental Precipitation objects.
2323
2424
Args:
25-
search_item (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
25+
search_items (list/file): A First Street Foundation IDs, lat/lng pair, address, or a
2626
file of First Street Foundation IDs
2727
csv (bool): To output extracted data to a csv or not
2828
output_dir (str): The output directory to save the generated csvs
@@ -33,7 +33,7 @@ def get_precipitation(self, search_item, csv=False, output_dir=None, extra_param
3333
"""
3434

3535
# Get data from api and create objects
36-
api_datas = self.call_api(search_item, "environmental", "precipitation", "county", extra_param=extra_param)
36+
api_datas = self.call_api(search_items, "environmental", "precipitation", "county", extra_param=extra_param)
3737
product = [EnvironmentalPrecipitation(api_data) for api_data in api_datas]
3838

3939
if csv:

0 commit comments

Comments
 (0)