货币字段和对“z3c.form”的支持。
z3c.currenc的Python项目详细描述
为zope.schema实现货币字段并支持 将字段与z3c.form一起使用。
详细文档
货币字段
货币字段是一个数字字段,专门用于管理 货币价值。
>>> from z3c.currency import field, interfaces>>> price = field.Currency( ... title=u'Price', ... description=u'The price of the item.')
除了通用属性之外,currency字段还提供了两个附加属性 属性,精度和单位。精度旨在考虑 指定值是整单位还是整单位的1/100 单位——美元和美分。默认情况下,此字段设置为美分:
>>> price.precision is interfaces.CENTS True
可以设置为美元:
>>> price.precision = interfaces.DOLLARS >>> price.precision is interfaces.DOLLARS True
对于金融应用程序,我们有时还需要分文:
>>> price.precision = interfaces.SUBCENTS >>> price.precision is interfaces.SUBCENTS True
注:是否有一个更“国际化”的词来形容 货币?
单位指定所用货币的符号。它还用于 将数值格式化为字符串。
>>> price.unit u'$' >>> price.unit = u'SEK' >>> price.unit u'SEK'
当然,这两个属性都可用作构造函数参数:
>>> price = field.Currency( ... title=u'Price', ... description=u'The price of the item.', ... precision=interfaces.DOLLARS, ... unit=u'SEK')>>> price.precision is interfaces.DOLLARS True >>> price.unit u'SEK'
现在让我们看看验证。首先,价值必须始终 小数点:
>>> import decimal >>> price.validate(decimal.Decimal('12'))>>> price.validate(12) Traceback (most recent call last): ... WrongCurrencyType: int>>> price.validate(12.0) Traceback (most recent call last): ... WrongCurrencyType: float
另外,当精度设置为美元时,这里的值 必须是整数:
>>> price.validate(decimal.Decimal('12'))>>> price.validate(decimal.Decimal('12.01')) Traceback (most recent call last): ... IncorrectValuePrecision: 0>>> price.validate(decimal.Decimal('12.00')) Traceback (most recent call last): ... IncorrectValuePrecision: 0
当精度设置为分时,
>>> price.precision = interfaces.CENTS
则只接受带两个小数位的值:
>>> price.validate(decimal.Decimal('12.00'))>>> price.validate(decimal.Decimal('12')) Traceback (most recent call last): ... IncorrectValuePrecision: 1>>> price.validate(decimal.Decimal('12.0')) Traceback (most recent call last): ... IncorrectValuePrecision: 1
如果我们允许低于美分,
>>> price.precision = interfaces.SUBCENTS
允许任何精度:
>>> price.validate(decimal.Decimal('12.0')) >>> price.validate(decimal.Decimal('12')) >>> price.validate(decimal.Decimal('12.00001'))
如果不需要该字段,…
>>> price.required = False
让我们确保验证仍然通过。
>>> price.validate(None)
请注意,IFromUnicode接口故意不受支持:
>>> price.fromUnicode Traceback (most recent call last): ... AttributeError: 'Currency' object has no attribute 'fromUnicode'
z3c.form支持
此包还支持与^{tt2}的集成$ 包裹。特别是它实现了来自^{tt6}的数据转换器$ 任何接受Unicode字符串的小部件的字段。
>>> from z3c.currency import converter >>> conv = converter.CurrencyConverter(price, None) >>> conv <DataConverter from Currency to NoneType>
转换器很容易从任何值生成字符串:
>>> conv.toWidgetValue(decimal.Decimal(12)) u'12' >>> conv.toWidgetValue(decimal.Decimal(1200)) u'1,200' >>> conv.toWidgetValue(decimal.Decimal(-12)) u'-12' >>> conv.toWidgetValue(decimal.Decimal('-12.0')) u'-12.00' >>> conv.toWidgetValue(decimal.Decimal('-12.00')) u'-12.00'
请注意,总是打印两个小数位。你也可以设置 精确到美元:
>>> conv.field.precision = interfaces.DOLLARS>>> conv.toWidgetValue(decimal.Decimal(12)) u'12' >>> conv.toWidgetValue(decimal.Decimal('12.00')) u'12'
我们也试试分币:
>>> conv.field.precision = interfaces.SUBCENTS>>> conv.toWidgetValue(decimal.Decimal('12.00')) u'12.00' >>> conv.toWidgetValue(decimal.Decimal('12')) u'12' >>> conv.toWidgetValue(decimal.Decimal('12.0001')) u'12.0001'
如果缺少该值,请优雅地处理它。
>>> conv.toWidgetValue(None) u''
现在我们来分析一个值。解析器有一点灵活性,不仅仅是 接受输出值,…
>>> conv.field.precision = interfaces.CENTS >>> conv.toFieldValue(u'12') Decimal('12.00') >>> conv.toFieldValue(u'1,200') Decimal('1200.00') >>> conv.toFieldValue(u'-12') Decimal('-12.00') >>> conv.toFieldValue(u'-12.00') Decimal('-12.00')>>> conv.field.precision = interfaces.DOLLARS >>> conv.toFieldValue(u'12') Decimal('12') >>> conv.toFieldValue(u'12.00') Decimal('12')>>> conv.field.precision = interfaces.SUBCENTS >>> conv.toFieldValue(u'12') Decimal('12') >>> conv.toFieldValue(u'12.00') Decimal('12.00') >>> conv.toFieldValue(u'12.0000') Decimal('12.0000') >>> conv.toFieldValue(u'12.0001') Decimal('12.0001')
还有其他输入值:
>>> conv.toFieldValue(u'1200') Decimal('1200')
如果浏览器发送空字符串,请优雅地处理它。
>>> conv.toFieldValue(u'')
变化
1.2.0(2018-11-14)
- Python3.6和3.7支持。放下Python2.6。
- 测试现在使用python setup.py test运行
1.1.1(2015-11-09)
- 标准化命名空间初始化
1.1.0(2013-09-27)
- 添加了新的精度值“sub-cents”(interfaces.SUBCENTS),其中 允许精度超过一分钱,这是财务和其他所需的 商业应用。
1.0.0(2013-08-16)
- 更新了trove分类器。
- 已将代码移动到github。
- 更改验证以引发自定义验证错误,因为 代码查看异常的doc字符串,而不是第一个参数。
- 改进的转换器
- 确保初始分析后十进制的正确精度。
- 将值格式化为适当的精度。
- 将iccurrency接口中的“precision”字段更改为一个选项,以便 ui生成得很好。
- 稍微清理一下代码。
0.1.0(2007-09-12)
- 初始释放
- 支持精度和单位的Currency字段的实现。
- 数据转换器的实现。