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

Commit b7a1b25

Browse files
committed
Bug fixes, performance fixes, and documentation updates
1 parent dfc54f4 commit b7a1b25

6 files changed

Lines changed: 36 additions & 23 deletions

File tree

firststreet/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class FirstStreet:
4141
MissingAPIError: If the API is not provided
4242
"""
4343

44-
def __init__(self, api_key=None, connection_limit=100, rate_limit=20000, rate_period=1, version=None, log=True):
44+
def __init__(self, api_key=None, connection_limit=100, rate_limit=4990, rate_period=60, version=None, log=True):
4545

4646
if not api_key:
4747
raise MissingAPIKeyError('Missing API Key.')

firststreet/__main__.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
parser.add_argument("-p", "--product", help="Example: adaptation_detail", required=False)
2525
parser.add_argument("-api_key", "--api_key", required=False)
2626
parser.add_argument("-v", "--version", required=False)
27-
# Convert log default to an actual boolean
2827
parser.add_argument("-log", "--log", help="Example: False", required=False, default="True")
2928
parser.add_argument("-connection_limit", "--connection_limit", help="Example: 100",
3029
required=False, default="100")
31-
parser.add_argument("-rate_limit", "--rate_limit", help="Example: 5000", required=False, default="20000")
32-
parser.add_argument("-rate_period", "--rate_period", help="Example: 3600", required=False, default="1")
30+
parser.add_argument("-rate_limit", "--rate_limit", help="Example: 4990", required=False, default="4990")
31+
parser.add_argument("-rate_period", "--rate_period", help="Example: 60", required=False, default="60")
3332
parser.add_argument("-o", "--output_dir", help="Example: /output", required=False)
3433
parser.add_argument("-s", "--search_items", help="Example: 28,29", required=False,)
3534
parser.add_argument("-l", "--location_type", help="Example: property", required=False)
@@ -156,16 +155,23 @@
156155
sys.exit()
157156

158157
# Adjust the connection variables
159-
connection_adjust = input("Adjust connection parameters (Y/N)? Defaults to 100 connections, with a rate limit "
160-
"of 5000 calls per 60 seconds: ")
161-
if connection_adjust.lower() == "y":
162-
argument.connection_limit = input("Input new connection limit (default 100): ")
163-
argument.rate_limit = input("Input new rate limit (default 5000): ")
164-
argument.rate_period = input("Input new rate period in seconds (default 60): ")
165-
else:
166-
argument.connection_limit = 100
167-
argument.rate_limit = 5000
168-
argument.rate_period = 60
158+
if not argument.connection_limit and not argument.rate_limit and not argument.rate_period:
159+
connection_adjust = input("Adjust connection parameters (Y/N)? Defaults to 100 connections, with a rate "
160+
"limit of 4990 calls per 60 seconds: ")
161+
if connection_adjust.lower() == "y":
162+
input_params = input("Input new connection limit (default 100): ")
163+
if input_params != '':
164+
argument.connection_limit = "providerid:{}".format(input_params)
165+
input_params = input("Input new rate limit (default 5000): ")
166+
if input_params != '':
167+
argument.rate_limit = "providerid:{}".format(input_params)
168+
input_params = input("Input new rate period in seconds (default 60): ")
169+
if input_params != '':
170+
argument.rate_period = "providerid:{}".format(input_params)
171+
else:
172+
argument.connection_limit = 100
173+
argument.rate_limit = 4990
174+
argument.rate_period = 60
169175

170176
# Adjust the output directory
171177
if not argument.output_dir:
@@ -182,7 +188,12 @@
182188
if os.path.isfile(argument.search_items):
183189
search_items = read_search_items_from_file(argument.search_items)
184190
else:
185-
for search_item in argument.search_items.strip().split(";"):
191+
items = argument.search_items.strip().split(";")
192+
if len(items) == 1:
193+
logging.warning("Could not find the file '{}'. Treating the input as a search_item instead. "
194+
"If this is unexpected, check the spelling or path of the input"
195+
.format(argument.search_items))
196+
for search_item in items:
186197
try:
187198
search_items.append(ast.literal_eval(search_item))
188199
except (SyntaxError, ValueError):

firststreet/api/csv_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import logging
77
import os
88
import pathlib
9-
import shapely.geometry
109

1110
# External Imports
1211
import pandas as pd
12+
import shapely.geometry
1313

1414

1515
def to_csv(data, product, product_subtype, location_type=None, output_dir=None):

firststreet/api/location.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def get_detail(self, search_items, location_type, csv=False, output_dir=None, ex
4141
"""
4242

4343
if not location_type:
44-
raise InvalidArgument("No loocation type provided: {}".format(location_type))
44+
raise InvalidArgument("No location type provided: {}".format(location_type))
4545
elif not isinstance(location_type, str):
4646
raise TypeError("location is not a string")
4747

firststreet/http_util.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ async def tile_response(self, response, endpoint):
141141

142142
if response.status != 200 and response.status != 500:
143143
raise self._network_error(self.options, rate_limit,
144-
status=response.reason, message=response.status)
144+
status=response.reason, endpoint=endpoint, message=response.status)
145145

146146
elif response.status == 500:
147147
logging.info(
@@ -162,7 +162,7 @@ async def product_response(self, response, endpoint):
162162

163163
try:
164164
if response.status != 200 and response.status != 404 and response.status != 500:
165-
raise self._network_error(self.options, rate_limit, error=body.get('error'))
165+
raise self._network_error(self.options, rate_limit, endpoint, error=body.get('error'))
166166

167167
error = body.get("error")
168168
if error:
@@ -213,11 +213,12 @@ def _parse_rate_limit(headers):
213213
'reset': headers.get('x-ratelimit-reset'), 'requestId': headers.get('x-request-id')}
214214

215215
@staticmethod
216-
def _network_error(options, rate_limit, error=None, status=None, message=None):
216+
def _network_error(options, rate_limit, endpoint, error=None, status=None, message=None):
217217
"""Handles any network errors as a result of the First Street Foundation API
218218
Args:
219219
options (dict): The options used in the header of the response
220220
rate_limit (dict): The rate limit information
221+
endpoint (str): The failing endpoint
221222
error (dict): The body returned from the request call
222223
status (str): The status error from the response
223224
message (str): The message error from the response
@@ -229,10 +230,11 @@ def _network_error(options, rate_limit, error=None, status=None, message=None):
229230
message = error.get('message')
230231

231232
if not status == 429:
232-
formatted = "Network Error {}: {}".format(status, message)
233+
formatted = "Network Error {}: {}. {}".format(status, message, endpoint)
233234
else:
234-
formatted = "Network Error {}: {}. Limit: {}. Remaining: {}. Reset: {}".format(status,
235+
formatted = "Network Error {}: {}. {}. Limit: {}. Remaining: {}. Reset: {}".format(status,
235236
message,
237+
endpoint,
236238
rate_limit.get('limit'),
237239
rate_limit.get('remaining'),
238240
rate_limit.get('reset'))

firststreet/models/geometry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Author: Kelvin Lai <kelvin@firststreet.org>
22
# Copyright: This module is owned by First Street Foundation
33

4-
# Internal Imports
4+
# External Imports
55
from shapely.geometry import shape
66

77

0 commit comments

Comments
 (0)