This project aimed to the own flexible implementation of Protocol Buffers in pure Python.
Features
- Compatible with Google's one.
- Supports required, packed and packed repeated fields.
- Supports embedded messages.
- Supports streaming of several messages.
- Provides self-describing messages.
- Easily extensible.
How to Use
Fow now, there is full protobuf encoding implementation, so you can use the `encoding` module with full compatibility with the standard implementation.
All you you need to start is to download the module, unzip it and write:
Sample 1. Introduction
Assume you have the following definition:
First, you should create the message type:
Then, create a message and fill it with the appropriate data:
You can serialize this easily:
You also can deserialize this message with:
or with:
Easy enough :)
Sample 2. Required field
To add a missing field you should pass an additional `flags` parameter to `add_field` like this:
If you'll not fill a required field, then `ValueError` will be raised during serialization.
Sample 3. Repeated field
Do like this:
A input value of repeated field can be any iterable object. The loaded value will always be `list`.
Sample 4. Packed repeated field
Sample 5. Embedded messages
Consider the following definitions:
and:
To create an embedded field, pass `EmbeddedMessage` as the type of field and fill it like this:
Supported Data Types
There are the following data types supported for now:
Techniques
Streaming messages
The Protocol Buffer format is not self delimiting. But you can wrap you message type in `EmbeddedMessage` class and write/read it sequentially.
The other option is to use `protobuf.EofWrapper` that has a `limit` parameter in its constructor. The `EofWrapper` raises `EOFError` when the specified number of bytes is read.
Self-describing messages and TypeMetadata
There is no any description of the message type in a message itself. Therefore, if you want to send a self-described messages, you should send the a description of the message too.
There is a way... Look:
You can send your `bytes` anywhere and you'll got your message type on the other side!
add_field chaining
`add_field` return the message type itself, thus you can do so:
More info
See `protobuf` to see the API and `run-tests` modules to see more usage samples. The code is documented.
