Phrogz Phrogz - 1 month ago 17
Python Question

Find enums listed in Python DESCRIPTOR for ProtoBuf

I have received a Google ProtoBuf using Python, and am attempting to compare the value for an enum to a string representation of it. Based on this and this I should be able to use something like

enum_values_by_name
to get the info I need. However, all the
enum*
related attributes are empty:

>>> type(my_message)
<class 'myObjects_pb2.myObject'>

>>> my_message
# nID: 53564
# nAge: 2
# type: OBJECT_CLASS_SIGN
# view: OBJECT_VIEW_FRONT

>>> my_message.type
# 1

>>> filter(lambda s: s.startswith('enum'), dir(my_message.DESCRIPTOR))
# ['enum_types', 'enum_types_by_name', 'enum_values_by_name']

>>> my_message.DESCRIPTOR.enum_types
# []

>>> my_message.DESCRIPTOR.enum_types_by_name
# {}

>>> my_message.DESCRIPTOR.enum_values_by_name
# {}


Perhaps it is related to the fact that my protobuf is defined in many files, and the enums I want are not defined in the main file which I'm importing (but which is used to decode
my_message
)?

Why am I getting these empty collections and (more importantly) how do find information about the enums?

Answer

I don't know why the DESCRIPTOR for the message includes enum attributes that are not populated. (This seems like a bug to me.) However, there are (at least) two solutions to this:

1) If you know the name of the file where the enums are defined, you can 'hack' get the enum value by name via:

# This is the root ProtoBuf definition
from mydir import rootapi_pb2 

# We happen to know that the enums are defined in myenums.proto
enum_file = rootapi_pb2.myenums__pb2 # NOTE: Extra Underscore!
enum_value = getattr(enum_file, 'OBJECT_CLASS_SIGN')

If you don't want to rely on this hack, however, you can eventually find the enum descriptor, and thus the value from the name, via:

my_message.DESCRIPTOR.fields_by_name['type'].enum_type.values_by_name['OBJECT_CLASS_SIGN'].number

Because that's so horrific, here it is wrapped up as a safe, re-usable function:

def enum_value(msg, field_name, enum_name):
    """Return the integer for the enum name of a field,
       or None if the field does not exist, is not an enum, or the
       enum does not have a value with the supplied name."""
    field = msg.DESCRIPTOR.fields_by_name.get(field_name,None)
    if field and field.enum_type:
        enum = field.enum_type.values_by_name.get(enum_name,None)
        return enum and enum.number

print(enum_value(my_message, 'type', 'OBJECT_CLASS_SIGN'))
# 1