From 304573c2f641e051bbb1691df0462ef5412736c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Sun, 25 Jun 2023 22:12:24 +0200 Subject: [PATCH] Few minor Python 3 code modernizations --- overpass/api.py | 42 ++++++++++++++++++------------------------ tests/test_api.py | 2 +- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/overpass/api.py b/overpass/api.py index 5dfee7c389..24a79253ae 100644 --- a/overpass/api.py +++ b/overpass/api.py @@ -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, @@ -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( @@ -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 @@ -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() diff --git a/tests/test_api.py b/tests/test_api.py index 95c9c09122..bd9b9d3fad 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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)