Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Protocol Buffers for Python wrapper that would allow object annotation in Python code not it proto files?

So I have an object model in python. My application is in development stage so currently I can allow unstable serialization format. I want annotate my object hierarchy in Python code (if needed) or annotate nothing at all allowing some Protobuf wrapper to do it all dynamically.

It would be a real pain to describe each object in plain proto files and keep booth hierarchies up to date.

So I wonder if there is a way to use Protocol Buffers in python annotating\relying on reflection in python code (alike they do it in protobuf-net)?

like image 614
DuckQueen Avatar asked Mar 08 '16 12:03

DuckQueen


People also ask

What is protocol buffers in Python?

With protocol buffers, you write a . proto description of the data structure you wish to store. From that, the protocol buffer compiler creates a class that implements automatic encoding and parsing of the protocol buffer data with an efficient binary format.

How do you write a protobuf file in Python?

The protobuf compiler generates a class that implements automate encoding and parsing data with an efficient binary format. We need to specify the language through the command to create a class. The generated class allows us to create the element by instantiating new message according to a . proto file.

What are protocol buffers used for?

Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.


1 Answers

I had similar problem. I wanted to be able to store objects in database and at the same time serialize them using protobuf.

Approach I used was to define objects in SQLAlchemy and then, using SQLAlchemy reflection (inspect method) wrote *.proto file generator utility.

Then have to invoke protoc normally to get protobuf classes for those defined in SQLAlchemy objects. So after each change in SQLAlchemy object definition I have to invoke script building proto files and invoking protoc. This works surprisingly well.

Other option I was considering was to design object definition format and generators that could generate SQLAlchemy objects and protobuf objects. I decided against it because this object definition format should be able to contain all information to build SQLAlchemy objects (indexes, constraints, triggers) which was like redesigning what SQLAlchemy already done nicely but you may don't need such rich object definition and this approach might be perfect for you.

I took idea of developing my own object definition from book Pragmatic Programmer, where you can read more about it.

like image 104
omikron Avatar answered Oct 23 '22 03:10

omikron