def _AddHasFieldMethod(message_descriptor, cls):
 singular_fields = {}
 for field in message_descriptor.fields:
  if (field.label != _FieldDescriptor.LABEL_REPEATED):
   singular_fields[field.name] = field
 def HasField(self, field_name):
  try:
   field = singular_fields[field_name]
  except KeyError:
   raise ValueError(('Protocol message has no singular "%s" field.' % field_name))
  if (field.cpp_type == _FieldDescriptor.CPPTYPE_MESSAGE):
   value = self._fields.get(field)
   return ((value is not None) and value._is_present_in_parent)
  else:
   return (field in self._fields)
 cls.HasField = HasField

def _AddHasFieldMethod ( message_descriptor , cls ) : singular_fields = { } for field in message_descriptor . fields : if ( field . label != _FieldDescriptor . LABEL_REPEATED ) : singular_fields [ field . name ] = field def HasField ( self , field_name ) : try : field = singular_fields [ field_name ] except KeyError : raise ValueError ( ( 'Protocol message has no singular "%s" field.' % field_name ) ) if ( field . cpp_type == _FieldDescriptor . CPPTYPE_MESSAGE ) : value = self . _fields . get ( field ) return ( ( value is not None ) and value . _is_present_in_parent ) else : return ( field in self . _fields ) cls . HasField = HasField
helper for _addmessagemethods() .