コード例 #1
0
def test_custom_promotions(T1, T2):
    a, b = T1(1), T2(2)

    # Prepare conversions
    set_conversion(T2, T1, lambda x: T1(x.data))
    set_promotion_rule(T1, T2, T1)

    @set_promotion(T1, int, restype=int)
    @set_promotion(T2, int, restype=int)
    def promote_to_int(x, y):
        return convert(x.data, int), y

    # Create examples and test
    assert_allsame(promote(a, b), (a, T1(2)))
    assert_allsame(promote(a, 2), (1, 2))
    assert_allsame(promote(a, b, 3), (1, 2, 3))
    assert_allsame(promote(3, b, a), (3, 2, 1))
コード例 #2
0
def test_failed_promotions():
    with pytest.raises(TypeError):
        promote("42", 42)

    with pytest.raises(RuntimeError):
        set_promotion_rule(float, int, int)

    with pytest.raises(RuntimeError):
        set_promotion_rule(int, float, int)

    with pytest.raises(RuntimeError):
        set_promotion(int, float, function=int)

    with pytest.raises(RuntimeError):
        set_promotion(float, int, function=int)

    with pytest.raises(RuntimeError):
        set_promotion_rule(str, str, str)

    with pytest.raises(RuntimeError):
        set_promotion(str, str, function=str)
コード例 #3
0
ファイル: mathtools.py プロジェクト: diegor2/FGAme
    def __rtruediv__(self, other):
        return type(self)(*(x / other for x in self))


class Vec2(FGAmeVec, Vec[2, float]):
    '''Vetor bidimensional'''


@set_conversion(list, Vec2)
@set_conversion(tuple, Vec2)
def _tuple2vec2(u):
    x, y = u
    return Vec2(x, y)


set_promotion_rule(Vec2, tuple, Vec2)
set_promotion_rule(Vec2, list, Vec2)


@add.overload([Vec2, tuple])
@add.overload([Vec2, list])
@add.overload([tuple, Vec2])
@add.overload([list, Vec2])
def add(u, v):
    x, y = u
    a, b = v
    return Vec2(x + a, y + b)


@sub.overload([Vec2, tuple])
@sub.overload([Vec2, list])