def _key_id_or_name_n(key, index):
  """Internal helper function for key id and name transforms.

  Args:
    key: A datastore key.
    index: The depth in the key to return; 0 is root, -1 is leaf.

  Returns:
    The id or name of the nth deep sub key in key.
  """
  if not key:
    return None
  path = key.to_path()
  if not path:
    return None
  path_index = (index * 2) + 1
  return path[path_index]