Update README.md
This commit is contained in:
99
README.md
99
README.md
@@ -25,63 +25,62 @@ You also need a working version of the latest [Cython](http://cython.org/) insta
|
|||||||
At the moment, there is no documenation, but the library is almost a 1:1 clone of the [Capnproto C++ Library](http://kentonv.github.io/capnproto/cxx.html)
|
At the moment, there is no documenation, but the library is almost a 1:1 clone of the [Capnproto C++ Library](http://kentonv.github.io/capnproto/cxx.html)
|
||||||
|
|
||||||
The examples has one example that shows off the capabilities quite nicely. Here it is, reproduced:
|
The examples has one example that shows off the capabilities quite nicely. Here it is, reproduced:
|
||||||
|
```python
|
||||||
|
import capnp
|
||||||
|
addressbook = capnp.load('addressbook.capnp')
|
||||||
|
|
||||||
import os
|
def writeAddressBook(fd):
|
||||||
import capnp
|
message = capnp.MallocMessageBuilder()
|
||||||
this_dir = os.path.dirname(__file__)
|
addressBook = message.initRoot(addressbook.AddressBook)
|
||||||
addressbook = capnp.load(os.path.join(this_dir, 'addressbook.capnp'))
|
people = addressBook.initPeople(2)
|
||||||
|
|
||||||
def writeAddressBook(fd):
|
alice = people[0]
|
||||||
message = capnp.MallocMessageBuilder()
|
alice.id = 123
|
||||||
addressBook = message.initRoot(addressbook.AddressBook)
|
alice.name = 'Alice'
|
||||||
people = addressBook.initPeople(2)
|
alice.email = 'alice@example.com'
|
||||||
|
alicePhones = alice.initPhones(1)
|
||||||
|
alicePhones[0].number = "555-1212"
|
||||||
|
alicePhones[0].type = 'mobile'
|
||||||
|
alice.employment.school = "MIT"
|
||||||
|
|
||||||
alice = people[0]
|
bob = people[1]
|
||||||
alice.id = 123
|
bob.id = 456
|
||||||
alice.name = 'Alice'
|
bob.name = 'Bob'
|
||||||
alice.email = 'alice@example.com'
|
bob.email = 'bob@example.com'
|
||||||
alicePhones = alice.initPhones(1)
|
bobPhones = bob.initPhones(2)
|
||||||
alicePhones[0].number = "555-1212"
|
bobPhones[0].number = "555-4567"
|
||||||
alicePhones[0].type = 'mobile'
|
bobPhones[0].type = 'home'
|
||||||
alice.employment.school = "MIT"
|
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 = people[1]
|
capnp.writePackedMessageToFd(fd, message)
|
||||||
bob.id = 456
|
|
||||||
bob.name = 'Bob'
|
|
||||||
bob.email = 'bob@example.com'
|
|
||||||
bobPhones = bob.initPhones(2)
|
|
||||||
bobPhones[0].number = "555-4567"
|
|
||||||
bobPhones[0].type = 'home'
|
|
||||||
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
|
|
||||||
|
|
||||||
capnp.writePackedMessageToFd(fd, message)
|
f = open('example', 'w')
|
||||||
|
writeAddressBook(f.fileno())
|
||||||
|
|
||||||
f = open('example', 'w')
|
def printAddressBook(fd):
|
||||||
writeAddressBook(f.fileno())
|
message = capnp.PackedFdMessageReader(f.fileno())
|
||||||
|
addressBook = message.getRoot(addressbook.AddressBook)
|
||||||
|
|
||||||
def printAddressBook(fd):
|
for person in addressBook.people:
|
||||||
message = capnp.PackedFdMessageReader(f.fileno())
|
print person.name, ':', person.email
|
||||||
addressBook = message.getRoot(addressbook.AddressBook)
|
for phone in person.phones:
|
||||||
|
print phone.type, ':', phone.number
|
||||||
|
|
||||||
for person in addressBook.people:
|
which = person.employment.which()
|
||||||
print person.name, ':', person.email
|
print which
|
||||||
for phone in person.phones:
|
|
||||||
print phone.type, ':', phone.number
|
|
||||||
|
|
||||||
which = person.employment.which()
|
if which == addressbook.Person.Employment.Which.UNEMPLOYED:
|
||||||
print which
|
print 'unemployed'
|
||||||
|
elif which == addressbook.Person.Employment.Which.EMPLOYER:
|
||||||
|
print 'employer:', person.employment.employer
|
||||||
|
elif which == addressbook.Person.Employment.Which.SCHOOL:
|
||||||
|
print 'student at:', person.employment.school
|
||||||
|
elif which == addressbook.Person.Employment.Which.SELF_EMPLOYED:
|
||||||
|
print 'unemployed'
|
||||||
|
print
|
||||||
|
|
||||||
if which == addressbook.Person.Employment.Which.UNEMPLOYED:
|
f = open('example', 'r')
|
||||||
print 'unemployed'
|
printAddressBook(f.fileno())
|
||||||
elif which == addressbook.Person.Employment.Which.EMPLOYER:
|
```
|
||||||
print 'employer:', person.employment.employer
|
|
||||||
elif which == addressbook.Person.Employment.Which.SCHOOL:
|
|
||||||
print 'student at:', person.employment.school
|
|
||||||
elif which == addressbook.Person.Employment.Which.SELF_EMPLOYED:
|
|
||||||
print 'unemployed'
|
|
||||||
print
|
|
||||||
|
|
||||||
f = open('example', 'r')
|
|
||||||
printAddressBook(f.fileno())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user