def __new__(cls, value=0, context=None): """ Initialize a new Size object. Must pass a bytes or a spec value for size. The bytes value is a numerical value for the size this object represents, in bytes. The spec value is a string specification of the size using any of the size specifiers in the _decimalPrefixes or _binaryPrefixes lists combined with a 'b' or 'B'. For example, to specify 640 kilobytes, you could pass any of these spec parameters: "640kb" "640 kb" "640KB" "640 KB" "640 kilobytes" If you want to use a spec value to represent a bytes value, you can use the letter 'b' or 'B' or omit the size specifier. """ if isinstance(value, (unicode, str)): size = _parseSpec(value) elif isinstance(value, (int, long, float, Decimal)): size = Decimal(value) elif isinstance(value, Size): size = Decimal(value.convertTo("b")) else: raise ValueError("invalid value %s for size" % value) # drop any partial byte size = size.to_integral_value(rounding=ROUND_DOWN) self = Decimal.__new__(cls, value=size) return self
def __new__(cls, bytes=None, spec=None): """ Initialize a new Size object. Must pass either bytes or spec, but not both. The bytes parameter is a numerical value for the size this object represents, in bytes. The spec parameter is a string specification of the size using any of the size specifiers in the _decimalPrefix or _binaryPrefix lists combined with a 'b' or 'B'. For example, to specify 640 kilobytes, you could pass any of these parameter: spec="640kb" spec="640 kb" spec="640KB" spec="640 KB" spec="640 kilobytes" If you want to use spec to pass a bytes value, you can use the letter 'b' or 'B' or simply leave the specifier off and bytes will be assumed. """ if bytes and spec: raise SizeParamsError("only specify one parameter") if bytes is not None: if type(bytes).__name__ in ["int", "long", "float", 'Decimal' ] and bytes >= 0: self = Decimal.__new__(cls, value=bytes) else: raise SizeNotPositiveError("bytes= param must be >=0") elif spec: self = Decimal.__new__(cls, value=_parseSpec(spec)) else: raise SizeParamsError("missing bytes= or spec=") return self
def __new__(cls, bytes=None, spec=None): """ Initialize a new Size object. Must pass either bytes or spec, but not both. The bytes parameter is a numerical value for the size this object represents, in bytes. The spec parameter is a string specification of the size using any of the size specifiers in the _decimalPrefix or _binaryPrefix lists combined with a 'b' or 'B'. For example, to specify 640 kilobytes, you could pass any of these parameter: spec="640kb" spec="640 kb" spec="640KB" spec="640 KB" spec="640 kilobytes" If you want to use spec to pass a bytes value, you can use the letter 'b' or 'B' or simply leave the specifier off and bytes will be assumed. """ if bytes and spec: raise SizeParamsError("only specify one parameter") if bytes: if type(bytes).__name__ in ["int", "long"] and bytes > 0: self = Decimal.__new__(cls, value=bytes) else: raise SizeNotPositiveError("bytes= param must be >0") elif spec: self = Decimal.__new__(cls, value=_parseSpec(spec)) else: raise SizeParamsError("missing bytes= or spec=") return self
def __new__(cls, value="0", context=None): if isinstance(value,basestring) and value.endswith('%'): self = Decimal.__new__(Percentage,Decimal(value[:-1]) / 100,context) self._value = value return self #~ raise Exception("Invalid Percentage %r" % value) return cls.__new__(Quantity,value,context)
def __new__(cls, value="0:00", context=None): if isinstance(value, datetime.timedelta): hours = 0 if value.days != 0: hours += value.days * 24 value = datetime.timedelta(seconds=value.seconds) a = str(value).split(':')[:2] hours += int(a[0]) minutes = int(a[1]) cv = Decimal(hours) + Decimal(minutes) * DEC2HOUR text = '%d:%02d' % (hours, minutes) else: text = str(value) if ':' in text: try: h, m = text.split(':') except ValueError: raise ValueError("Cannot convert %r to Duration" % value) cv = Decimal(h) + Decimal(m) * DEC2HOUR else: cv = Decimal(value) hours = int(cv) minutes = old_div((cv - hours), DEC2HOUR).to_integral() # minutes = old_div((hours - int(self)), DEC2HOUR) text = '%d:%02d' % (hours, minutes) self = Decimal.__new__(cls, cv, context) self._text = text return self
def __new__(cls, value=0, context=None): """ Initialize a new Size object. Must pass a bytes or a spec value for size. The bytes value is a numerical value for the size this object represents, in bytes. The spec value is a string specification of the size using any of the size specifiers in the _DECIMAL_PREFIXES or _BINARY_PREFIXES lists combined with a 'b' or 'B'. For example, to specify 640 kilobytes, you could pass any of these spec parameters: "640kb" "640 kb" "640KB" "640 KB" "640 kilobytes" If you want to use a spec value to represent a bytes value, you can use the letter 'b' or 'B' or omit the size specifier. """ if isinstance(value, (six.string_types, bytes)): size = parseSpec(value) elif isinstance(value, (six.integer_types, float, Decimal)): size = Decimal(value) elif isinstance(value, Size): size = Decimal(value.convertTo()) else: raise ValueError("invalid value %s for size" % value) # drop any partial byte size = size.to_integral_value(rounding=ROUND_DOWN) self = Decimal.__new__(cls, value=size, context=context) return self
def __new__(cls, value=0, context=None, summary_instance=None): """ Create a new immutable Decimal object, adding our custom attributes. """ obj = Decimal.__new__(cls, value, context) obj.initialise_context(summary_instance) return obj
def __new__(cls, value="0", context=None): if isinstance(value, basestring) and value.endswith('%'): self = Decimal.__new__(Percentage, Decimal(value[:-1]) / 100, context) self._value = value return self #~ raise Exception("Invalid Percentage %r" % value) return cls.__new__(Quantity, value, context)
def __new__(cls, value="0", context=None): if isinstance(value, six.string_types) and value.endswith('%'): self = Decimal.__new__(Percentage, old_div(Decimal(value[:-1]), 100), context) self._value = value return self #~ raise Exception("Invalid Percentage %r" % value) return cls.__new__(Quantity, value, context)
def __new__(cls, orb=0): """Orb initialization (orb between 0 and 30). :type orb: numeric """ orb = float(orb) if orb < 0 or orb > 30: # we consider larger orbs to be errors... raise ValueError('Invalid orb value %s.' % orb) return Decimal.__new__(cls, str(orb))
def __new__(cls, value="0", context=None): #~ def __init__(self,value,**kw): if isinstance(value,basestring) and ':' in value: h,m = value.split(':') value = Decimal(h) + Decimal(m) * DEC2HOUR #~ self = super(Duration,cls).__new__(Duration,value,context) self = Decimal.__new__(Duration,value,context) #~ self._hh_mm = True return self
def __new__(cls, value="0", context=None): if isinstance(value, basestring): if ':' in value: h, m = value.split(':') value = Decimal(h) + Decimal(m) * DEC2HOUR elif isinstance(value, datetime.timedelta): a = str(value).split(':')[:2] return cls(':'.join(a)) self = Decimal.__new__(Duration, value, context) return self
def __new__(cls, num, denom=None): if denom is None: if isinstance(num, cls): return num else: denom = 1 num, denom = int(num), int(denom) instance = Decimal.__new__(cls, Decimal(num) / denom) instance.__num, instance.__denom = (num, denom) instance.__tuple = (unicode(num), unicode(denom)) return instance
def __new__(cls, value, **kw): new = Decimal.__new__(cls, value.replace(",", "."), **kw) new.int = Decimal(int(new)) new.dec = new - new.int new.string = value.replace(".", ",") new.len = len(new.string) new.l_int = len(new.int.as_tuple().digits) new.l_dec = len(new.dec.as_tuple().digits) if new.dec else 0 new.l_chiffres = new.l_int + new.l_dec return new
def __new__(cls, num, denom=None): if denom is None: if isinstance(num, cls): return num else: denom = 1 num, denom = int(num), int(denom) instance = Decimal.__new__(cls, Decimal(num) / denom) instance.__num, instance.__denom = (num, denom) instance.__tuple = (str(num), str(denom)) return instance
def new_money(cls, value='NaN', context=None): """ Build a class named MoneyIn<currency_code> inheriting from Decimal. """ if isinstance(value, cls): assert cls._currency_code == value._currency_code, "Money type currency mismatch" if value is None: value = 'NaN' try: self = Decimal.__new__(cls, value, context) except Exception as err: raise ValueError(err) return self
def __new__(cls, amount_input, currency): subunit = currency_subunits.get(currency, subunit2) value = Decimal(amount_input).quantize(subunit) self = Decimal.__new__(cls, value) self.amount_input = amount_input if '-' in amount_input: self.sign = -1 elif '+' in amount_input: self.sign = 1 else: # Unspecified. self.sign = 0 return self
def __new__(cls, value="0%", context=None): if value is NotImplemented: return value if isinstance(value, six.string_types): text = value if text[-1] != "%": text += "%" cv = Decimal(text[:-1]) / Decimal(100) else: cv = value text = str(value * 100) + "%" self = Decimal.__new__(cls, cv, context) self._text = text return self
def __new__(self, *args, **kwargs): """ Overloads the decimal __new__ function in order to round the input value to the new value. """ if self._precision is not None: if len(args): value = Decimal(args[0]).quantize(Decimal(str(self._precision))) else: value = Decimal(0) elif len(args): value = Decimal(args[0]) else: value = Decimal(0) obj = Decimal.__new__(self, value, **kwargs) return obj
def __new__(cls, value="0", context=None): if isinstance(value, six.string_types): if ':' in value: h, m = value.split(':') value = Decimal(h) + Decimal(m) * DEC2HOUR elif isinstance(value, datetime.timedelta): hours = 0 if value.days != 0: hours += value.days * 24 value = datetime.timedelta(seconds=value.seconds) a = str(value).split(':')[:2] hours += int(a[0]) minutes = int(a[1]) return cls('{0}:{1}'.format(hours, minutes)) # return cls(':'.join(a)) self = Decimal.__new__(Duration, value, context) return self
def __new__(cls, value="0", context=None): if isinstance(value, basestring): if ':' in value: h, m = value.split(':') value = Decimal(h) + Decimal(m) * DEC2HOUR elif isinstance(value, datetime.timedelta): hours = 0 if value.days != 0: hours += value.days * 24 value = datetime.timedelta(seconds=value.seconds) a = str(value).split(':')[:2] hours += int(a[0]) minutes = int(a[1]) return cls('{0}:{1}'.format(hours, minutes)) # return cls(':'.join(a)) self = Decimal.__new__(Duration, value, context) return self
def __new__(cls, num, exponent=None): if exponent is None: if isinstance(num, cls): return num else: exponent = 1 else: exponent = int(exponent) if exponent > 0: value = Decimal(num) * (10 ** exponent) else: value = Decimal(num) / (10 ** abs(exponent)) instance = Decimal.__new__(cls, value) instance.__num, instance.__exponent = num, exponent instance.__tuple = (str(num), str(exponent)) return instance
def __new__(cls, num, exponent=None): if exponent is None: if isinstance(num, cls): return num else: exponent = 1 else: exponent = int(exponent) if exponent > 0: value = Decimal(num) * (10**exponent) else: value = Decimal(num) / (10**abs(exponent)) instance = Decimal.__new__(cls, value) instance.__num, instance.__exponent = num, exponent instance.__tuple = (unicode(num), unicode(exponent)) return instance
def new_money(cls, value='NaN', context=None): """ Build a class named MoneyIn<currency_code> inheriting from Decimal. """ if isinstance(value, cls): assert cls._currency_code == value._currency_code if isinstance(value, (cls, Decimal)): self = object.__new__(cls) self._exp = value._exp self._sign = value._sign self._int = value._int self._is_special = value._is_special return self if value is None: value = 'NaN' try: self = Decimal.__new__(cls, value, context) except Exception as err: raise ValueError(err) return self
def __new__(cls, bytes=None, spec=None): """ Initialize a new Size object. Must pass either bytes or spec, but not both. The bytes parameter is a numerical value for the size this object represents, in bytes. The spec parameter is a string specification of the size using any of the size specifiers in the _decimalPrefixes or _binaryPrefixes lists combined with a 'b' or 'B'. For example, to specify 640 kilobytes, you could pass any of these parameters: spec="640kb" spec="640 kb" spec="640KB" spec="640 KB" spec="640 kilobytes" If you want to use spec to pass a bytes value, you can use the letter 'b' or 'B' or simply leave the specifier off and bytes will be assumed. """ if bytes and spec: raise SizeParamsError("only specify one parameter") if bytes is not None: if isinstance(bytes, (int, long, float, Decimal)): value = Decimal(bytes) elif isinstance(bytes, Size): value = Decimal(bytes.convertTo("b")) else: raise ValueError("invalid value for bytes param") elif spec: value = _parseSpec(spec) else: raise SizeParamsError("missing bytes= or spec=") # drop any partial byte value = value.to_integral_value(rounding=ROUND_DOWN) self = Decimal.__new__(cls, value=value) return self
def __new__(cls, *args, **kwargs): vals = list(args) vals[0] = str(args[0]) vals = tuple(vals) newobj = Decimal.__new__(cls, *vals, **kwargs) return newobj
def __new__(cls, value, currency=None): self = Decimal.__new__(cls, value) self.currency = currency return self
def __new__(cls, angle=0): """Aspect angle initialization (angle between 0 and 180).""" angle = float(angle) if angle < 0 or angle > 180: raise ValueError('Invalid angle %s.' % angle) return Decimal.__new__(cls, str(angle))
def __new__(cls, value="0", context=None): d = Dec.__new__(cls, value, context) return d.quantize(Dec('0.00000001'), rounding=ROUND_DOWN)
def __new__(cls, value="0", context=None): obj = Decimal.__new__(cls, value, context) obj.saved_string = value return obj
def __new__(self, amount, currency): return Decimal.__new__(self, amount)
def __new__(cls, value="0", context=None): cls.decimal3 = True return Decimal.__new__(cls, value=value or "0", context=context)
def __new__(cls, value): return Decimal.__new__(cls, value, cls.context)
def __new__(cls, amount): if amount is None: raise ValueError('Cannot convert None to currency') return Decimal.__new__(cls, cls._sanitize(amount))
def __new__(cls, amount): return Decimal.__new__(cls, cls._sanitize(amount))