You can use the type's descriptor to inspect the fields it contains and what
all their types are.  MyType.DESCRIPTOR is the descriptor for MyType.  Once
you've figured out what field you want to read/write, use getattr() /
setattr().

Note that the C++ and Java interfaces for this are somewhat cleaner and more
mature than the Python interface.  But you should be able to do it in Python
too.

On Thu, Dec 23, 2010 at 10:33 PM, BigBaaadBob <bigbaaad...@gmail.com> wrote:

> I have a couple of applications where I need to do some generic coding
> with protobufs.  In one case I have a gui that needs to display
> selected contents out of a message, and in another case I need to
> transcode from a different message format into a protobuf.  It is
> trivial to hand-write hardcoded stuff for both of these, but it
> violates the DRY principle, and is error prone, etc.
>
> What I want to do in both cases (the gui) is, by conventionally using
> the same names for the gui or other message fields as the fields in
> the protobuf messages, use introspection to do the field mapping.
>
> So, for example, given a list of PyQT widgets, I would use the widget
> names to go find a like-named field in the protobuf message.  But
> since the protobuf message is likely to be composite, and in fact it
> might be a subfield I need, I have to do some serious introspection.
> Also, I want to do this kind of at init time BEFORE I receive a
> message.
>
> For example, I might have a PyQT widget named tt_m1, and I want to get
> the .temperatures.tt_m1.val field out of the following protobuf
> message structure by setting up a mapping between the widget and the
> protobuf field at init time.  I'd like to do similar things in the
> opposite direction: given a selected set of protobuf fields, transcode
> from another message into protobuf contents.
>
> Suggestions?
>
> message Analog {
>  required double val = 1;
>  enum AnalogStatus {
>    SENSOR_NOMINAL     = 0;
>    SENSOR_UNDER_LIMIT = 1;
>    SENSOR_OVER_LIMIT  = 2;
>    SENSOR_FAULT       = 3;
>    SENSOR_NO_DATA     = 4;
>  }
>  required AnalogStatus status = 2;
>  optional double limit = 3;
> }
>
> message Temperatures {
>  optional Analog tt_m1 = 1;
>  optional Analog tt_m2 = 2;
>  optional Analog tt_m3 = 3;
>  optional Analog tt_m4 = 4;
> }
>
> message StatusUpdate {
>   ...stuff...
>   optional Temperatures temperatures = 7;
>   ...stuff...
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To post to this group, send email to proto...@googlegroups.com.
> To unsubscribe from this group, send email to
> protobuf+unsubscr...@googlegroups.com<protobuf%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/protobuf?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to