For example, I have proto-file File.proto:
enum Test {
ONE = 1;
TWO = 2;
}
I generate file File_pb2.py with protoc from File.proto.
I want in a python-code get string “ONE” (that corresponds to the name of File_pb2.ONE) by value 1 (that corresponds to the value of File_pb2.ONE) from generated file File_pb2.py without defining my own dictionaries. How can I do that?
Assuming the generated python is located in File_pb2.py code Try this:
In your case, this should give
'ONE'The reverse is :
will give
1.EDIT: As correctly pointed by @dyoo in the comments, a new method was later introduced in protobuf library:
EDIT: This has changed again in proto3. Now the Name() and Value() methods belong to the EnumTypeWrapper class so they can be accessed like: