From 59beea5108a0aeb5bed16e070783b0c541c962e4 Mon Sep 17 00:00:00 2001 From: Jason Paryani Date: Sun, 7 Jul 2013 13:00:03 -0700 Subject: [PATCH] Change how init works --- capnp/capnp.pyx | 6 ------ examples/example.py | 19 ++++++++++--------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/capnp/capnp.pyx b/capnp/capnp.pyx index 2c8d051..9251644 100644 --- a/capnp/capnp.pyx +++ b/capnp/capnp.pyx @@ -276,12 +276,6 @@ cdef class _DynamicStructBuilder: return self def __getattr__(self, field): - if field.startswith('init'): - field_name = field[4].lower() + field[5:] - try: - self._has(field_name) # We don't need to test bool value here, since it will throw an exception if the field is non-existant - return lambda size: self.init(field_name, size) - except ValueError: pass return toPython(self.thisptr.get(field)) cdef _setattrInt(self, field, value): diff --git a/examples/example.py b/examples/example.py index 6e902d8..4ebd5e6 100644 --- a/examples/example.py +++ b/examples/example.py @@ -5,17 +5,16 @@ import capnp this_dir = os.path.dirname(__file__) addressbook = capnp.load(os.path.join(this_dir, 'addressbook.capnp')) - def writeAddressBook(fd): message = capnp.MallocMessageBuilder() addressBook = message.initRoot(addressbook.AddressBook) - people = addressBook.initPeople(2) + people = addressBook.init('people', 2) alice = people[0] alice.id = 123 alice.name = 'Alice' alice.email = 'alice@example.com' - alicePhones = alice.initPhones(1) + alicePhones = alice.init('phones', 1) alicePhones[0].number = "555-1212" alicePhones[0].type = 'mobile' alice.employment.school = "MIT" @@ -24,7 +23,7 @@ def writeAddressBook(fd): bob.id = 456 bob.name = 'Bob' bob.email = 'bob@example.com' - bobPhones = bob.initPhones(2) + bobPhones = bob.init('phones', 2) bobPhones[0].number = "555-4567" bobPhones[0].type = 'home' bobPhones[1].number = "555-7654" @@ -33,9 +32,6 @@ def writeAddressBook(fd): capnp.writePackedMessageToFd(fd, message) -f = open('example', 'w') -writeAddressBook(f.fileno()) - def printAddressBook(fd): message = capnp.PackedFdMessageReader(f.fileno()) @@ -59,5 +55,10 @@ def printAddressBook(fd): print('self employed') print() -f = open('example', 'r') -printAddressBook(f.fileno()) + +if __name__ == '__main__': + f = open('example', 'w') + writeAddressBook(f.fileno()) + + f = open('example', 'r') + printAddressBook(f.fileno())