# Python

> Ein Client für Python ab 3.9, der nur die Standardbibliothek braucht. Ein Modul, keine Abhängigkeiten.

Source: https://findort.ch/de/docs/python/

## Installieren

<p class="install"><span class="sign">pip install findort</span></p>

```python
from findort import Findort

findort = Findort(os.environ["FINDORT_KEY"], lang="de")
place = findort.geocode("Bundesplatz 3, Bern", country="ch")

if place:
    print(place.label, place.location.lat, place.location.lon)
```

Auf dem Server nimmst du einen geheimen Key `sk_…`.

## Client erstellen

```python
```

| Argument | Typ | Vorgabe | Beschreibung |
|---|---|---|---|
| `key` | str | | API-Key |
| `base_url` | str | `https://api.findort.ch` | Adresse der API |
| `lang` | str | | Sprache für alle Anfragen: `de`, `fr`, `it`, `en` |
| `country` | str oder Liste | | Länderfilter für alle Suchen: `"ch"` oder `["ch", "li"]` |
| `timeout` | float | `8.0` | Zeitlimit in Sekunden |
| `transport` | callable | | Eigene HTTP-Funktion für Tests: `(url, headers) -> (status, body_bytes)` |

## search

```python
    "Bahnhofstr 1 Zür",
    limit=5,
    types=["address", "poi"],
    near=(47.3769, 8.5417),
)
for place in places:
    print(place.label)
```

| Argument | Typ | Beschreibung |
|---|---|---|
| `limit` | int | 1 bis 20, Vorgabe 5 |
| `lang` | str | Sprache nur für diese Anfrage |
| `country` | str oder Liste | Länderfilter nur für diese Anfrage |
| `near` | `(lat, lon)` | Resultate in der Nähe bevorzugen |
| `bbox` | `(minLon, minLat, maxLon, maxLat)` | hart auf ein Rechteck begrenzen |
| `types` | str oder Liste | nur diese [Typen](https://findort.ch/de/docs/antwort/#typen) |

## reverse

```python
print(places[0].label if places else "nichts gefunden")
```

Argumente: `limit`, `lang`, `radius` (Kilometer, 0.01 bis 50), `types`.

## geocode

Der beste Treffer als `Place` oder `None`. Nimmt dieselben Argumente wie `search`.

```python
```

## Place

`Place` ist eine unveränderliche Dataclass.

| Feld | Typ |
|---|---|
| `id`, `label`, `name`, `type`, `category` | str |
| `address` | dict mit den [Adressfeldern](https://findort.ch/de/docs/antwort/#adresse) |
| `location` | `Location` mit `lat` und `lon` |
| `bbox` | `tuple` als `(minLon, minLat, maxLon, maxLat)` oder `None` |

Dazu kommen die Abkürzungen `street`, `housenumber`, `postcode`, `city` (Stadt oder Ortsteil) und `country_code`. Sie sind `None`, wenn der Wert fehlt.

`Location.distance_to()` rechnet die Luftlinie in Kilometern:

```python
thun = findort.geocode("Bahnhof Thun")
print(f"{bern.location.distance_to(thun.location):.1f} km")
```

## Fehler

Bei einer Fehlerantwort wirft der Client `FindortError` mit `status` (HTTP) und `code`. Ist der Server nicht erreichbar, ist `code` gleich `network_error` und `status` gleich `0`.

```python
from findort import FindortError

try:
    findort.search("Bern")
except FindortError as e:
    if e.code == "rate_limited":
        time.sleep(2)
```

Alle Codes stehen unter [Fehler](https://findort.ch/de/docs/fehler/).
