Skip to main content

ISO 4217 currency code library

Project description

https://github.com/ikseek/iso_4217/workflows/Python%20package/badge.svg https://img.shields.io/pypi/v/iso-4217?style=plastic

This package contains ISO 4217 active and historical currency data. Supports pint for operations with currency units.

>>> from iso_4217 import Currency
>>> Currency.USD
<Currency.USD: 'US Dollar'>
>>> Currency.USD.value
'US Dollar'
>>> Currency.USD.number
840
>>> Currency('US Dollar')
<Currency.USD: 'US Dollar'>
>>> Currency.JPY.entities
frozenset({'JAPAN'})
>>> Currency.ZWR
<Currency.ZWR: 'Zimbabwe Dollar (2009)'>
>>> Currency.ZWR.entities
frozenset()
>>> Currency.ZWR.withdrew_entities
(Historic(entity='ZIMBABWE', name='Zimbabwe Dollar'...2009, month=6), begin=None)),)
>>> Currency.VED
<Currency.VED: 'Bolívar Soberano (VED)'>

Pint units and subunits are available with convenient unit and subunit properties on Currency. Accessing these properties requires pint package installed and automatically defines currency units in application default registry.

>>> Currency.USD.unit * 5 + Currency.USD.subunit * 5
<Quantity(5.05, 'USD')>

Currency units can be defined in any UnitRegistry manually

>>> import pint
>>> from decimal import Decimal
>>> from iso_4217 import define_currency_units
>>> reg = define_currency_units(pint.UnitRegistry(non_int_type=Decimal))
>>> 5 * reg.USD
<Quantity(5, 'USD')>

But units from separate registries should not be mixed

>>> Currency.USD.unit == reg.USD
Traceback (most recent call last):
...
ValueError: Cannot operate with Unit and Unit of different registries.

If you want to replace registry used by Currency just replace the application registry:

>>> pint.set_application_registry(reg)
>>> Currency.USD.unit == reg.USD
True

Subunits are defined with s suffix:

>>> 5 * reg.USDs
<Quantity(5, 'USDs')>
>>> (5 * reg.USDs).to("USD")
<Quantity(0.05, 'USD')>
>>> (5 * reg.BHDs).to_base_units()
<Quantity(0.005, 'BHD')>

Each currency is defined within it’s own dimension:

>>> (5 * reg.USD).to('EUR')
Traceback (most recent call last):
...
pint.errors.DimensionalityError: Cannot convert from 'USD' ([currency_USD]) to 'EUR' ([currency_EUR])

But automatic currency conversion can be made via pint Contexts

>>> context = pint.Context()
>>> eur_to_usd = lambda r, eur: eur * r("1.2 USD/EUR")
>>> context.add_transformation("[currency_EUR]", "[currency_USD]", eur_to_usd)
>>> (Currency.EUR.unit * 5).to('USD', context)
<Quantity(6.0, 'USD')>

Inspired by iso4217 package by Hong Minhee.

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

iso_4217-0.6.250331.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

iso_4217-0.6.250331-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file iso_4217-0.6.250331.tar.gz.

File metadata

  • Download URL: iso_4217-0.6.250331.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for iso_4217-0.6.250331.tar.gz
Algorithm Hash digest
SHA256 5bcb6455bf1b2fc1a0049f65afb9aff82991129d57bad91bb7a3cd8c503b60ce
MD5 98d20e630780feb868762d9b275dc7c6
BLAKE2b-256 99e9d281227654d6feefe47bb4a0d4cdf8fd8c9df9afec3cb46abcef27d44944

See more details on using hashes here.

File details

Details for the file iso_4217-0.6.250331-py3-none-any.whl.

File metadata

File hashes

Hashes for iso_4217-0.6.250331-py3-none-any.whl
Algorithm Hash digest
SHA256 0bab059c01f59225f635c3c2b500f11dfec48685e01dd5c15d63414235f91ac3
MD5 4500a024e64fb3ec857e17c51e80ff05
BLAKE2b-256 7054c1b5517665cc8dab1d73ed3bcf683f496b562eb4af4abd2986c97f78d775

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page