Skip to main content

Pure-Python country name to ISO-3166 alpha-2 resolver with custom aliases.

Project description

country-iso2ify

A lightweight Python library for converting free-form country names into standardized ISO-3166 alpha-2 codes.Designed for real-world datasets, APIs, and AWS Lambda — with no database, no external APIs, and safe fallback logic.

Features

  • 🔍 Accurate ISO resolution using pycountry

  • 🎛 Custom alias layer for outliers (e.g., Kosovo → XK, Ivory Coast → CI)

  • Fully in-memory, Lambda-friendly, fast lookups

  • 🧠 Optional validated fuzzy matching

  • 🔧 Easily extend with add_alias()

  • 🗂 Includes curated default aliases

Installation

   pip install country-iso2ify

Or include in your Lambda layer / requirements.

Usage

  from country_iso2ify import get_resolver

  resolver = get_resolver()

  resolver.resolve("United States")     # "US"
  resolver.resolve("Ivory Coast")       # "CI"
  resolver.resolve("Kosovo")            # "XK"
  resolver.resolve("Republic of Korea") # "KR"

Add aliases dynamically

  resolver.add_alias("Mainland China", "CN")

Load aliases from S3

  resolver.load_custom_aliases_from_s3(
      bucket="my-bucket",
      key="configs/custom_alias.csv",
      skip_if_unchanged=True,  # default: compares ETag/LastModified before downloading
  )

The CSV should contain either a header row with columns like alias / name and alpha2 / alpha_2 / code, or simply two columns without headers (alias in the first column, ISO alpha-2 code in the second). Additional columns are ignored.

By default the resolver issues a cheap HeadObject call and skips the download if the ETag/Last-Modified matches the last loaded version. Pass skip_if_unchanged=False if you always want to re-fetch the file.

Enable safe fuzzy matching

  from country_iso2ify import CountryResolver

  resolver = CountryResolver(allow_fuzzy=True)
  resolver.resolve("Unted Sttes")  # "US"

Slack notifications for unresolved names

Set the COUNTRY_LOOKUP_SLACK_WEBHOOK environment variable to a Slack incoming-webhook URL to receive alerts whenever the resolver fails to map an input country name. Each unique unresolved name is reported once per process to avoid noisy repeats. You can optionally override the requests timeout (default 2.0 seconds) via COUNTRY_LOOKUP_SLACK_TIMEOUT.

Why this library?

  • Works offline

  • Handles non-standard names

  • Deterministic, safe, and fast

  • Perfect for ETL pipelines, APIs, and Lambda workloads

License

MIT License.

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

country_iso2ify-0.1.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

country_iso2ify-0.1.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file country_iso2ify-0.1.0.tar.gz.

File metadata

  • Download URL: country_iso2ify-0.1.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for country_iso2ify-0.1.0.tar.gz
Algorithm Hash digest
SHA256 43e169a5b32a65563a185a36b01f1205867f86cbdea44460fd0d918b50bd27e5
MD5 0df11bf403f09160ab3b6356e073167c
BLAKE2b-256 ac8d11c0d60a28ce88f338a492f9f73e6b17753000cee83cc6a7332246eba220

See more details on using hashes here.

File details

Details for the file country_iso2ify-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for country_iso2ify-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7135376efc34a8928156219f52031df04d64587017b6b19f8adb0ef9f1469d8d
MD5 db1ccf5ea1396a7f2c24b82efa69e51e
BLAKE2b-256 67ff00a105aec54dd94436e36c43098ad6077e9deb78f2e0d8147a7d42c9aa7e

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