Skip to main content

python-countries

Run tests codecov

Country Database in Python

  • name
  • currency
  • flag
  • official_languages
  • ...

See ggicci/countries for the full list of the properties.

Install

pip install country-database

Usage

Load all countries

from country_database import load_countries

CountryIndex = load_countries()
print(CountryIndex.CAN.alpha2_code) # "CA"
print(CountryIndex.CAN.name) # "Canada"
print(CountryIndex.CAN.name.to_locale("zh")) # "加拿大"


CountryIndexZh = load_countries(locale="zh")
print(CountryIndexZh.CAN.name) # "加拿大"
print(CountryIndexZh.CAN.name.to_locale("en")) # "Canada"

Load one country

from country_database import load_country

ca = load_country("CA") # or "CAN"
print(ca.name) # "Canada"

Load a custom list of countries

from dataclasses import dataclass
from country_database import CountryProperties, load_countries_generic

@dataclass(frozen=True)
class MyCountryIndex:
    CAN: CountryProperties
    USA: CountryProperties
    CHN: CountryProperties

CountryIndex = load_countries_generic(MyCountryIndex)

Add custom properties to a country

The default database is ggicci/countries. You can add your own data and load them easily by instantiate a DataLoader and pass it to the load_countries*, load_country* or just merge your database to the default_dataloader.

from pathlib import Path
from dataclasses import dataclass
from country_database import (
    CountryProperties,
    DataLoader,
    FullCountryIndex,
    Property,
)

CUSTOM_DATA_DIR = Path("/path/to/your/custom/data/dir")

# NOTE: if you don't need the fields from the default database,
# you can just inherit CountryPropertiesBase instead of CountryProperties
@dataclass(frozen=True)
class MyCountry(CountryProperties):
    custom_field_1: Property
    custom_field_2: Property


# WAY 1: create a new dataloader.
loader = DataLoader() # will load the default database
loader.merge_database(CUSTOM_DATA_DIR)

# NOTE: If you don't want to load the default database, use the following statement:
# It will create a loader which only loads
# 1. the country codes from the default database;
# 2. data from CUSTOM_DATA_DIR;
# but data from the default database won't be loaded.
#
# loader = DataLoader(CUSTOM_DATA_DIR)

CountryIndex = load_countries_generic(FullCountryIndex[MyCountry], loader=loader)

# WAY 2: merge your database to the default_dataloader.
from country_database import default_dataloader
default_dataloader.merge_database(CUSTOM_DATA_DIR)
CountryIndex = load_countries_generic(FullCountryIndex[MyCountry])

Metadata

Release files for country-database 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for country-database 0.1.0
File Size Uploaded Provenance Actions
country-database-0.1.0.tar.gz 17.0 kB Without provenance Details for country-database-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for country-database 0.1.0
File Interpreter ABI Platform Actions
country_database-0.1.0-py3-none-any.whl Python 3 none any Details for country_database-0.1.0-py3-none-any.whl

Total release size: 34.1 kB

Release files / country-database-0.1.0.tar.gz

Download URL country-database-0.1.0.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1870d60a31c02999478dde76cc385145d4dd563c866bc10fc3aaf31d1064cf68
BLAKE2b-256 checksum
How to use checksums
2cb74a6c17c8d3dac2df6d7c525b465eb19d88c95266fd46d67c41e9e2039b1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.1.5 CPython/3.9.4

Release files / country_database-0.1.0-py3-none-any.whl

Download URL country_database-0.1.0-py3-none-any.whl
Size 17.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c8f2de610e09b42cc01801d2f20c27185378a13327af95bb565c78d0bb23bcc6
BLAKE2b-256 checksum
How to use checksums
bbfff9f3ddd58367796fbb5576f818825d3c3f0f911024310b3a3f88a8058350
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.1.5 CPython/3.9.4

Security for country-database 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page