MCPcopy
hub / github.com/google-deepmind/penzai / set_by_path

Method set_by_path

penzai/core/selectors.py:363–382  ·  view source on GitHub ↗

Replaces the selected subtree(s) based on their path(s). If you need both the value and the key, see ``.apply(fn, with_keypath=True)``. Args: replacer: A mapping from key paths to replacements, or a function that builds such a mapping. Passing ``self.selection_by_path`` w

(
      self, replacer: Mapping[KeyPath, Any] | Callable[[KeyPath], Any]
  )

Source from the content-addressed store, hash-verified

361 return self.selected_by_path
362
363 def set_by_path(
364 self, replacer: Mapping[KeyPath, Any] | Callable[[KeyPath], Any]
365 ) -> Any:
366 """Replaces the selected subtree(s) based on their path(s).
367
368 If you need both the value and the key, see
369 ``.apply(fn, with_keypath=True)``.
370
371 Args:
372 replacer: A mapping from key paths to replacements, or a function that
373 builds such a mapping. Passing ``self.selection_by_path`` will return
374 the original tree unchanged.
375
376 Returns:
377 A modified version of the original tree, with replacements taken from the
378 replacer.
379 """
380 if callable(replacer):
381 replacer = {k: replacer(k) for k in self.selected_by_path.keys()}
382 return self.apply(lambda k, v: replacer[k], with_keypath=True)
383
384 def select_and_set_by_path(
385 self, replacements_by_path: dict[KeyPath, Any]

Callers 5

set_sequenceMethod · 0.95
goMethod · 0.80
from_uncachedMethod · 0.80

Calls 1

applyMethod · 0.95