Skip to main content

This is a wrapper for pycountry, to make said library more usable.

Project description

Pycountry-Wrapper

publishing workflow

This is a wrapper for pycountry, to make said library more usable.

Installation

You can install the library by using pip:

pip install pycountry-wrapper

Usage

from pycountry_wrapper import Country

germany = Country.from_alpha_2("DE")
print(germany)
print(germany.name)

try:
    does_not_exist = Country.from_alpha_2("EN")
except ValueError:
    # if the country wasn't found, a ValueError is raised
    pass

Creating country class

You can call create an instance of Country in multiple slightly different ways.

The ISO 3166-1 standart can either use 2 or 3 letters (alpha_2 or alpha_3).

from pycountry_wrapper import Country

# auto detects if alpha_2 or alpha_3
Country("DE")
Country("DEU")

# you can specify what to use, if required.
Country.from_alpha_2("DE")
Country.from_alpha_3("DEU")

If the country can't be found it will raise a EmptyCountryException or use the fallback defined in config.fallback_country.

Alternatively you can get an instance of Country by using Country.search. This will return None if no country was found.

I also implemented a null-object pattern of Country, meaning you can get an EmptyCountry object. If you create a country from this object you'll get an instance of Country if it was found, and an instance of EmptyCountry if it wasn't.

empty = EmptyCountry("InvalidCountry")
print(type(empty))  # <class 'pycountry_wrapper.country.EmptyCountry'>

found = EmptyCountry("US")
print(type(found))  # <class 'pycountry_wrapper.country.Country'>

Accessing information

There are only a handful (readonly) attributes.

from pycountry_wrapper import Country

country = Country("DE")

country.name
country.alpha_2
country.alpha_3
country.official_name

If you have an EmptyCountry these attributes will all be None.

Configuring behavior

If you want to set a fallback country or disable fuzzy search you can do that with the config module.

from pycountry_wrapper import config

config.fallback_country = "US"
config.allow_fuzzy_search = False

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

pycountry_wrapper-1.0.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

pycountry_wrapper-1.0.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file pycountry_wrapper-1.0.1.tar.gz.

File metadata

  • Download URL: pycountry_wrapper-1.0.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pycountry_wrapper-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6111b5bc178099741da11f96f478999f76cd5fe068633e94e17b98537c9c5a23
MD5 fd6586cd459f4fecd998b88d5ead9085
BLAKE2b-256 b6e2e1d51bfc83de83e5738b1ffde8641d678ebca6001712ba2bfa8f81837169

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycountry_wrapper-1.0.1.tar.gz:

Publisher: python-publish.yml on hazel-noack/pycountry-wrapper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pycountry_wrapper-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pycountry_wrapper-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 19d97af4c86c8d55a9eb43f8939e5aaf2d72c7cec9c607927e9a95a8233cc520
MD5 99d3335e525ab703dd7d64ce68c09b31
BLAKE2b-256 b6a1164f412d2d7176f922de6fbc5508f70132439220ab844c25ad3f6353a0eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for pycountry_wrapper-1.0.1-py3-none-any.whl:

Publisher: python-publish.yml on hazel-noack/pycountry-wrapper

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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