def ParseEnum(field, value):
enum_descriptor = field.enum_type<EOL>try:<EOL><INDENT>number = int(value, <NUM_LIT:0>)<EOL><DEDENT>except ValueError:<EOL><INDENT>enum_value = enum_descriptor.values_by_name.get(value, None)<EOL>if enum_value is None:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' % (<EOL>enum_descriptor.full_name, value))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>enum_value = enum_descriptor.values_by_number.get(number, None)<EOL>if enum_value is None:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>' % (<EOL>enum_descriptor.full_name, number))<EOL><DEDENT><DEDENT>return enum_value.number<EOL>
Parse an enum value.

    The value can be specified by a number (the enum value), or by
    a string literal (the enum name).

    Args:
      field: Enum field descriptor.
      value: String value.

    Returns:
      Enum value number.

    Raises:
      ValueError: If the enum value could not be parsed.