Skip to content

Commit

Permalink
Merge pull request #160 from eumiro/modernize
Browse files Browse the repository at this point in the history
Few minor Python 3 code modernizations
  • Loading branch information
mvexel authored Feb 13, 2024
2 parents ec583e6 + 304573c commit 72a45ad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
42 changes: 18 additions & 24 deletions overpass/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,7 @@ def __init__(self, *args, **kwargs):
self._status = None

if self.debug:
# https://stackoverflow.com/a/16630836
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
import http.client as http_client
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging,
Expand Down Expand Up @@ -152,19 +147,18 @@ def _api_status() -> dict:
available_slots = int(
next(
(
available_re.search(line).group()
m.group()
for line in lines
if available_re.search(line)
if (m := available_re.search(line))
), 0
)
)

waiting_re = re.compile(r'(?<=Slot available after: )[\d\-TZ:]{20}')
waiting_slots = tuple(
datetime.strptime(
waiting_re.search(line).group(), "%Y-%m-%dT%H:%M:%S%z"
)
for line in lines if waiting_re.search(line)
datetime.strptime(m.group(), "%Y-%m-%dT%H:%M:%S%z")
for line in lines
if (m := waiting_re.search(line))
)

current_idx = next(
Expand Down Expand Up @@ -248,7 +242,7 @@ def _construct_ql_query(self, userquery, responseformat, verbosity, date):
raw_query += ";"

if date:
date = f'[date:"{date.strftime("%Y-%m-%dT%H:%M:%SZ")}"]'
date = f'[date:"{date:%Y-%m-%dT%H:%M:%SZ}"]'

if responseformat == "geojson":
template = self._GEOJSON_QUERY_TEMPLATE
Expand Down Expand Up @@ -281,19 +275,19 @@ def _get_from_overpass(self, query):

self._status = r.status_code

if self._status != 200:
if self._status == 400:
raise OverpassSyntaxError(query)
elif self._status == 429:
raise MultipleRequestsError()
elif self._status == 504:
raise ServerLoadError(self._timeout)
raise UnknownOverpassError(
"The request returned status code {code}".format(code=self._status)
)
else:
if self._status == 200:
r.encoding = "utf-8"
return r
elif self._status == 400:
raise OverpassSyntaxError(query)
elif self._status == 429:
raise MultipleRequestsError()
elif self._status == 504:
raise ServerLoadError(self._timeout)
else:
raise UnknownOverpassError(
f"The request returned status code {self._status}"
)

def _as_geojson(self, elements):
ids_already_seen = set()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_api_status(response: Path, slots_available: int, slots_running: Tuple[d
map_query = overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635)
api.get(map_query)

assert api.slots_available <= 2 and api.slots_available >= 0
assert 0 <= api.slots_available <= 2
assert api.slots_available == slots_available

assert isinstance(api.slots_running, tuple)
Expand Down

0 comments on commit 72a45ad

Please sign in to comment.