def _do_cmps(cls, first, second, op): try: first = NumberValue(first) second = NumberValue(second) except ValueError: return op(getattr(first, 'value', first), getattr(second, 'value', second)) first_type = _conv_type.get(first.unit) second_type = _conv_type.get(second.unit) if first_type == second_type or first_type is None or second_type is None: return op(first.value, second.value) else: return op(first_type, second_type)
def _compare(self, other, op): if not isinstance(other, NumberValue): raise TypeError("Can't compare %r and %r" % (self, other)) # TODO this will need to get more complicated for full unit support first = NumberValue(self) second = NumberValue(other) first_type = _conv_type.get(first.unit) second_type = _conv_type.get(second.unit) if first_type == second_type or first_type is None or second_type is None: return op(first.value, second.value) else: return op(first_type, second_type)
def comparable(number1, number2): n1, n2 = NumberValue(number1), NumberValue(number2) type1 = _conv_type.get(n1.unit) type2 = _conv_type.get(n2.unit) return BooleanValue(type1 == type2)