Utils#
A simple utility to import something by its string name.
- traitlets.import_item(name: str) Any #
Import and return
bar
given the stringfoo.bar
.Calling
bar = import_item("foo.bar")
is the functional equivalent of executing the codefrom foo import bar
.- Parameters:
name (string) – The fully qualified name of the module/package being imported.
- Returns:
mod – The module that was imported.
- Return type:
module object
- traitlets.signature_has_traits(cls: Type[T]) Type[T] #
Return a decorated class with a constructor signature that contain Trait names as kwargs.
This is a way to expand the signature of the HasTraits
class constructor. This
enables auto-completion of trait-names in IPython and xeus-python when having
Jedi>=0.15 by adding trait names with their default values in the constructor
signature.
Example:
from inspect import signature
from traitlets import HasTraits, Int, Unicode, signature_has_traits
@signature_has_traits
class Foo(HasTraits):
number1 = Int()
number2 = Int()
value = Unicode('Hello')
def __init__(self, arg1, **kwargs):
self.arg1 = arg1
super(Foo, self).__init__(**kwargs)
print(signature(Foo)) # <Signature (arg1, *, number1=0, number2=0, value='Hello', **kwargs)>
Links#
- class traitlets.link(source: Any, target: Any, transform: Any = None)#
Link traits from different objects together so they remain in sync.
- Parameters:
source ((object / attribute name) pair)
target ((object / attribute name) pair)
transform (iterable with two callables (optional)) – Data transformation between source and target and target and source.
Examples
>>> class X(HasTraits): ... value = Int()
>>> src = X(value=1) >>> tgt = X(value=42) >>> c = link((src, "value"), (tgt, "value"))
Setting source updates target objects: >>> src.value = 5 >>> tgt.value 5
- class traitlets.directional_link(source: Any, target: Any, transform: Any = None)#
Link the trait of a source object with traits of target objects.
- Parameters:
Examples
>>> class X(HasTraits): ... value = Int()
>>> src = X(value=1) >>> tgt = X(value=42) >>> c = directional_link((src, "value"), (tgt, "value"))
Setting source updates target objects: >>> src.value = 5 >>> tgt.value 5
Setting target does not update source object: >>> tgt.value = 6 >>> src.value 5