On Mon, Apr 27, 2009 at 8:38 PM, Dan <danle...@gmail.com> wrote: > > Hello, > I've recently started using Google Protocol Buffers with Python on my > server. I'm writing code to convert my data source into a binary > payload, but I'm running into some snags and was wondering if there > was a better way I'm not seeing in the documentation. > > Basically, I want a more dynamic Python accessor for the sake of code > reusability. There are two use cases: > > 1. Message[string] instead of Message.string > > Right now, I can only change fields by using Message.string (e.g., > Person.name). However, I wish I could use Message[string] instead > (e.g., Person['name']), so that I could apply one method to multiple > fields.
You can use Python's standard getattr() and setattr() functions for this, e.g.: name = getattr(message, 'name') setattr(message, 'name', new_value) > 2. Message.Message2 = Message2 instead of having to set all internal > attributes > > As far as I can tell, the standard way to set inner message's fields > is to access it directory (e.g., Person.Address.city, > Person.Address.zipcode, etc.). I want to be able to write a method to > generate an Address message, then simply add it to the message (e.g., > I've got an Address addr, and I use Person.Address = addr). Use: message.sub_message.CopyFrom(other_message) The full Message interface, including CopyFrom(), is documented here: http://code.google.com/apis/protocolbuffers/docs/reference/python/google.protobuf.message.Message-class.html --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Protocol Buffers" group. To post to this group, send email to email@example.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 -~----------~----~----~----~------~----~------~--~---