Hi All,

Firstly, just wanted to thank Kenton and the Google team, PB2 is a
beautiful piece of work! Thanks heaps.

I will almost certainly go to some deep circle of Programmer's Hell
for this, but it might be useful for someone until the guys get a
chance to add text_mode message parsing functionality to the Python
API. There are almost certainly more elegant ways of doing this.


def parse_text_format(message_string, generated_message_type):
    """Parses the given Protobuf text_format into a new instance of
the given type."""

    # Should be defined globally somewhere
    PROTO_FILENAME = "person.proto"

    # Instance new message
    obj = generated_message_type()

    # Wrap the protoc command-line utility, expects that 'protoc'
should be on your PATH somewhere
    (stdout, stdin) = popen2.popen2("protoc %s --encode=%s" %
(PROTO_FILENAME, message_type.DESCRIPTOR.name), bufsize=1024)

    # Feed in the message_string in text_format

    # Read out the protoc-encoded binary format
    binary_string = stdout.read()

    # Parse the resulting binary representation.
    return obj


Assuming person.proto contains:

message Person {
    required string name = 1;


from person_pb2 import *
guido = parse_text_format("""name: "Guido"""", person)

Should give you a person object which you can use for nefarious

Kind regards,

Nicholas Reid
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 

Reply via email to