Change naming for functions to conform to PEP 8. Also deprecate old

read/write api.
This commit is contained in:
Jason Paryani
2013-09-01 20:10:57 -07:00
parent 6fcdf841e4
commit 1317527893
9 changed files with 270 additions and 257 deletions

View File

@@ -42,19 +42,22 @@ There is some basic documentation [here](http://jparyani.github.io/pycapnp/).
The examples directory has one example that shows off the capabilities quite nicely. Here it is, reproduced:
```python
from __future__ import print_function
import os
import capnp
addressbook = capnp.load('addressbook.capnp')
def writeAddressBook(fd):
message = capnp.MallocMessageBuilder()
addressBook = message.initRoot(addressbook.AddressBook)
people = addressBook.initPeople(2)
this_dir = os.path.dirname(__file__)
addressbook = capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
def writeAddressBook(file):
addresses = addressbook.AddressBook.new_message()
people = addresses.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"
@@ -63,29 +66,26 @@ 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"
bobPhones[1].number = "555-7654"
bobPhones[1].type = 'work'
bob.employment.unemployed = None
capnp.writePackedMessageToFd(fd, message)
addresses.write(file)
f = open('example', 'w')
writeAddressBook(f.fileno())
def printAddressBook(fd):
message = capnp.PackedFdMessageReader(f.fileno())
addressBook = message.getRoot(addressbook.AddressBook)
def printAddressBook(file):
addresses = addressbook.AddressBook.read(file)
for person in addressBook.people:
print person.name, ':', person.email
for person in addresses.people:
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 == 'unemployed':
print('unemployed')
@@ -95,10 +95,15 @@ def printAddressBook(fd):
print('student at:', person.employment.school)
elif which == 'selfEmployed':
print('self employed')
print
print()
f = open('example', 'r')
printAddressBook(f.fileno())
if __name__ == '__main__':
f = open('example', 'w')
writeAddressBook(f)
f = open('example', 'r')
printAddressBook(f)
```
## Common Problems