meteociel.cities.get_city

meteociel.cities.get_city(target_name: str, *, keys: dict = None, max_delta: int = 2)

Search in the database for a targeted city. All the cities whose names contains target_name will match. Furthermore, please note that the cities’ is are str instance.

Warning

You must generate a database before using get_city.

Tip

You can pass "" as name to match all cities.

Parameters

target_namestr

The name of the city you are looking for.

keysdict, keyword-only, optionnal

By default: {}. In addition to the name a keys dictionnary can be given to restrict research. The items of keys must match the items of the cities database.

max_deltaint, keyword-only, optionnal

By default 2. The maximum delta allowed when comparing the searched name with the names in the database. The higher max_delta is, the higher the tolerance to typing errors will be, and the more likely a search will return many results.

Returns

matchdict

The dictionnary that contains the cities that matched the search.

Raises

FileNotFoundError

This exception is raised if database doesn’t exist. In that case, check the spelling of cities.DATABASE_NAME if you are using a custom one or generate a database by calling cities.generate_database().

CityNotFoundError

Raised if the requested city has not been found in the database.

Exemples

>>> import json
>>> from meteociel.cities import get_city
>>> matches = get_city("Ajaccio")
>>> json.dumps(matches, indent=4)
{
    "7761": {
        "names": [
            "ajaccio - campo dell'oro",
            "ajaccio"
        ],
        "has-sounding": true,
        "has-station": true,
        "station-type": "synop",
        "country": "france"
    },
    "7752": {
        ...
    },
    "20004014": {
        ...
    }
}
>>> matches = get_city("Ajaccio", keys={"has-sounding": True})
>>> json.dumps(matches, indent=4)
{
    "7761": {
        "names": [
            "ajaccio",
            "ajaccio - campo dell'oro"
        ],
        "has-sounding": true,
        "has-station": true,
        "station-type": "synop"
        "country": "france"
    }
}