def __new__(cls, *args, **kwargs): obj = super().__new__(cls, *args, **kwargs) if cls.__dunders_already_set: return obj for attr in OPERATOR_METHODS: try: operation = getattr(timedelta, attr) except AttributeError: pass else: setattr(cls, attr, perform_on_constant(operation)) cls.__dunders_already_set = True return obj
def test_if_right_operand_is_a_different_type_of_constant_it_uses_it_directly(self): class T1(Constant): pass class T2(Constant): pass c1 = T1(1, "one") c2 = T2(2, "Two") # even though values are of the same type, since they are different constants # they cannot be compared. operation = perform_on_constant(operator.add) with self.assertRaises(TypeError): assert operation(c1, c2)
def test_optional_arguments_can_be_provided(self): c1 = Constant(3, "foo") operation = perform_on_constant(pow) assert operation(c1, 3, 4) == 3
def test_if_left_operand_is_not_a_constant_it_uses_it_directly(self): c1 = Constant(1, "one") operation = perform_on_constant(operator.add) assert operation(2, c1) == 3
def test_if_both_operands_are_constants_it_uses_value_of_both(self): c1 = Constant(1, "one") c2 = Constant(2, "two") operation = perform_on_constant(operator.add) assert operation(c1, c2) == 3