Skip to main content

A lightweight local city database library in Python with multilanguage support.

Project description

Pycities

A lightweight city local database library in Python with multilanguage support. All data is obtained from geonames.org.

Pure Python with built-in sqlite3 module, no additional requirements.

Requirements

  • Python >= 3.7
  • SQLite3 >= 3.3.3 (Versions below 3.3.3 are unstable)

Installation

pip install pycities

Usage

# -*- coding: utf-8 -*-
from dataclasses import dataclass
from pycities import RowFactoryModelConfig, CityDatabase, dict_factory


# You can define your own custom model or use existed ones for: tuple, dict, list, sqlite3.Row
@dataclass(frozen=True)
class City:
    id: int
    name: str
    administrative_name: str
    country_name: str
    longitude: str
    latitude: str


# Create row factory for your custom model
def city_factory(cursor, row) -> City:
    return City(**dict_factory(cursor, row))


# Set your factory config for your custom model
RowFactoryModelConfig.set(City, city_factory)

# Create new instance with our model.
# You can define fields that you want to fetch in SELECT queries.
# All supported fields can be found in `pycities.config.CITY_ALL_FIELDS`
db = CityDatabase[City](
    fetch_fields=("id", "name", "administrative_name", "country_name", "longitude", "latitude")
)
# Connect to datasource
db.connect()
print(db.supported_languages)
# >>> ('uk', 'pl', 'en', 'ru')

print(db.search(query="Wroclaw", lang="pl", limit=1))
# >>> [City(id=3081368, name='Wrocław', administrative_name='Województwo dolnośląskie', country_name='Polska', longitude=17.03333, latitude=51.1)]
print(db.search(query="Breslau", lang="en", limit=1))
# >>> [City(id=3081368, name='Wroclaw', administrative_name='Lower Silesian Voivodeship', country_name='Poland', longitude=17.03333, latitude=51.1)]

for lang in db.supported_languages:
    print(db.get_city(5128581, lang=lang))
# >>> City(id=5128581, name='Нью-Йорк', administrative_name='Нью-Йорк', country_name='США', longitude=-74.00597, latitude=40.71427)
# >>> City(id=5128581, name='Nowy Jork', administrative_name='Nowy Jork', country_name='USA', longitude=-74.00597, latitude=40.71427)
# >>> City(id=5128581, name='New York', administrative_name='New York', country_name='United States', longitude=-74.00597, latitude=40.71427)
# >>> City(id=5128581, name='Нью-Йорк', administrative_name='Нью-Йорк', country_name='США', longitude=-74.00597, latitude=40.71427)

for city in db.get_nearest(latitude=43.313, longitude=-31.123, limit=3):
    print(city)
# >>> City(id=6941014, name='Ponta Delgada', administrative_name='Azores', country_name='Portugal', longitude=-25.66874, latitude=37.73952)
# >>> City(id=2270380, name='Câmara de Lobos', administrative_name='Madeira', country_name='Portugal', longitude=-16.97718, latitude=32.65043)
# >>> City(id=2267827, name='Funchal', administrative_name='Madeira', country_name='Portugal', longitude=-16.92547, latitude=32.66568)

db.close()

Contributing

Feel free to contribute to this project. You can suggest your changes, fixes, upgrades and etc. Contact me by kliuchkovladyslav@gmail.com.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycities-0.2.0.tar.gz (7.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycities-0.2.0-py3-none-any.whl (7.5 MB view details)

Uploaded Python 3

File details

Details for the file pycities-0.2.0.tar.gz.

File metadata

  • Download URL: pycities-0.2.0.tar.gz
  • Upload date:
  • Size: 7.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.1

File hashes

Hashes for pycities-0.2.0.tar.gz
Algorithm Hash digest
SHA256 64b4a1604aad32a4dcb716289a7d84e348a6a41f641ae334c94428476d8f07ae
MD5 79421cc456946c5b96d6afadeadeff40
BLAKE2b-256 42328dcdcc6994272e64a1e66bef3b2aad17b70596dd919107cc261f0cbb8210

See more details on using hashes here.

File details

Details for the file pycities-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pycities-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.1

File hashes

Hashes for pycities-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95ba1c61432e8b74d84cb079daafe78801063931b4c28cf3eb4a7c247a9f52d6
MD5 1861064b2cc92f358c7b90fd0a91fbe4
BLAKE2b-256 4f31321130f7b2ba7d373c11a8a2faa5372e3f9163c27d9ce730be380635282a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page