@classmethod
  def FromMessage(cls, message):
    message_class = message.__class__

    # The CombinedContainer is a result of ResourceContainers.
    # Might need some better handling...
    if (message_class not in cls._proto_models.values() and
        message_class.__name__ != "CombinedContainer"):
      error_msg = ('The message is an instance of %s, which is a class this '
                   'EndpointsModel does not know how to process.' %
                   (message_class.__name__))
      raise TypeError(error_msg)

    entity_kwargs = {}
    alias_args = []

    for field in sorted(message_class.all_fields(),
                        key=lambda field: field.number):
      name = field.name
      value = getattr(message, name, None)
      if value is None:
        continue

      value_property = _VerifyProperty(cls, name)

      if field.repeated:
        if not isinstance(value, (list, tuple)):
          error_msg = ('Repeated attribute should be a list or tuple. '
                       'Received a %s.' % (value.__class__.__name__,))
          raise TypeError(error_msg)
        to_add = [FromValue(value_property, element) for element in value]
      else:
        to_add = FromValue(value_property, value)

      local_name = value_property._code_name
      if isinstance(value_property, EndpointsAliasProperty):
        alias_args.append((local_name, to_add))
      else:
        entity_kwargs[local_name] = to_add

    # Will not throw exception if a required property is not included. This
    # sort of exception is only thrown when attempting to put the entity.
    entity = cls(**entity_kwargs)

    # Set alias properties, will fail on an alias property if that
    # property was not defined with a setter
    for name, value in alias_args:
      setattr(entity, name, value)

    return entity
Converts a ProtoRPC message to an entity of the model class.
dataset/ETHPy150Open GoogleDeveloperExperts/experts-app-backend/endpoints_proto_datastore/ndb/model.py/EndpointsModel.FromMessage