From a4d3e0525365a5232afd3068909ba4e474483918 Mon Sep 17 00:00:00 2001 From: Marco Schmid Date: Thu, 23 Apr 2026 17:28:12 +0200 Subject: [PATCH] Task_2: adding docstrings and typehints, raise errors --- TASK.md | 25 +++++-------------------- main.py | 27 ++++++++++++++++++++++++++- requirements.txt | 0 3 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 requirements.txt diff --git a/TASK.md b/TASK.md index ac6fa14..4167170 100644 --- a/TASK.md +++ b/TASK.md @@ -1,21 +1,6 @@ -# TASK: +# TASK 2: -Overpass-Query: -> wir wollen diesen in eine Python-Funktion `fetch_bergbahnen() in `main.py` einbauen - -``` - [out:json][timeout:60]; - ( - node["aerialway"="station"]({{bbox}}); - way["aerialway"="station"]({{bbox}}); - node["railway"="funicular"]({{bbox}}); - way["railway"="funicular"]({{bbox}}); - node["railway"="station"]["funicular"="yes"]({{bbox}}); - ); - out center body; -``` - -* Kopiert diesen auf `https://overpass-turbo.eu/` und führt ihn mal aus. -* Spielt mit den Zoomstufen -> bbox -* Versucht mehr über bbox herauszufinden (z.B. https://wiki.openstreetmap.org/wiki/Overpass_API) -* Versucht eine einfache fetch_bergbahn() zu bauen, die API lautet "https://overpass-api.de/api/interpreter" - Nutzt dazu die request-Library in Python, wo ihr post- und get-Requests bauen könnt \ No newline at end of file + * Fügt der aktuellen Funktion einen aussagekräftigen docstrings und typehints hinzu + * Versucht ein Error-Management mit in die Funktion einzubauen. Benutzt dazu eine eigene `OverpassApiError`-Klasse. + -> Überlegt: Was könnte nicht klappen? Wo könnte es Fehler geben? + * Versucht mal bbox auf (45.8, 5.9, 47.8, 10.5) zu setzen und die Funktion aufzurufen. Klappt das? \ No newline at end of file diff --git a/main.py b/main.py index 722c831..dd04dd2 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,33 @@ +import requests from pprint import pprint +OVERPASS_URL = "https://overpass-api.de/api/interpreter" + +BERGBAHN_QUERY = """ +[out:json][timeout:3][maxsize:500000]; +( + node["aerialway"="station"]({bbox}); + way["aerialway"="station"]({bbox}); + node["railway"="funicular"]({bbox}); + way["railway"="funicular"]({bbox}); + node["railway"="station"]["funicular"="yes"]({bbox}); +); +out center body; +""" + + def fetch_bergbahnen(bbox) -> dict: - return {} + bbox_str = ",".join(map(str, bbox)) + query = BERGBAHN_QUERY.format(bbox=bbox_str) + + resp = requests.post( + OVERPASS_URL, + data={"data": query}, + timeout=5, + headers={"User-Agent": "CDS Exercise"}, + ) + return resp.json() if __name__ == "__main__": diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29