MCPcopy Create free account
hub / github.com/ipython/traitlets / directional_link

Class directional_link

traitlets/traitlets.py:362–426  ·  view source on GitHub ↗

Link the trait of a source object with traits of target objects. Parameters ---------- source : (object, attribute name) pair target : (object, attribute name) pair transform: callable (optional) Data transformation between source and target. Examples --------

Source from the content-addressed store, hash-verified

360
361
362class directional_link:
363 """Link the trait of a source object with traits of target objects.
364
365 Parameters
366 ----------
367 source : (object, attribute name) pair
368 target : (object, attribute name) pair
369 transform: callable (optional)
370 Data transformation between source and target.
371
372 Examples
373 --------
374 >>> class X(HasTraits):
375 ... value = Int()
376
377 >>> src = X(value=1)
378 >>> tgt = X(value=42)
379 >>> c = directional_link((src, "value"), (tgt, "value"))
380
381 Setting source updates target objects:
382 >>> src.value = 5
383 >>> tgt.value
384 5
385
386 Setting target does not update source object:
387 >>> tgt.value = 6
388 >>> src.value
389 5
390
391 """
392
393 updating = False
394
395 def __init__(self, source: t.Any, target: t.Any, transform: t.Any = None) -> None:
396 self._transform = transform if transform else lambda x: x
397 _validate_link(source, target)
398 self.source, self.target = source, target
399 self.link()
400
401 def link(self) -> None:
402 try:
403 setattr(
404 self.target[0],
405 self.target[1],
406 self._transform(getattr(self.source[0], self.source[1])),
407 )
408 finally:
409 self.source[0].observe(self._update, names=self.source[1])
410
411 @contextlib.contextmanager
412 def _busy_updating(self) -> t.Any:
413 self.updating = True
414 try:
415 yield
416 finally:
417 self.updating = False
418
419 def _update(self, change: t.Any) -> None:

Callers 4

test_connect_sameMethod · 0.90
test_tranformMethod · 0.90
test_link_differentMethod · 0.90
test_unlink_linkMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_connect_sameMethod · 0.72
test_tranformMethod · 0.72
test_link_differentMethod · 0.72
test_unlink_linkMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…