Re: [protobuf] Dynamic messages in Python

1,976 views
Skip to first unread message

Jason Hsueh

unread,
Sep 10, 2012, 10:50:53 PM9/10/12
to Jan Koriťák, Protocol Buffers
This feature was just implemented internally, and I believe it should make the next release (2.5.0). It's in progress, but not quite ready to hit svn yet.


On Thu, Aug 30, 2012 at 6:53 AM, Jan Koriťák <je...@pudr.com> wrote:
Hello,

I am using Python to write a tool to control our backends which communicate using protocol buffers.

We would like our tool to be independent on current version of backends and therefore independent on .proto files that were used to build the particular version of the backend.

Therefore the tool receives serialized FileDescriptorSet over network, which should allow to create messages from the same descriptors, that are used at the backend side.

This is possible to implement in C++ (and we actually use it in another component) using DynamicMessage. (Create DescriptorPool, load all the FileDescriptors to the pool using BuildFile(), and then retrieve Descriptor from the pool. Then it is possible to create message using DynamicMessageFactory)

However, after thorough research on the web and of the source code of protocol buffers python bindings, I couldn't figure out how to do this (version 2.4.0). 

I was able to deserialize FileDescriptorSet, iterate over FileDescriptorProtos and get DescriptorProto object. However I didn't find a way to construct Descriptor objects, which are needed to create classes for message types(with GeneratedProtocolMessageType as a metaclass).

I tried another way, when I found some access to C++ library API in google.protobuf.internal (which is probably not a good idea to use). I tried to indirectly use the C++ descriptor pool by cpp_message.BuildFile(), but there is no way to get the Descriptor object back from the pool (it would be C++ object anyway). However there is a method cpp_message.NewCMessage, which can construct a message using descriptors from the pool. I was able to construct messages that way, but I was unable to set any fields. Only method that appeared usable was SetScalar, which requires C++ decriptor, which I was unable to get. Not to mention all of the Python magic is gone when dealing directly with the C++ API.

Is there any way to create messages of arbitrary type, which is distributed over network, in Python ? I see those possibilities in both Java and C++, is Python library lacking such funcionality ?

Thank you very much, hope I was clear enough.
Best regards,¨

Jan Koritak

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/LEFgfPpImWEJ.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.

Jason Hsueh

unread,
Sep 25, 2012, 10:44:39 PM9/25/12
to JP Brahma, Protocol Buffers, Jan Koriťák
This has hit the repository but 2.5.0 release isn't finalized yet. You can play around with it in trunk though: http://code.google.com/p/protobuf/source/browse/#svn%2Ftrunk%2Fpython%2Fgoogle%2Fprotobuf


On Tue, Sep 25, 2012 at 12:14 AM, JP Brahma <jpbr...@gmail.com> wrote:
Hi,
    I am also looking for this feature. I saw the SVN update to CHANGES.txt.
    Can you please confirm if this has been committed to the public svn ?

Thanks
To view this discussion on the web visit https://groups.google.com/d/msg/protobuf/-/L9mSM7H-GywJ.

Tomas Palenicek

unread,
Dec 19, 2013, 9:29:25 AM12/19/13
to prot...@googlegroups.com
Hello,

2.5.0 is out for a while and requested features are mentioned within the changelog. Is there any usage example anywhere please?

Is announced "Added ParseMessage(descriptor, bytes) to generate a new Message instance from a descriptor and a byte string" a wrapper to simplify usege of announced "Added support for dynamic message creation. DescriptorDatabase, DescriptorPool, and MessageFactory work like their C++ couterparts to simplify Descriptor construction from *DescriptorProtos, and MessageFactory provides a message instance from a Descriptor"? Or these two things are intended for different use cases?

Thanks, Tomas

Feng Xiao

unread,
Jan 9, 2014, 6:54:19 PM1/9/14
to Tomas Palenicek, Protocol Buffers
On Thu, Dec 19, 2013 at 6:29 AM, Tomas Palenicek <pale...@gmail.com> wrote:
Hello,

2.5.0 is out for a while and requested features are mentioned within the changelog. Is there any usage example anywhere please?
You can read the unit-tests for these features (e.g., descriptor_pool_test.py, message_factory_test.py). They should cover all APIs of these classes.
 

Is announced "Added ParseMessage(descriptor, bytes) to generate a new Message instance from a descriptor and a byte string" a wrapper to simplify usege of announced "Added support for dynamic message creation. DescriptorDatabase, DescriptorPool, and MessageFactory work like their C++ couterparts to simplify Descriptor construction from *DescriptorProtos, and MessageFactory provides a message instance from a Descriptor"?
Yes, I think it's just a thin wrapper implemented using MessageFactory.
 
Or these two things are intended for different use cases?

Thanks, Tomas

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages