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. Written and tested for Python 3.6 and above. Supports pint for operations with currency units.

>>> from iso_4217 import Currency
>>> Currency.USD
<Currency.USD: 'US Dollar'>
>>> Currency.USD.value
'US Dollar'
>>> 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)),)

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.3.180829.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

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

iso_4217-0.3.180829-py3-none-any.whl (16.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: iso_4217-0.3.180829.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Linux/5.4.0-1047-azure

File hashes

Hashes for iso_4217-0.3.180829.tar.gz
Algorithm Hash digest
SHA256 14eb79c99b66524397c57a8fca9825dfc26f3f9d8ebc4b8cb39b6bbeb9d7b9b0
MD5 2519da1dd5c8e19b1fd6de4e521b90d1
BLAKE2b-256 b628089e9e2d8b3ba3a2a2d37ce10444372b4f9747ad15d0f5a056a6748e1196

See more details on using hashes here.

File details

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

File metadata

  • Download URL: iso_4217-0.3.180829-py3-none-any.whl
  • Upload date:
  • Size: 16.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.5 Linux/5.4.0-1047-azure

File hashes

Hashes for iso_4217-0.3.180829-py3-none-any.whl
Algorithm Hash digest
SHA256 e18036fd82c60476c4a6cb8e9361dc50ffdaa03470481b74d6c2daa99ef46246
MD5 c8e5d786aad898b1c51bb39c55654e07
BLAKE2b-256 30951e64266c2269b13e0f180f20d52758dd0aab25400fdf363306029b37f922

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