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.3.tar.gz (18.0 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.3-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: country_iso2ify-0.1.3.tar.gz
  • Upload date:
  • Size: 18.0 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.3.tar.gz
Algorithm Hash digest
SHA256 b23ffaeef9d8fc8a4dad9b51da926c97ccdedbac8c4a135507a561e1b072103e
MD5 225ca2477454dac7906505b00ea7ae82
BLAKE2b-256 5c29e5204bdc031216e27bfb5e7b3ca0cfbb94c549e13f1df34502210a7f73c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for country_iso2ify-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4a6bc53f9d7b59cbc15577874b9b48db948195794756b63fa4f94b6c0d142335
MD5 9d5d3eecd4952d3ac75c206dbb40fd5a
BLAKE2b-256 263a7c633529405c8a3081496d8963ba7f9246763ea766111a9c6fb8a1021159

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