def GetOrCreate(obj, path, constructor=dict):
  """Get or create the object by following the field names in the path.

  not exist, create the appropriate value.

  Args:
    obj: A dictionary representing a yaml dictionary
    path: A list of strings representing fields to follow.
    constructor: If the object at the end of the path does not exist, create an
      object using the constructor given.

  Returns:
    An object at found by following the path.
  """
  first, rest = path[0], path[1:]

  if rest:
    if first not in obj:
      obj[first] = dict()
    return GetOrCreate(obj[first], rest, constructor)
  else:
    if first not in obj:
      obj[first] = constructor()
    return obj[first]