The HDX Python Country Library provides utilities to map between country and region codes and names and to match administrative level names from different sources. It also provides utilities for foreign exchange enabling obtaining current and historic FX rates for different currencies.
It provides country mappings including ISO 2 and ISO 3 letter codes (ISO 3166) and regions using live official data from the UN OCHA feed with fallbacks to an internal static file if there is any problem with retrieving data from the url. (Also it is possible to force the use of the internal static files.)
It can exact match English, French, Spanish, Russian, Chinese and Arabic. There is a fuzzy matching for English look up that can handle abbreviations in country names like Dem. for Democratic and Rep. for Republic.
Mapping administration level names from a source to a given base set is also handled including phonetic fuzzy name matching.
It also provides foreign exchange rates and conversion from amounts in local currency to USD and vice-versa. The conversion relies on Yahoo Finance, falling back on currency-api for current rates, and Yahoo Finance falling back on IMF data via IATI (with interpolation) for historic daily rates.
For more information, please read the documentation.
This library is part of the Humanitarian Data Exchange (HDX) project. If you have humanitarian related data, please upload your datasets to HDX.
Development is currently done using Python 3.12. We recommend using a virtual
environment such as venv
:
python -m venv venv
source venv/bin/activate
In your virtual environment, install all packages for development by running:
pip install -r requirements.txt
Be sure to install pre-commit
, which is run every time you make a git commit:
pip install pre-commit
pre-commit install
With pre-commit, all code is formatted according to ruff guidelines.
To check if your changes pass pre-commit without committing, run:
pre-commit run --all-files
Ensure you have the required packages to run the tests:
pip install -r requirements.txt
To run the tests and view coverage, execute:
pytest -c --cov hdx
uv is used for package management. If
you’ve introduced a new package to the source code (i.e. anywhere in src/
),
please add it to the project.dependencies
section of pyproject.toml
with
any known version constraints.
To add packages required only for testing, add them to the test
section under
[project.optional-dependencies]
.
Any changes to the dependencies will be automatically reflected in
requirements.txt
with pre-commit
, but you can re-generate the file without
committing by executing:
pre-commit run pip-compile --all-files
Hatch is used for project management. The project can be built using:
hatch build
Linting and syntax checking can be run with:
hatch fmt --check
Tests can be executed using:
hatch test