def select_and_set_by_path(
      self, replacements_by_path: dict[KeyPath, Any]
  ) -> Any:
    """Selects subtrees and replaces them based on relative keypaths.

    Convenience method that combines `at_keypaths` and `set_by_path`.

    Args:
      replacements_by_path: A mapping from key paths to replacements. Key paths
        are relative to the current selected nodes.

    Returns:
      A modified version of the original tree, with replacements taken from the
      replacer.
    """

    def go(node):
      return (
          select(node)
          .at_keypaths(replacements_by_path.keys())
          .set_by_path(replacements_by_path)
      )

    return self.apply(go)
Selects subtrees and replaces them based on relative keypaths.

    Convenience method that combines `at_keypaths` and `set_by_path`.

    Args:
      replacements_by_path: A mapping from key paths to replacements. Key paths
        are relative to the current selected nodes.

    Returns:
      A modified version of the original tree, with replacements taken from the
      replacer.
    
penzai/core/selectors.py