def decimal_new(cls, value=None): if value is None: return cls.numberWithInt_(0) else: if isinstance(value, NSDecimal): return cls.decimalNumberWithDecimal_(value) elif isinstance(value, NSDecimalNumber): return cls.decimalNumberWithDecimal_(value.decimalValue()) elif isinstance(value, float): return cls.numberWithDouble_(value) elif isinstance(value, str): value = NSDecimal(value) return cls.decimalNumberWithDecimal_(value) else: # The value is either an integer, or # invalid (and numberWithLongLong_ wil raise # TypeError) try: return cls.numberWithLongLong_(value) except ValueError: raise TypeError("Value is not a number")
# TypeError) try: return cls.numberWithLongLong_(value) except ValueError: raise TypeError("Value is not a number") addConvenienceForClass( "NSDecimalNumber", ( ("__new__", staticmethod(decimal_new)), ( "__add__", lambda self, other: NSDecimalNumber( operator.add(NSDecimal(self), other)), ), ( "__radd__", lambda self, other: NSDecimalNumber( operator.add(other, NSDecimal(self))), ), ( "__sub__", lambda self, other: NSDecimalNumber( operator.sub(NSDecimal(self), other)), ), ( "__rsub__", lambda self, other: NSDecimalNumber( operator.sub(other, NSDecimal(self))),