Python Pricing Package
Project description
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
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 pricing-1.0.1.tar.gz
.
File metadata
- Download URL: pricing-1.0.1.tar.gz
- Upload date:
- Size: 24.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58dfed2fdf152b4e629cc12b15951c12f4b9146a31a6b6c3f7536eafee015259 |
|
MD5 | 33825de5402d1f6eaa5281fcb9617f12 |
|
BLAKE2b-256 | 8c118ebdc16eaa6628f7f34710e27f568bb477377739ab4357a0b43a3d18f924 |
File details
Details for the file pricing-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: pricing-1.0.1-py3-none-any.whl
- Upload date:
- Size: 29.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88f444ff0f1de678d1d7666459ffffcf456d8f1e9af9ab77cd9375ad1534e1eb |
|
MD5 | ebcdf109f65201964fcddecf211135e9 |
|
BLAKE2b-256 | 9da4f92d423c06d1cda66e51bbf8919b456f3c26f6ce505a001232b44f818c86 |