code: def key_from_protobuf(pb):
      
      path_args = []
      for element in pb.path:
          path_args.append(element.kind)
          if element.id:  # Simple field (int64)
              path_args.append(element.id)
          # This is safe: we expect proto objects returned will only have
          # one of `name` or `id` set.
          if element.name:  # Simple field (string)
              path_args.append(element.name)
  
      project = None
      if pb.partition_id.project_id:  # Simple field (string)
          project = pb.partition_id.project_id
      namespace = None
      if pb.partition_id.namespace_id:  # Simple field (string)
          namespace = pb.partition_id.namespace_id
  
      return Key(*path_args, namespace=namespace, project=project)
docs: Factory method for creating a key based on a protobuf.
  
      The protobuf should be one returned from the Cloud Datastore
      Protobuf API.
  
      :type pb: :class:`.entity_pb2.Key`
      :param pb: The Protobuf representing the key.
  
      :rtype: :class:`google.cloud.datastore.key.Key`
      :returns: a new `Key` instance