python example for reading multiple protobuf messages from a stream

前端 未结 3 2100
情歌与酒
情歌与酒 2021-02-05 09:14

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/P

3条回答
  •  萌比男神i
    2021-02-05 09:49

    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)
    

提交回复
热议问题