Get enum as string in Python

11,369 views
Skip to first unread message

Mike

unread,
Aug 26, 2010, 2:07:30 AM8/26/10
to Protocol Buffers
I'm pretty sure this is possible, but I can't seem to figure it out.
There was another post about how to do this in C++, but I couldn't
figure out how to make it work in Python.

My message is similar to:

message SomeMessage {
enum MyType {
FIRST = 0;
SECOND = 1;
THIRD = 2;
}

required MyType type = 0;
}


How do I programmatically get the string "FIRST" back if someone
passes me this message with the 'type' field set to SomeMessage.FIRST?

Something along the lines of...
>>> print SomeMessage.EnumName(SomeMessage.FIRST)
FIRST

Thanks!

Jason Hsueh

unread,
Aug 26, 2010, 3:29:36 AM8/26/10
to Mike, Protocol Buffers
You can pull this from the descriptors:
my_enum_descriptor = SomeMessage.DESCRIPTOR.enums_by_name['MyType']
my_enum_descriptor.values_by_number[SomeMessage.FIRST].name



--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
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.


Mike

unread,
Aug 26, 2010, 8:51:59 PM8/26/10
to Protocol Buffers
Got it, Thanks!

In your first statement, I believe the method is actually
"enum_types_by_name" - at least that's what worked for me.

On Aug 26, 12:29 am, Jason Hsueh <jas...@google.com> wrote:
> You can pull this from the descriptors:
> my_enum_descriptor = SomeMessage.DESCRIPTOR.enums_by_name['MyType']
> my_enum_descriptor.values_by_number[SomeMessage.FIRST].name
>
> (See the docs on the descriptor module fromhttp://code.google.com/apis/protocolbuffers/docs/reference/python/ind...
> )
>
>
>
> On Wed, Aug 25, 2010 at 11:07 PM, Mike <mspie...@gmail.com> wrote:
> > I'm pretty sure this is possible, but I can't seem to figure it out.
> > There was another post about how to do this in C++, but I couldn't
> > figure out how to make it work in Python.
>
> > My message is similar to:
>
> > message SomeMessage {
> >  enum MyType {
> >    FIRST = 0;
> >    SECOND = 1;
> >    THIRD = 2;
> >  }
>
> >  required MyType type = 0;
> > }
>
> > How do I programmatically get the string "FIRST" back if someone
> > passes me this message with the 'type' field set to SomeMessage.FIRST?
>
> > Something along the lines of...
> > >>> print SomeMessage.EnumName(SomeMessage.FIRST)
> > FIRST
>
> > Thanks!
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Protocol Buffers" group.
> > To post to this group, send email to prot...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > protobuf+u...@googlegroups.com<protobuf%2Bunsubscribe@googlegroups.c om>
> > .

Jason Hsueh

unread,
Aug 26, 2010, 9:06:20 PM8/26/10
to Mike, Protocol Buffers
Oh, indeed enum_types_by_name is what you want - my bad!

To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages