Overview

pblight - lightweight protocol buffer library

Overview

pblight implements parts of the Google protocol buffer spec[1]. It lets you define message, create message objects, serialize message objects to the pb wire format, and parse the wire format back into objects.

Pros:

  • Class based syntax for defining messages. Looks similar to the Dango ORM model class format
  • Treats the code as the authoritative definition of messages, rather than defining messages in a .proto file. It's possible to emit .proto files from the code instead
  • Very light weight. Less than 300 lines
  • Does not rely on dynamically generating source code, so no need to regenerate files whenever a message is changed.
  • Easy to add custom data types

Cons:

  • Does not implement the complete pb spec
  • Not very well tested yet
  • Can not generate objects from .proto files. (might in the future)

Example

This example creates a message to represent an instant message, serializes it and reconstructs it.

import time import pblight as pb

class InstantMessage(pb.ProtoBufMessage):
senttime = pb.Int32(1) sender = pb.String(2) recipient = pb.String(3, type="repeated") # msg can be sent to many attachment = pb.Bytes(4, type="optional") # can attach files to msg
msg = InstantMessage(senttime=time.time(),
sender="Rune")
for name in ["David", "Sverre]:
msg.recipient.append(name) : repeated fields are plain old lists.

data = msg.serialize() # send this off on a socket or something # Assume we got a reply back on a socket into replydata

reply = InstantMessage.parse(replydata) # reply is instance of InstantMessage.

Contact

The module is maintaned by Rune F. Halvorsen <runefh@gmail.com>. The project resides at http://bitbucket.org/runeh/pblight

[1] http://code.google.com/apis/protocolbuffers/docs/encoding.html