ISO 4217 currency code library
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file iso_4217-0.6.240901.tar.gz
.
File metadata
- Download URL: iso_4217-0.6.240901.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01717ccf1f21cc3e630094ce7e67565bada98c78b31a8e0f01cc9a67e266c528 |
|
MD5 | 318b69a991b59483d5dde3cdae6867eb |
|
BLAKE2b-256 | 038c365d4c70fe086407953d6e3ac1eb32278bec55ea8dd0ae491a4a2ddb1ccc |
File details
Details for the file iso_4217-0.6.240901-py3-none-any.whl
.
File metadata
- Download URL: iso_4217-0.6.240901-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 445c9044bb19537a21b992701af33140778aca9c4b7a1c7b7429191050b1c3ce |
|
MD5 | 8528c80d1e3c0fd1c2381e3f996c0ab2 |
|
BLAKE2b-256 | bb53b1009988d857fa6fdd1dd9703e4ccebfd0b498a441a5f9f596c8f12d5a00 |