Fix up example for python3
This commit is contained in:
19725
capnp/capnp.cpp
19725
capnp/capnp.cpp
File diff suppressed because it is too large
Load Diff
48862
capnp/schema.cpp
48862
capnp/schema.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,11 @@
|
|||||||
|
from __future__ import print_function
|
||||||
import os
|
import os
|
||||||
import capnp
|
import capnp
|
||||||
|
|
||||||
this_dir = os.path.dirname(__file__)
|
this_dir = os.path.dirname(__file__)
|
||||||
addressbook = capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
|
addressbook = capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
|
||||||
|
|
||||||
|
|
||||||
def writeAddressBook(fd):
|
def writeAddressBook(fd):
|
||||||
message = capnp.MallocMessageBuilder()
|
message = capnp.MallocMessageBuilder()
|
||||||
addressBook = message.initRoot(addressbook.AddressBook)
|
addressBook = message.initRoot(addressbook.AddressBook)
|
||||||
@@ -24,36 +27,37 @@ def writeAddressBook(fd):
|
|||||||
bobPhones = bob.initPhones(2)
|
bobPhones = bob.initPhones(2)
|
||||||
bobPhones[0].number = "555-4567"
|
bobPhones[0].number = "555-4567"
|
||||||
bobPhones[0].type = 'home'
|
bobPhones[0].type = 'home'
|
||||||
bobPhones[1].number = "555-7654"
|
bobPhones[1].number = "555-7654"
|
||||||
bobPhones[1].type = addressbook.Person.PhoneNumber.Type.WORK
|
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)
|
capnp.writePackedMessageToFd(fd, message)
|
||||||
|
|
||||||
f = open('example', 'w')
|
f = open('example', 'w')
|
||||||
writeAddressBook(f.fileno())
|
writeAddressBook(f.fileno())
|
||||||
|
|
||||||
|
|
||||||
def printAddressBook(fd):
|
def printAddressBook(fd):
|
||||||
message = capnp.PackedFdMessageReader(f.fileno())
|
message = capnp.PackedFdMessageReader(f.fileno())
|
||||||
addressBook = message.getRoot(addressbook.AddressBook)
|
addressBook = message.getRoot(addressbook.AddressBook)
|
||||||
|
|
||||||
for person in addressBook.people:
|
for person in addressBook.people:
|
||||||
print person.name, ':', person.email
|
print(person.name, ':', person.email)
|
||||||
for phone in person.phones:
|
for phone in person.phones:
|
||||||
print phone.type, ':', phone.number
|
print(phone.type, ':', phone.number)
|
||||||
|
|
||||||
which = person.employment.which()
|
which = person.employment.which()
|
||||||
print which
|
print(which)
|
||||||
|
|
||||||
if which == addressbook.Person.Employment.Which.UNEMPLOYED:
|
if which == addressbook.Person.Employment.Which.UNEMPLOYED:
|
||||||
print 'unemployed'
|
print('unemployed')
|
||||||
elif which == addressbook.Person.Employment.Which.EMPLOYER:
|
elif which == addressbook.Person.Employment.Which.EMPLOYER:
|
||||||
print 'employer:', person.employment.employer
|
print('employer:', person.employment.employer)
|
||||||
elif which == addressbook.Person.Employment.Which.SCHOOL:
|
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:
|
elif which == addressbook.Person.Employment.Which.SELF_EMPLOYED:
|
||||||
print 'self employed'
|
print('self employed')
|
||||||
print
|
print()
|
||||||
|
|
||||||
f = open('example', 'r')
|
f = open('example', 'r')
|
||||||
printAddressBook(f.fileno())
|
printAddressBook(f.fileno())
|
||||||
|
|||||||
Reference in New Issue
Block a user