python定价包
pricing的Python项目详细描述
维护人员
乔·布莱克{{a7}{a8}
简介
使用CLDR支持的区域设置感知格式和货币交换的定价类和工具。易于扩展/定制,因为它是基于zca构建的。
在项目的zcml中定义您的自定义货币和兑换,您就是黄金。
安装
pip3 install pricing
用法
基本
>>>frompricingimportPrice...Price('2.22','USD')EUR2.22>>>Price('2.34223434','BTC')BTC2.34223434>>>m=Price(2,'USD')...m.amountDecimal('2')...m.currency'USD'>>>m=Price('2.22','EUR')...m/2EUR1.11>>>m+Price('7.77','EUR')EUR9.99
格式化
>>>m=Price('1234.567','EUR')...str(m)'EUR 1,234.57'>>>m.format()'€1,234.57'
付款uri
创建与bip21和eip681兼容的支付uri。
>>>frompricing.urisimportBIP21PaymentURI...BIP21PaymentURI(...'bitcoin',...address='19kxPokCjD6tUU3sHaLZgEQBkRsCTBt3jj',...amount='4.32345').to_uri()'bitcoin:19kxPokCjD6tUU3sHaLZgEQBkRsCTBt3jj?amount=4.32345'
>>>frompricing.urisimportEIP681PaymentURI...EIP681PaymentURI(...'ethereum',...address='077a7506b69e37e4f6852577190f04a35df9a36c',...value='4.32345').to_uri()'ethereum:0x077a7506b69e37e4f6852577190f04a35df9a36c?value=4.32345'
货币兑换
货币兑换通过“安装”一个实现IExchangeBackend接口的后端类来工作。
xprice
您可以使用money.XPrice(price的一个子类)在添加、减去和分割货币对象时自动转换货币(+、+=、-、-=、/、//)。当使用不同的货币聚合大量货币对象时,这非常有用。最左边对象的货币具有优先级。
frompricingimportXPrice# Register backend and rates as above...a=XPrice(1,'AAA')b=XPrice(1,'BBB')assertsum([a,b])==XPrice('1.25','AAA')
扩展和定制
您可以使用zcml配置自定义货币格式和交换,创建名为currency.zcml的新文件,并按照下面的示例进行配置。
<configurexmlns:zope="http://namespaces.zope.org/zope"xmlns="http://namespaces.zope.org/currency"><zope:includepackage="pricing"file="currency-meta.zcml"/><currencydefault="USD"><currencyFormatname="bitcoin"code="BTC"symbol="₿"format="¤#,##0.########"currency_digits="false"decimal_quantization="true"/><currencyFormatname="litecoin"code="LTC"symbol="Ł"format="¤#,##0.########"currency_digits="false"decimal_quantization="true"/><currencyFormatname="ether"code="ETH"symbol="Ξ"format="¤#,##0.##################"currency_digits="false"decimal_quantization="true"/><exchangecomponent="pricing.exchange.Exchange"backend="pricing.exchange.CoinBaseBackend"base="USD"/></currency></configure>
然后将currency.zcml包含在configure.zcml文件中:
<includefile="currency.zcml"/>
字段
包括基于zope.schema和attrs的类的自定义字段。签出money.fields。
例外情况
- PriceException(Exception)
- 所有异常的基类。
- CurrencyMismatch(PriceException, ValueError) 当混合不同的货币时抛出DD>,例如{TT11}$。价格对象必须先转换为相同的货币,否则xprice可以用于自动转换。
- InvalidOperandType(PriceException, TypeError)
- 尝试无效操作时抛出,例如货币对象之间的乘法。
- ExchangeError(PriceException)
- 交换异常的基类。
- ExchangeBackendNotInstalled(ExchangeError)
- 如果尝试转换,则抛出,但没有可用的后端。
- ExchangeRateNotFound(ExchangeError)
- 安装的后端无法提供源货币和目标货币之间的适当汇率。
层次结构
- PriceException
- CurrencyMismatch
- InvalidOperandType
- ExchangeError
- ExchangeBackendNotInstalled
- ExchangeRateNotFound