python类DecimalException()的实例源码

fields.py 文件源码 项目:sdining 作者: Lurance 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def to_internal_value(self, data):
        """
        Validate that the input is a decimal number and return a Decimal
        instance.
        """

        data = smart_text(data).strip()

        if self.localize:
            data = sanitize_separators(data)

        if len(data) > self.MAX_STRING_LENGTH:
            self.fail('max_string_length')

        try:
            value = decimal.Decimal(data)
        except decimal.DecimalException:
            self.fail('invalid')

        # Check for NaN. It is the only value that isn't equal to itself,
        # so we can use this to identify NaN values.
        if value != value:
            self.fail('invalid')

        # Check for infinity and negative infinity.
        if value in (decimal.Decimal('Inf'), decimal.Decimal('-Inf')):
            self.fail('invalid')

        return self.quantize(self.validate_precision(value))
fields.py 文件源码 项目:esdc-ce 作者: erigones 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def from_native(self, value):
        """
        Validates that the input is a decimal number. Returns a Decimal
        instance. Returns None for empty values. Ensures that there are no more
        than max_digits in the number, and no more than decimal_places digits
        after the decimal point.
        """
        if value in validators.EMPTY_VALUES:
            return None
        value = smart_text(value).strip()
        try:
            value = Decimal(value)
        except DecimalException:
            raise ValidationError(self.error_messages['invalid'])
        return value
fields.py 文件源码 项目:aiorestframework 作者: Skorpyon 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def to_internal_value(self, data):
        """
        Validate that the input is a decimal number and return a Decimal
        instance.
        """

        data = str(data).strip()

        if len(data) > self.MAX_STRING_LENGTH:
            self.fail('max_string_length')

        try:
            value = decimal.Decimal(data)
        except decimal.DecimalException:
            self.fail('invalid')

        # Check for NaN. It is the only value that isn't equal to itself,
        # so we can use this to identify NaN values.
        if value != value:
            self.fail('invalid')

        # Check for infinity and negative infinity.
        if value in (decimal.Decimal('Inf'), decimal.Decimal('-Inf')):
            self.fail('invalid')

        return self.quantize(self.validate_precision(value))
mathematical_evaluation.py 文件源码 项目:Tutorial-Chatterbot 作者: isipalma 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def is_float(self, string):
        """
        If the string is a float, returns
        the float of the string. Otherwise,
        it returns False.
        """
        try:
            return decimal.Decimal(string)
        except decimal.DecimalException:
            return False
validators.py 文件源码 项目:sqlalchemy-validation 作者: blazelibs 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _to_python(self, value, state):
        try:
            return Decimal(value)
        except DecimalException:
            raise formencode.Invalid('Please enter a number', value, state)
evaluate_mathematically.py 文件源码 项目:ChatBot 作者: drinksober 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def is_float(self, string):
        """
        If the string is a float, returns
        the float of the string. Otherwise,
        it returns False.
        """

        try:
            return decimal.Decimal(string)
        except decimal.DecimalException:
            return False
evaluate_mathematically.py 文件源码 项目:ChatBot 作者: drinksober 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def is_float(self, string):
        """
        If the string is a float, returns
        the float of the string. Otherwise,
        it returns False.
        """

        try:
            return decimal.Decimal(string)
        except decimal.DecimalException:
            return False
resources.py 文件源码 项目:django-hordak 作者: adamcharnock 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def import_obj(self, obj, data, dry_run):
        F = TransactionCsvImportColumn.TO_FIELDS
        use_dual_amounts = F.amount_out in data and F.amount_in in data

        if F.date not in data:
            raise ValueError('No date column found')

        try:
            date = datetime.strptime(data[F.date], self.date_format).date()
        except ValueError:
            raise ValueError('Invalid value for date. Expected {}'.format(
                dict(DATE_FORMATS)[self.date_format]
            ))

        description = data[F.description]

        # Do we have in/out columns, or just one amount column?
        if use_dual_amounts:
            amount_out = data[F.amount_out]
            amount_in = data[F.amount_in]

            if amount_in and amount_out:
                raise ValueError('Values found for both Amount In and Amount Out')
            if not amount_in and not amount_out:
                raise ValueError('Value required for either Amount In or Amount Out')

            if amount_out:
                try:
                    amount = abs(Decimal(amount_out)) * -1
                except DecimalException:
                    raise ValueError('Invalid value found for Amount Out')
            else:
                try:
                    amount = abs(Decimal(amount_in))
                except DecimalException:
                    raise ValueError('Invalid value found for Amount In')
        else:
            if F.amount not in data:
                raise ValueError('No amount column found')
            if not data[F.amount]:
                raise ValueError('No value found for amount')
            try:
                amount = Decimal(data[F.amount])
            except:
                raise DecimalException('Invalid value found for Amount')

        if amount == Decimal('0'):
            raise ValueError('Amount of zero not allowed')

        data = dict(
            date=date,
            amount=amount,
            description=description,
        )
        return super(StatementLineResource, self).import_obj(obj, data, dry_run)


问题


面经


文章

微信
公众号

扫码关注公众号