我们从Python开源项目中,提取了以下38个代码示例,用于说明如何使用decimal.DecimalException()。
def to_python(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 self.empty_values: return None if self.localize: value = formats.sanitize_separators(value) value = smart_text(value).strip() try: value = Decimal(value) except DecimalException: raise ValidationError(self.error_messages['invalid'], code='invalid') return value
def to_python(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 self.empty_values: return None if self.localize: value = formats.sanitize_separators(value) value = force_text(value).strip() try: value = Decimal(value) except DecimalException: raise ValidationError(self.error_messages['invalid'], code='invalid') return value
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 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.validate_precision(value)
def iter_decode(self, text, validation='lax', **kwargs): _text = self.normalize(text) if validation != 'skip' and self.patterns: for error in self.patterns(_text): yield error try: result = self.to_python(_text) except (ValueError, DecimalException) as err: if validation == 'skip': yield unicode_type(_text) else: yield XMLSchemaDecodeError(self, text, self.to_python, reason=str(err)) yield None return if validation != 'skip': for validator in self.validators: for error in validator(result): yield error if isinstance(result, Decimal): try: result = kwargs.get('decimal_type')(result) except TypeError: pass yield result
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))
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
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))
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
def _to_python(self, value, state): try: return Decimal(value) except DecimalException: raise formencode.Invalid('Please enter a number', value, state)
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)