I'm working with data from spinn3r, which consists of multiple different protobuf messages serialized into a byte stream:
http://code.google.com/p/spinn3r-client/wiki/Protostream
"A protostream is a stream of protocol buffer messages, encoded on the wire as length prefixed varints according to the Google protocol buffer specification. The stream has three parts: a header, the payload, and a tail marker."
This seems like a pretty standard use case for protobufs. In fact, protobuf core distribution provides CodedInputStream for both C++ and Java. But, it appears that protobuf does not provide such a tool for python -- the 'internal' tools are not setup for this kind of external use:
https://groups.google.com/forum/?fromgroups#!topic/protobuf/xgmUqXVsK-o
So... before I go and cobble together a python varint parser and tools for parsing a stream of different message types: does anyone know of any tools for this?
Why is it missing from protobuf? (Or am I just failing to find it?)
This seems like a big gap for protobuf, especially when compared to thrift's equivalent tools for both 'transport' and 'protocol'. Am I viewing that correctly?
It looks like the code in the other answer is potentially lifted from here. Check the licence before using this file but I managed to get it to read varint32
s using code such as this:
import sys
import myprotocol_pb2 as proto
import varint # (this is the varint.py file)
data = open("filename.bin", "rb").read() # read file as string
decoder = varint.decodeVarint32 # get a varint32 decoder
# others are available in varint.py
next_pos, pos = 0, 0
while pos < len(data):
msg = proto.Msg() # your message type
next_pos, pos = decoder(data, pos)
msg.ParseFromString(data[pos:pos + next_pos])
# use parsed message
pos += next_pos
print "done!"
This is very simple code designed to load messages of a single type delimited by varint32
s which describe the next message's size.
Update: It may also be possible to include this file directly from the protobuf library by using:
from google.protobuf.internal.decoder import _DecodeVarint32
I've implemented a small python package to serialize multiple protobuf messages into a stream and deserialize them from a stream. You can install it by pip
:
pip install pystream-protobuf
Here's a sample code writing two lists of protobuf messages in to a file:
import stream
with stream.open("test.gam", "wb") as ostream:
ostream.write(*objects_list)
ostream.write(*another_objects_list)
and then reading the same messages (e.g. Alignment messages defined in vg_pb2.py
) from the stream:
import stream
import vg_pb2
alns_list = []
with stream.open("test.gam", "rb") as istream:
for data in istream:
aln = vg_pb2.Alignment()
aln.ParseFromString(data)
alns_list.append(aln)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With