Change how init works
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
f = open('example', 'w')
|
||||
writeAddressBook(f.fileno())
|
||||
|
||||
f = open('example', 'r')
|
||||
printAddressBook(f.fileno())
|
||||
|
||||
Reference in New Issue
Block a user