1010
1111try :
1212 import aiohttp
13- aiohttp_avaiable = True
13+ AIOHTTP_AVAILABLE = True
1414except ImportError :
15- aiohttp_avaiable = False
15+ AIOHTTP_AVAILABLE = False
1616
1717def backoff_max_time ():
1818 return int (os .environ .get ('BACKOFF_MAX_TIME' , '120' ))
@@ -62,7 +62,9 @@ def __init__(self, reset_time, reset_to):
6262
6363 def __unicode__ (self ):
6464 """Convert exception to a string."""
65- return f"Your rate limit has expired. It will reset to { self .reset_to } on { self .reset_time .isoformat ()} "
65+ return ("Your rate limit has expired. "
66+ f"It will reset to { self .reset_to } on { self .reset_time .isoformat ()} "
67+ )
6668
6769 __str__ = __unicode__
6870
@@ -137,7 +139,7 @@ def __exit__(self, *args):
137139 return False
138140
139141 async def __aenter__ (self ):
140- if not aiohttp_avaiable :
142+ if not AIOHTTP_AVAILABLE :
141143 raise AioHttpError ("You must install `aiohttp` to use async methods" )
142144
143145 self .session = aiohttp .ClientSession ()
@@ -156,7 +158,8 @@ def geocode(self, query, **kwargs):
156158
157159 :returns: Dict results
158160 :raises InvalidInputError: if the query string is not a unicode string
159- :raises RateLimitExceededError: if you have exceeded the number of queries you can make. Exception says when you can try again
161+ :raises RateLimitExceededError: if you have exceeded the number of queries you can make.
162+ : Exception says when you can try again
160163 :raises UnknownError: if something goes wrong with the OpenCage API
161164
162165 """
@@ -179,12 +182,12 @@ async def geocode_async(self, query, **kwargs):
179182
180183 :returns: Dict results
181184 :raises InvalidInputError: if the query string is not a unicode string
182- :raises RateLimitExceededError: if you have exceeded the number of queries you can make. Exception says when you can try again
185+ :raises RateLimitExceededError: if exceeded number of queries you can make. You can try again
183186 :raises UnknownError: if something goes wrong with the OpenCage API
184187
185188 """
186189
187- if not aiohttp_avaiable :
190+ if not AIOHTTP_AVAILABLE :
188191 raise AioHttpError ("You must install `aiohttp` to use async methods." )
189192
190193 if not self .session :
@@ -206,7 +209,8 @@ def reverse_geocode(self, lat, lng, **kwargs):
206209 :param lng: Longitude
207210 :return: Results from OpenCageData
208211 :rtype: dict
209- :raises RateLimitExceededError: if you have exceeded the number of queries you can make. Exception says when you can try again
212+ :raises RateLimitExceededError: if you have exceeded the number of queries you can make.
213+ : Exception says when you can try again
210214 :raises UnknownError: if something goes wrong with the OpenCage API
211215 """
212216 return self .geocode (_query_for_reverse_geocoding (lat , lng ), ** kwargs )
@@ -221,7 +225,7 @@ async def reverse_geocode_async(self, lat, lng, **kwargs):
221225 :param lng: Longitude
222226 :return: Results from OpenCageData
223227 :rtype: dict
224- :raises RateLimitExceededError: if you have exceeded the number of queries you can make. Exception says when you can try again
228+ :raises RateLimitExceededError: if exceeded number of queries you can make. You can try again
225229 :raises UnknownError: if something goes wrong with the OpenCage API
226230 """
227231 return await self .geocode_async (_query_for_reverse_geocoding (lat , lng ), ** kwargs )
@@ -239,8 +243,8 @@ def _opencage_request(self, params):
239243
240244 try :
241245 response_json = response .json ()
242- except ValueError as e :
243- raise UnknownError ("Non-JSON result from server" ) from e
246+ except ValueError as excinfo :
247+ raise UnknownError ("Non-JSON result from server" ) from excinfo
244248
245249 if response .status_code == 401 :
246250 raise NotAuthorizedError ()
@@ -250,8 +254,11 @@ def _opencage_request(self, params):
250254
251255 if response .status_code in (402 , 429 ):
252256 # Rate limit exceeded
253- reset_time = datetime .utcfromtimestamp (response_json ['rate' ]['reset' ])
254- raise RateLimitExceededError (reset_to = int (response_json ['rate' ]['limit' ]), reset_time = reset_time )
257+ reset_time = datetime .utcfromtimestamp (response .json ()['rate' ]['reset' ])
258+ raise RateLimitExceededError (
259+ reset_to = int (response .json ()['rate' ]['limit' ]),
260+ reset_time = reset_time
261+ )
255262
256263 if response .status_code == 500 :
257264 raise UnknownError ("500 status code from API" )
@@ -265,8 +272,8 @@ async def _opencage_async_request(self, params):
265272 async with self .session .get (self .url , params = params ) as response :
266273 try :
267274 response_json = await response .json ()
268- except ValueError as e :
269- raise UnknownError ("Non-JSON result from server" ) from e
275+ except ValueError as excinfo :
276+ raise UnknownError ("Non-JSON result from server" ) from excinfo
270277
271278 if response .status == 401 :
272279 raise NotAuthorizedError ()
@@ -278,7 +285,10 @@ async def _opencage_async_request(self, params):
278285 # Rate limit exceeded
279286 print (response_json )
280287 reset_time = datetime .utcfromtimestamp (response_json ['rate' ]['reset' ])
281- raise RateLimitExceededError (reset_to = int (response_json ['rate' ]['limit' ]), reset_time = reset_time )
288+ raise RateLimitExceededError (
289+ reset_to = int (response_json ['rate' ]['limit' ]),
290+ reset_time = reset_time
291+ )
282292
283293 if response .status == 500 :
284294 raise UnknownError ("500 status code from API" )
@@ -336,9 +346,10 @@ def floatify_latlng(input_value):
336346 if len (input_value ) == 2 and sorted (input_value .keys ()) == ['lat' , 'lng' ]:
337347 # This dict has only 2 keys 'lat' & 'lon'
338348 return {'lat' : float_if_float (input_value ["lat" ]), 'lng' : float_if_float (input_value ["lng" ])}
339- else :
340- return dict ((key , floatify_latlng (value )) for key , value in input_value .items ())
341- elif isinstance (input_value , collections .abc .MutableSequence ):
349+
350+ return dict ((key , floatify_latlng (value )) for key , value in input_value .items ())
351+
352+ if isinstance (input_value , collections .abc .MutableSequence ):
342353 return [floatify_latlng (x ) for x in input_value ]
343- else :
344- return input_value
354+
355+ return input_value
0 commit comments