number.py 文件源码

python
阅读 36 收藏 0 点赞 0 评论 0

项目:filters 作者: eflglobal 项目源码 文件源码
def _apply(self, value):
        allowed_types = (text_type, int, float, DecimalType,)
        if self.allow_tuples:
            # Python's Decimal type supports both tuples and lists.
            # :py:meth:`decimal.Decimal.__init__`
            allowed_types += (list, tuple,)

        value = self._filter(value, Type(allowed_types))

        if self._has_errors:
            return value

        try:
            d = DecimalType(value)
        except (InvalidOperation, TypeError, ValueError):
            return self._invalid_value(value, self.CODE_INVALID, exc_info=True)

        # Decimal's constructor also accepts values such as 'NaN' or
        # '+Inf', which aren't valid in this context.
        # :see: decimal.Decimal._parser
        if not d.is_finite():
            return self._invalid_value(
                value       = value,
                reason      = self.CODE_NON_FINITE,
                exc_info    = True,
            )

        if self.max_precision is not None:
            d = d.quantize(self.max_precision)

        return d
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号