Pricing
Maintainer
Joe Black | me@joeblack.nyc | github
Introduction
Pricing classes and tools with CLDR-backed locale-aware formatting and currency exchange. Easy to extend/customize since it’s built on ZCA.
Define your custom currencies and exchange’s in your project’s ZCML and you’re golden.
Installation
pip3 install pricing
Usage
Basic
>>> from pricing import Price
... Price('2.22', 'USD')
EUR 2.22
>>> Price('2.34223434', 'BTC')
BTC 2.34223434
>>> m = Price(2, 'USD')
... m.amount
Decimal('2')
... m.currency
'USD'
>>> m = Price('2.22', 'EUR')
... m / 2
EUR 1.11
>>> m + Price('7.77', 'EUR')
EUR 9.99
Formatting
>>> m = Price('1234.567', 'EUR')
... str(m)
'EUR 1,234.57'
>>> m.format()
'€1,234.57'
Payment URI’s
Create BIP21 and EIP681 compatible payment URI’s.
>>> from pricing.uris import BIP21PaymentURI
... BIP21PaymentURI(
... 'bitcoin',
... address='19kxPokCjD6tUU3sHaLZgEQBkRsCTBt3jj',
... amount='4.32345').to_uri()
'bitcoin:19kxPokCjD6tUU3sHaLZgEQBkRsCTBt3jj?amount=4.32345'
>>> from pricing.uris import EIP681PaymentURI
... EIP681PaymentURI(
... 'ethereum',
... address='077a7506b69e37e4f6852577190f04a35df9a36c',
... value='4.32345').to_uri()
'ethereum:0x077a7506b69e37e4f6852577190f04a35df9a36c?value=4.32345'
Currency Exchange
Currency exchange works by “installing” a backend class that implements the IExchangeBackend interface.
XPrice
You can use money.XPrice (a subclass of Price), for automatic currency conversion while adding, subtracting, and dividing money objects (+, +=, -, -=, /, //). This is useful when aggregating lots of money objects with heterogeneous currencies. The currency of the leftmost object has priority.
from pricing import XPrice
# Register backend and rates as above...
a = XPrice(1, 'AAA')
b = XPrice(1, 'BBB')
assert sum([a, b]) == XPrice('1.25', 'AAA')
Extending and customization
You can use ZCML to configure custom currencyFormats and exchanges, create a new file called currency.zcml, and follow the example below to configure.
<configure
xmlns:zope="http://namespaces.zope.org/zope"
xmlns="http://namespaces.zope.org/currency">
<zope:include package="pricing" file="currency-meta.zcml" />
<currency default="USD">
<currencyFormat
name="bitcoin"
code="BTC"
symbol="₿"
format="¤#,##0.########"
currency_digits="false"
decimal_quantization="true" />
<currencyFormat
name="litecoin"
code="LTC"
symbol="Ł"
format="¤#,##0.########"
currency_digits="false"
decimal_quantization="true" />
<currencyFormat
name="ether"
code="ETH"
symbol="Ξ"
format="¤#,##0.##################"
currency_digits="false"
decimal_quantization="true" />
<exchange
component="pricing.exchange.Exchange"
backend="pricing.exchange.CoinBaseBackend"
base="USD" />
</currency>
</configure>
Then include currency.zcml in your configure.zcml file:
<include file="currency.zcml" />
Fields
Included are custom fields for zope.schema and attrs based classes. Checkout money.fields.
Exceptions
- PriceException(Exception)
Base class for all exceptions.
- CurrencyMismatch(PriceException, ValueError)
Thrown when mixing different currencies, e.g. Price(2, 'EUR') + Price(2, 'USD'). Price objects must be converted first to the same currency, or XPrice could be used for automatic conversion.
- InvalidOperandType(PriceException, TypeError)
Thrown when attempting invalid operations, e.g. multiplication between money objects.
- ExchangeError(PriceException)
Base class for exchange exceptions.
- ExchangeBackendNotInstalled(ExchangeError)
Thrown if a conversion is attempted, but there is no backend available.
- ExchangeRateNotFound(ExchangeError)
The installed backend failed to provide a suitable exchange rate between the origin and target currencies.
Hierarchy
PriceException
CurrencyMismatch
InvalidOperandType
ExchangeError
ExchangeBackendNotInstalled
ExchangeRateNotFound
Changes
Metadata
Release files for pricing 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pricing-1.0.1.tar.gz | 24.2 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pricing-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 53.6 kB
Release files / pricing-1.0.1.tar.gz
| Download URL | pricing-1.0.1.tar.gz |
|---|---|
| Size | 24.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
58dfed2fdf152b4e629cc12b15951c12f4b9146a31a6b6c3f7536eafee015259
|
|
BLAKE2b-256 checksum How to use checksums |
8c118ebdc16eaa6628f7f34710e27f568bb477377739ab4357a0b43a3d18f924
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pricing-1.0.1-py3-none-any.whl
| Download URL | pricing-1.0.1-py3-none-any.whl |
|---|---|
| Size | 29.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
88f444ff0f1de678d1d7666459ffffcf456d8f1e9af9ab77cd9375ad1534e1eb
|
|
BLAKE2b-256 checksum How to use checksums |
9da4f92d423c06d1cda66e51bbf8919b456f3c26f6ce505a001232b44f818c86
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for pricing 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.