Skip to main content

Python 定价包

项目描述

构建状态 Github 回购 皮皮版本 Pypi 许可证 皮皮轮 Pypi 版本

维护者

乔·布莱克 | @乔布莱克纽约| github

介绍

具有 CLDR 支持的区域感知格式和货币兑换的定价类和工具。由于它建立在 ZCA 之上,因此易于扩展/定制。

在您的项目的 ZCML 中定义您的自定义货币和交易所,您就很成功了。

安装

pip3 install pricing

用法

基本的

>>> 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

格式化

>>> m = Price('1234.567', 'EUR')
... str(m)
'EUR 1,234.57'

>>> m.format()
'€1,234.57'

支付 URI

创建 BIP21 和 EIP681 兼容的支付 URI。

>>> 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'

货币兑换

货币兑换通过“安装”一个实现IExchangeBackend接口的后端类来工作。

X价格

您可以使用money.XPrice(Price 的一个子类),在加、减和除货币对象(+、+=、-、-=、/、//)时进行自动货币转换。这在聚合大量具有异构货币的货币对象时很有用。最左边对象的货币具有优先权。

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')

扩展和定制

您可以使用 ZCML 配置自定义的 currencyFormats 和 exchange,创建一个名为currency.zcml的新文件,然后按照下面的示例进行配置。

<configure
    xmlns:zope=<s>"http://namespaces.zope.org/zope"</s>
    xmlns=<s>"http://namespaces.zope.org/currency"</s>>

    <zope:include package=<s>"pricing"</s> file=<s>"currency-meta.zcml"</s> />

    <currency default=<s>"USD"</s>>
        <currencyFormat
            name=<s>"bitcoin"</s>
            code=<s>"BTC"</s>
            symbol=<s>"₿"</s>
            format=<s>"¤#,##0.########"</s>
            currency_digits=<s>"false"</s>
            decimal_quantization=<s>"true"</s> />
        <currencyFormat
            name=<s>"litecoin"</s>
            code=<s>"LTC"</s>
            symbol=<s>"Ł"</s>
            format=<s>"¤#,##0.########"</s>
            currency_digits=<s>"false"</s>
            decimal_quantization=<s>"true"</s> />
        <currencyFormat
            name=<s>"ether"</s>
            code=<s>"ETH"</s>
            symbol=<s>"Ξ"</s>
            format=<s>"¤#,##0.##################"</s>
            currency_digits=<s>"false"</s>
            decimal_quantization=<s>"true"</s> />

        <exchange
            component=<s>"pricing.exchange.Exchange"</s>
            backend=<s>"pricing.exchange.CoinBaseBackend"</s>
            base=<s>"USD"</s> />
    </currency>

</configure>

然后在您的configure.zcml文件中包含currency.zcml :

<include file=<s>"currency.zcml"</s> />

字段

包括zope.schema和基于attrs的类的自定义字段。结帐money.fields

例外

PriceException(异常)

所有异常的基类。

货币不匹配(PriceException,ValueError)

混合不同货币时抛出,例如Price(2, 'EUR') + Price(2, 'USD')。Price 对象必须先转换为相同的货币,否则可以使用 XPrice 进行自动转换。

InvalidOperandType(PriceException,TypeError)

尝试无效操作时抛出,例如货币对象之间的乘法。

交换错误(价格异常)

交换异常的基类。

ExchangeBackendNotInstalled(ExchangeError)

如果尝试转换,但没有可用的后端,则抛出此异常。

ExchangeRateNotFound(ExchangeError)

安装的后端未能在原始货币和目标货币之间提供合适的汇率。

等级制度

  • 价格异常

    • 货币错配

    • 无效操作数类型

    • 交换错误

      • ExchangeBackendNotInstalled

      • ExchangeRateNotFound

变化

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

price-1.0.1.tar.gz (24.2 kB 查看哈希

已上传 source

内置分布

price-1.0.1-py3-none-any.whl (29.3 kB 查看哈希

已上传 py3