Utils¶
A simple utility to import something by its string name.
- traitlets.import_item(name)¶
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
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)>
- traitlets.signature_has_traits(cls)¶
Return a decorated class with a constructor signature that contain Trait names as kwargs.
Links¶
- class traitlets.link(source, target, transform=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
>>> c = link((src, "value"), (tgt, "value")) >>> src.value = 5 # updates other objects as well
- class traitlets.directional_link(source, target, transform=None)¶
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
>>> c = directional_link((src, "value"), (tgt, "value")) >>> src.value = 5 # updates target objects >>> tgt.value = 6 # does not update source object