python类b()的实例源码

test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_primitive(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Point {
            x @0 :Int64;
            y @1 :Int64;
        }
        """
        mod = self.compile(schema)
        buf = b('\x01\x00\x00\x00\x00\x00\x00\x00'  # 1
                '\x02\x00\x00\x00\x00\x00\x00\x00') # 2
        #
        p = mod.Point(1, 2)
        assert p.x == 1
        assert p.y == 2
        assert p._seg.buf == buf
        #
        p = mod.Point(y=2, x=1)
        assert p.x == 1
        assert p.y == 2
        assert p._seg.buf == buf
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 41 收藏 0 点赞 0 评论 0
def test_enum(self):
        schema = """
        @0xbf5147cbbecf40c1;
        enum Color {
            red @0;
            green @1;
            blue @2;
            yellow @3;
        }
        struct Point {
            x @0 :Int64;
            y @1 :Int64;
            color @2 :Color;
        }
        """
        mod = self.compile(schema)
        buf = b('\x01\x00\x00\x00\x00\x00\x00\x00'  # 1
                '\x02\x00\x00\x00\x00\x00\x00\x00'  # 2
                '\x03\x00\x00\x00\x00\x00\x00\x00') # yellow
        #
        p = mod.Point(1, 2, mod.Color.yellow)
        assert p.x == 1
        assert p.y == 2
        assert p.color == mod.Color.yellow
        assert p._seg.buf == buf
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def test_void(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Point {
            x @0 :Int64;
            y @1 :Int64;
            z @2 :Void;
        }
        """
        mod = self.compile(schema)
        buf = b('\x01\x00\x00\x00\x00\x00\x00\x00'  # 1
                '\x02\x00\x00\x00\x00\x00\x00\x00') # 2
        #
        p = mod.Point(1, 2)
        assert p.x == 1
        assert p.y == 2
        assert p._seg.buf == buf
        py.test.raises(TypeError, "mod.Point(z=None)")
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_struct(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Point {
            x @0 :Int64;
            y @1 :Int64;
        }
        struct Foo {
            x @0 :Point;
        }
        """
        mod = self.compile(schema)
        p = mod.Point(1, 2)
        foo = mod.Foo(p)
        assert foo._seg.buf == b('\x00\x00\x00\x00\x02\x00\x00\x00'  # ptr to point
                                 '\x01\x00\x00\x00\x00\x00\x00\x00'  # p.x == 1
                                 '\x02\x00\x00\x00\x00\x00\x00\x00') # p.y == 2
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_list_of_text(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Foo {
            x @0 :List(Text);
        }
        """
        mod = self.compile(schema)
        foo = mod.Foo([b'foo', b'bar', b'baz'])
        expected = b('\x01\x00\x00\x00\x1e\x00\x00\x00'    # ptrlist
                     '\x09\x00\x00\x00\x22\x00\x00\x00'
                     '\x09\x00\x00\x00\x22\x00\x00\x00'
                     '\x09\x00\x00\x00\x22\x00\x00\x00'
                     'foo\x00\x00\x00\x00\x00'
                     'bar\x00\x00\x00\x00\x00'
                     'baz\x00\x00\x00\x00\x00')
        assert foo._seg.buf == expected
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_list_of_lists(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Foo {
            x @0 :List(List(Int8));
        }
        """
        mod = self.compile(schema)
        foo = mod.Foo([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
        expected = b('\x01\x00\x00\x00\x1e\x00\x00\x00'  # list<ptr> (3 items)
                     '\x09\x00\x00\x00\x1a\x00\x00\x00'  # list<8> (3 items)
                     '\x09\x00\x00\x00\x12\x00\x00\x00'  # list<8> (2 items)
                     '\x09\x00\x00\x00\x22\x00\x00\x00'  # list<8> (4 items)
                     '\x01\x02\x03\x00\x00\x00\x00\x00'
                     '\x04\x05\x00\x00\x00\x00\x00\x00'
                     '\x06\x07\x08\x09\x00\x00\x00\x00')
        assert foo._seg.buf == expected
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def test_group(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Point {
            position :group {
                x @0 :Int64;
                y @1 :Int64;
            }
            color @2 :Text;
        }
        """
        mod = self.compile(schema)
        p = mod.Point(position=(1, 2), color=b'red')
        assert p.position.x == 1
        assert p.position.y == 2
        assert p.color == b'red'
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def test_nested_group(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Shape {
            position :group {
                a :group {
                    x @0 :Int64;
                    y @1 :Int64;
                }
                b :group {
                    x @2 :Int64;
                    y @3 :Int64;
                }
            }
        }
        """
        mod = self.compile(schema)
        p = mod.Shape(position=((1, 2), (3, 4)))
        assert p.position.a.x == 1
        assert p.position.a.y == 2
        assert p.position.b.x == 3
        assert p.position.b.y == 4
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_group_named_params(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Point {
            position :group {
                x @0 :Int64;
                y @1 :Int64;
            }
            color :group {
                alpha @2 :Float64;
                name  @3 :Text;
            }
        }
        """
        mod = self.compile(schema)
        p = mod.Point(position=mod.Point.Position(x=1, y=2),
                      color=mod.Point.Color(alpha=1.0, name=b'red'))
        assert p.position.x == 1
        assert p.position.y == 2
        assert p.color.alpha == 1.0
        assert p.color.name == b'red'
test_ctor.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_group_named_params(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Point {
            position :group {
                x @0 :Int64 = 42;
                y @1 :Int64;
            }
            color :group {
                alpha @2 :UInt8 = 255;
                name  @3 :Text;
            }
        }
        """
        mod = self.compile(schema)
        assert mod.Point.Position() == (42, 0)
        assert mod.Point.Color() == (255, None)
        p = mod.Point(position=mod.Point.Position(y=2),
                      color=mod.Point.Color(name=b'red'))
        assert p.position.x == 42
        assert p.position.y == 2
        assert p.color.alpha == 255
        assert p.color.name == b'red'
test_repr.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 43 收藏 0 点赞 0 评论 0
def test_enum(self):
        schema = """
        @0xbf5147cbbecf40c1;
        enum Color {
            red @0;
            green @1;
            blue @2;
        }
        struct P {
            x @0 :Color;
            y @1 :Color;
        }
        """
        self.mod = self.compile(schema)
        buf = b'\x01\x00\x00\x00\x00\x00\x00\x00'
        p = self.mod.P.from_buffer(buf, 0, 1, 0)
        self.check(p, '(x = green, y = red)')
test_repr.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def test_text(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Person {
            name @0 :Text;
            surname @1 :Text;
        }
        """
        self.mod = self.compile(schema)
        p = self.mod.Person(name=None, surname=None)
        self.check(p, '()')
        #
        p = self.mod.Person(name=b"foo", surname=None)
        self.check(p, '(name = "foo")')
        #
        p = self.mod.Person(name=None, surname=b"bar")
        self.check(p, '(surname = "bar")')
        #
        p = self.mod.Person(name=b"foo", surname=b"bar")
        self.check(p, '(name = "foo", surname = "bar")')
test_repr.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def test_text_special_chars(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct P {
            txt @0 :Text;
        }
        """
        self.mod = self.compile(schema)
        p = self.mod.P(txt=b'double "quotes"')
        self.check(p, r'(txt = "double \"quotes\"")')
        #
        p = self.mod.P(txt=b"single 'quotes'")
        self.check(p, r'(txt = "single \'quotes\'")')
        #
        p = self.mod.P(txt=b"tricky \" '")
        self.check(p, r'(txt = "tricky \" \'")')
        #
        p = self.mod.P(txt=u'hellò'.encode('utf-8'))
        self.check(p, r'(txt = "hell\xc3\xb2")')
test_repr.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 47 收藏 0 点赞 0 评论 0
def test_struct(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Point {
            x @0 :Int64;
            y @1 :Int64;
        }
        struct Rectangle {
            a @0 :Point;
            b @1 :Point;
            empty @2 :Point;
        }
        """
        self.mod = self.compile(schema)
        p1 = self.mod.Point(1, 2)
        p2 = self.mod.Point(3, 4)
        r = self.mod.Rectangle(p1, p2, None)
        self.check(r, '(a = (x = 1, y = 2), b = (x = 3, y = 4))')
test_repr.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def test_list(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct Point {
            x @0 :Int64;
            y @1 :Int64;
        }
        struct P {
            ints @0 :List(Int64);
            structs @1 :List(Point);
            texts @2 :List(Text);
        }
        """
        self.mod = self.compile(schema)
        p = self.mod.P(ints=[1, 2, 3], structs=None, texts=None)
        self.check(p, '(ints = [1, 2, 3])')
        #
        p1 = self.mod.Point(1, 2)
        p2 = self.mod.Point(3, 4)
        p = self.mod.P(ints=None, structs=[p1, p2], texts=None)
        self.check(p, '(structs = [(x = 1, y = 2), (x = 3, y = 4)])')
        #
        p = self.mod.P(ints=None, structs=None, texts=[b'foo', b'bar', b'baz'])
        self.check(p, '(texts = ["foo", "bar", "baz"])')
test_repr.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def test_union(self):
        schema = """
        @0xbf5147cbbecf40c1;
        struct P {
            union {
                x @0 :Int64;
                y @1 :Void;
                z @2 :Text;
            }
        }
        """
        self.mod = self.compile(schema)
        p = self.mod.P.new_x(1)
        self.check(p, '(x = 1)')
        #
        p = self.mod.P.new_y()
        self.check(p, '(y = void)')
        #
        p = self.mod.P.new_z(b'hello')
        self.check(p, '(z = "hello")')
test_misc.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_listbuilder_bug(self):
        schema = """
            @0xbf5147cbbecf40c1;
            struct Bar {
                x @0 :Int64;
                y @1 :Int64;
            }

            struct Foo {
                name @0 :Text;
                bars @1 :List(Bar);
            }
        """
        mod = self.compile(schema)
        bars = [mod.Bar(1, 2)]
        foo = mod.Foo(b'name', bars)
        assert len(foo.bars) == 1
        assert foo.bars[0].x == 1
        assert foo.bars[0].y == 2
test_misc.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_listbuilder_null_ptrs(self):
        schema = """
            @0xbf5147cbbecf40c1;
            struct Bar {
                x @0 :Int64;
                y @1 :Int64;
                name @2 :Text;
            }

            struct Foo {
                bars @0 :List(Bar);
            }
        """
        mod = self.compile(schema)
        a = mod.Bar(1, 2, None)
        b = mod.Bar(3, 4, None)
        foo = mod.Foo([a, b])
        a1 = foo.bars[0]
        assert a1.x == 1
        assert a1.y == 2
        assert a1.name is None
test_misc.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_compact_structs(self):
        schema = """
            @0xbf5147cbbecf40c1;
            struct Person {
                name @0 :Text;
            }

            struct Foo {
                key @0 :Person;
            }
        """
        mod = self.compile(schema)
        buf = b('garbage0'
                '\x05\x00\x00\x00\x32\x00\x00\x00'  # ptr to name
                'garbage1'
                'dummy\x00\x00\x00')
        p = mod.Person.from_buffer(buf, 8, 0, 1)
        foo = mod.Foo(p)
        assert foo.key.name == b'dummy'
        # we check that the structure has been packed
        assert foo.key._data_offset == 8
        assert foo.key._seg.buf[8:] == b('\x01\x00\x00\x00\x32\x00\x00\x00'  # ptr to dummy
                                         'dummy\x00\x00\x00')
test_misc.py 文件源码 项目:capnpy 作者: antocuni 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def test_compact_struct_inside_list(self):
        schema = """
            @0xbf5147cbbecf40c1;
            struct Person {
                name @0 :Text;
                surname @1 :Text;
            }

            struct Town {
                people @0 :List(Person);
            }
        """
        mod = self.compile(schema)
        p1 = mod.Person(b'Mickey', b'Mouse')
        p2 = mod.Person(b'Donald', b'Duck')
        t = mod.Town([p1, p2])
        assert t.people[0].name == b'Mickey'
        assert t.people[0].surname == b'Mouse'
        assert t.people[1].name == b'Donald'
        assert t.people[1].surname == b'Duck'


问题


面经


文章

微信
公众号

扫码关注公众号