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"

Enforce strict ISO-only outputs

By default, unresolved names echo back the original input so downstream ETL jobs can inspect them later. If you prefer to drop unresolved values (or receive None for single-value lookups), enable strict mode:

  from country_iso2ify import CountryResolver

  resolver = CountryResolver(is_strict=True)
  resolver.resolve("Atlantis")        # None
  resolver.resolve("US|Atlantis")     # "US"

  resolver = CountryResolver(is_strict=False)
  resolver.resolve("US|Atlantis")     # "US|Atlantis"

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.2.tar.gz (17.9 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.2-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: country_iso2ify-0.1.2.tar.gz
  • Upload date:
  • Size: 17.9 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.2.tar.gz
Algorithm Hash digest
SHA256 571bd7e8d3f085e55a8d4b64f499a7c4263807d0a2538a261ab7da8a60cae516
MD5 d2deacc54957eefbb43f547d7f2ed764
BLAKE2b-256 06d093892607f9f76de4f2154cfab0f770956bb9638f51fc27b0a969beca2e37

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for country_iso2ify-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2c651ad54f71112653dd4a1802674b976d91c6ef4599fb0a1c2101a31c92e0e0
MD5 61ce1d47620c38ac507d16a90cd0d4f8
BLAKE2b-256 7667661cf4e6d06d7b1ae0b03b903f3c54a5f16cf404ad0bf80fcb1075d77a59

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