Beispiel #1
0
    class X(object):
        p1 = meta.Tuple(P(), repeat=slice(4, None, -2))
        p2 = meta.Tuple(P(), repeat=slice(1, 10, 0))
        p3 = meta.Tuple(repeat=slice(0, 10))

        def _get_(self, name):
            return self.__dict__.get(name)

        def _set_(self, name, value):
            self.__dict__[name] = value

        def _delete_(self, name):
            self.__dict__.pop(name, None)
Beispiel #2
0
    class X(object):
        p1 = meta.Tuple(P())
        p2 = meta.Tuple(P(), P())
        p3 = meta.Tuple(P(), repeat=Ellipsis)
        p4 = meta.Tuple(P(), P(), repeat=Ellipsis)
        p5 = meta.Tuple(P(), repeat=5)
        p6 = meta.Tuple(P(), repeat=slice(2, 6, 3))
        p7 = meta.Tuple(P(), repeat=[2, 5])
        p8 = meta.Tuple(P(), repeat=frozenset([2, 5]))

        def _get_(self, name):
            return self.__dict__.get(name)

        def _set_(self, name, value):
            self.__dict__[name] = value

        def _delete_(self, name):
            self.__dict__.pop(name, None)
Beispiel #3
0
 class X(meta.Entity):
     a = meta.Tuple(meta.Integer(), U())
Beispiel #4
0
 class X(meta.Entity):
     a = P(default=123)
     b = P(default=random.random)
     c = meta.Tuple(P(), default=tuple, repeat=Ellipsis)
Beispiel #5
0
 class Y(meta.Entity):
     x = meta.Tuple(X(required=False), repeat=Ellipsis)
Beispiel #6
0
 class X(meta.Entity):
     p = meta.Tuple(meta.Integer(), meta.String(view='secret'))
Beispiel #7
0
 class X(meta.Entity):
     p = meta.Tuple(meta.Integer(default=lambda: 1))
Beispiel #8
0
def test_tuple():
    class P(meta.Property):
        def _load_(self, value, context):
            return value

        def _dump_(self, value, context):
            return value

    class X(object):
        p1 = meta.Tuple(P())
        p2 = meta.Tuple(P(), P())
        p3 = meta.Tuple(P(), repeat=Ellipsis)
        p4 = meta.Tuple(P(), P(), repeat=Ellipsis)
        p5 = meta.Tuple(P(), repeat=5)
        p6 = meta.Tuple(P(), repeat=slice(2, 6, 3))
        p7 = meta.Tuple(P(), repeat=[2, 5])
        p8 = meta.Tuple(P(), repeat=frozenset([2, 5]))

        def _get_(self, name):
            return self.__dict__.get(name)

        def _set_(self, name, value):
            self.__dict__[name] = value

        def _delete_(self, name):
            self.__dict__.pop(name, None)

    X.p1._bind_('p1', X)
    X.p2._bind_('p2', X)
    X.p4._bind_('p4', X)

    x = X()

    assert x.p1 is None
    x.p1 = [123]
    assert isinstance(x.p1, tuple)
    assert x.p1 == (123, )
    x.p1 = (123, )
    assert x.p1 == (123, )
    with pytest.raises(ValueError):
        x.p1 = 123
    with pytest.raises(ValueError):
        x.p1 = [123, 456]

    assert x.p2 is None
    x.p2 = [123, 456]
    assert x.p2 == (123, 456)
    with pytest.raises(ValueError):
        x.p2 = [123]

    assert x.p4 is None
    x.p4 = [123] * 32
    assert x.p4 == (123, ) * 32
    x.p4 = []
    assert x.p4 == ()
    with pytest.raises(ValueError):
        x.p4 = [123] * 33

    check_tuple35678(X)

    with pytest.raises(TypeError):
        meta.Tuple(None)

    assert repr(meta.Tuple()) == 'Tuple()'
    assert repr(meta.Tuple(P())) == 'Tuple(P())'

    class X(object):
        p1 = meta.Tuple(P(), repeat=slice(4, None, -2))
        p2 = meta.Tuple(P(), repeat=slice(1, 10, 0))
        p3 = meta.Tuple(repeat=slice(0, 10))

        def _get_(self, name):
            return self.__dict__.get(name)

        def _set_(self, name, value):
            self.__dict__[name] = value

        def _delete_(self, name):
            self.__dict__.pop(name, None)

    X.p1._bind_('p1', X)
    X.p2._bind_('p2', X)
    X.p3._bind_('p3', X)

    x = X()

    for i in range(10):
        if i in {0, 2, 4}:
            x.p1 = [123] * i
        else:
            with pytest.raises(ValueError):
                x.p1 = [123] * i
        with pytest.raises(ValueError):
            x.p2 = [123] * i
        if i == 0:
            x.p3 = [123] * i
        else:
            with pytest.raises(ValueError):
                x.p3 = [123] * i