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)?
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.
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.
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.
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.
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