code: def add_key_path(key_proto, *path_elements):
    
    for i in range(0, len(path_elements), 2):
      pair = path_elements[i:i+2]
      elem = key_proto.path.add()
      elem.kind = pair[0]
      if len(pair) == 1:
        return  # incomplete key
      id_or_name = pair[1]
      if isinstance(id_or_name, (int, long)):
        elem.id = id_or_name
      elif isinstance(id_or_name, basestring):
        elem.name = id_or_name
      else:
        raise TypeError(
            'Expected an integer id or string name as argument %d; '
            'received %r (a %s).' % (i + 2, id_or_name, type(id_or_name)))
    return key_proto
docs: Add path elements to the given datastore.Key proto message.
  
    Args:
      key_proto: datastore.Key proto message.
      *path_elements: list of ancestors to add to the key.
          (kind1, id1/name1, ..., kindN, idN/nameN), the last 2 elements
          represent the entity key, if no terminating id/name: they key
          will be an incomplete key.
  
    Raises:
      TypeError: the given id or name has the wrong type.
  
    Returns:
      the same datastore.Key.
  
    Usage:
      >>> add_key_path(key_proto, 'Kind', 'name')  # no parent, with name
      datastore.Key(...)
      >>> add_key_path(key_proto, 'Kind2', 1)  # no parent, with id
      datastore.Key(...)
      >>> add_key_path(key_proto, 'Kind', 'name', 'Kind2', 1)  # parent, complete
      datastore.Key(...)
      >>> add_key_path(key_proto, 'Kind', 'name', 'Kind2')  # parent, incomplete
      datastore.Key(...)