pblight - lightweight protocol buffer library
pblight implements parts of the Google protocol buffer spec. It lets you define message, create message objects, serialize message objects to the pb wire format, and parse the wire format back into objects.
- 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
- Does not implement the complete pb spec
- Not very well tested yet
- Can not generate objects from .proto files. (might in the future)
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(),
- 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.