Fix up example for python3

This commit is contained in:
Jason Paryani
2013-07-07 02:31:50 -07:00
parent 6c84e7c74f
commit 2a4a183b7f
3 changed files with 14 additions and 68597 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,8 +1,11 @@
from __future__ import print_function
import os
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)
@@ -24,36 +27,37 @@ def writeAddressBook(fd):
bobPhones = bob.initPhones(2)
bobPhones[0].number = "555-4567"
bobPhones[0].type = 'home'
bobPhones[1].number = "555-7654"
bobPhones[1].number = "555-7654"
bobPhones[1].type = addressbook.Person.PhoneNumber.Type.WORK
bob.employment.unemployed = None # This is definitely bad, syntax will change at some point
bob.employment.unemployed = None # This is definitely bad, syntax will change at some point
capnp.writePackedMessageToFd(fd, message)
f = open('example', 'w')
writeAddressBook(f.fileno())
def printAddressBook(fd):
message = capnp.PackedFdMessageReader(f.fileno())
addressBook = message.getRoot(addressbook.AddressBook)
for person in addressBook.people:
print person.name, ':', person.email
print(person.name, ':', person.email)
for phone in person.phones:
print phone.type, ':', phone.number
print(phone.type, ':', phone.number)
which = person.employment.which()
print which
print(which)
if which == addressbook.Person.Employment.Which.UNEMPLOYED:
print 'unemployed'
print('unemployed')
elif which == addressbook.Person.Employment.Which.EMPLOYER:
print 'employer:', person.employment.employer
print('employer:', person.employment.employer)
elif which == addressbook.Person.Employment.Which.SCHOOL:
print 'student at:', person.employment.school
print('student at:', person.employment.school)
elif which == addressbook.Person.Employment.Which.SELF_EMPLOYED:
print 'self employed'
print
print('self employed')
print()
f = open('example', 'r')
printAddressBook(f.fileno())