Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

Commit 72a45ad

Browse files
authored
Merge pull request #160 from eumiro/modernize
Few minor Python 3 code modernizations
2 parents ec583e6 + 304573c commit 72a45ad

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

overpass/api.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@ def __init__(self, *args, **kwargs):
6060
self._status = None
6161

6262
if self.debug:
63-
# https://stackoverflow.com/a/16630836
64-
try:
65-
import http.client as http_client
66-
except ImportError:
67-
# Python 2
68-
import httplib as http_client
63+
import http.client as http_client
6964
http_client.HTTPConnection.debuglevel = 1
7065

7166
# You must initialize logging,
@@ -152,19 +147,18 @@ def _api_status() -> dict:
152147
available_slots = int(
153148
next(
154149
(
155-
available_re.search(line).group()
150+
m.group()
156151
for line in lines
157-
if available_re.search(line)
152+
if (m := available_re.search(line))
158153
), 0
159154
)
160155
)
161156

162157
waiting_re = re.compile(r'(?<=Slot available after: )[\d\-TZ:]{20}')
163158
waiting_slots = tuple(
164-
datetime.strptime(
165-
waiting_re.search(line).group(), "%Y-%m-%dT%H:%M:%S%z"
166-
)
167-
for line in lines if waiting_re.search(line)
159+
datetime.strptime(m.group(), "%Y-%m-%dT%H:%M:%S%z")
160+
for line in lines
161+
if (m := waiting_re.search(line))
168162
)
169163

170164
current_idx = next(
@@ -248,7 +242,7 @@ def _construct_ql_query(self, userquery, responseformat, verbosity, date):
248242
raw_query += ";"
249243

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

253247
if responseformat == "geojson":
254248
template = self._GEOJSON_QUERY_TEMPLATE
@@ -281,19 +275,19 @@ def _get_from_overpass(self, query):
281275

282276
self._status = r.status_code
283277

284-
if self._status != 200:
285-
if self._status == 400:
286-
raise OverpassSyntaxError(query)
287-
elif self._status == 429:
288-
raise MultipleRequestsError()
289-
elif self._status == 504:
290-
raise ServerLoadError(self._timeout)
291-
raise UnknownOverpassError(
292-
"The request returned status code {code}".format(code=self._status)
293-
)
294-
else:
278+
if self._status == 200:
295279
r.encoding = "utf-8"
296280
return r
281+
elif self._status == 400:
282+
raise OverpassSyntaxError(query)
283+
elif self._status == 429:
284+
raise MultipleRequestsError()
285+
elif self._status == 504:
286+
raise ServerLoadError(self._timeout)
287+
else:
288+
raise UnknownOverpassError(
289+
f"The request returned status code {self._status}"
290+
)
297291

298292
def _as_geojson(self, elements):
299293
ids_already_seen = set()

tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def test_api_status(response: Path, slots_available: int, slots_running: Tuple[d
189189
map_query = overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635)
190190
api.get(map_query)
191191

192-
assert api.slots_available <= 2 and api.slots_available >= 0
192+
assert 0 <= api.slots_available <= 2
193193
assert api.slots_available == slots_available
194194

195195
assert isinstance(api.slots_running, tuple)

0 commit comments

Comments
 (0)