traits.trait_dict_object
Module¶
Classes¶
-
class
traits.trait_dict_object.
TraitDictEvent
(*, removed=None, added=None, changed=None)[source]¶ An object reporting in-place changes to a traits dict.
- Parameters
-
class
traits.trait_dict_object.
TraitDict
(*args, **kwargs)[source]¶ A subclass of dict that validates keys and values and notifies listeners of any change.
- Parameters
value (dict or iterable, optional) – The initial dict or an iterable containing key-value pairs.
key_validator (callable, optional) – Called to validate a key in the dict. The callable must accept a single key and return a validated key or raise a TraitError If not provided, all keys are considered valid.
value_validator (callable, optional) – Called to validate a value in the dict. The callable must accept a single value and return a validated value or raise a TraitError If not provided, all values are considered valid.
notifiers (list, optional) –
A list of callables with the signature:
notifier(trait_dict, removed, added, changed)
Where: ‘removed’ is a dict of key-values that are no longer in the dictionary. ‘added’ is a dict of new key-values that have been added. ‘changed’ is a dict with old values previously associated with the key.
-
key_validator
¶ Called to validate a key in the dict. The callable must accept a single key and return a validated key or raise a TraitError
- Type
callable
-
value_validator
¶ Called to validate a value in the dict. The callable must accept a single value and return a validated value or raise a TraitError
- Type
callable
-
notifiers
¶ A list of callables with the signature:
notifier(trait_dict, removed, added, changed)
Where: ‘removed’ is a dict of key-values that are no longer in the dictionary. ‘added’ is a dict of new key-values that have been added. ‘changed’ is a dict with old values previously associated with the key.
- Type
-
notify
(removed, added, changed)[source]¶ Call all notifiers.
This simply calls all notifiers provided by the class, if any. The notifiers are expected to have the signature:
notifier(trait_dict, removed, added, changed)
Any return values are ignored.
-
update
(other)[source]¶ Update the values in the dict by the new dict or an iterable of key-value pairs.
- Parameters
other (dict or iterable) – The dict from which values will be updated into this dict.
-
setdefault
(key, value=None)[source]¶ Returns the value if key is present in the dict, else creates the key-value pair and returns the value.
- Parameters
key (A hashable object.) – Key to the item.
-
class
traits.trait_dict_object.
TraitDictObject
(*args, **kwargs)[source]¶ A subclass of TraitDict that fires trait events when mutated. This is for backward compatibility with Traits 6.0 and lower.
This is used by the Dict trait type, and all values set into a Dict trait will be copied into a new TraitDictObject instance.
Mutation of the TraitDictObject will fire a “name_items” event with appropriate added, changed and removed values.
- Parameters
trait (CTrait instance) – The CTrait instance associated with the attribute that this dict has been set to.
object (HasTraits) – The object this dict belongs to. Can also be None in cases where the dict has been disconnected from its HasTraits parent.
name (str) – The name of the attribute on the object.
value (dict) – The dict of values to initialize the TraitDictObject with.
-
trait
¶ The CTrait instance associated with the attribute that this dict has been set to.
- Type
CTrait instance
-
object
¶ A callable that when called with no arguments returns the HasTraits object that this dict belongs to, or None if there is no such object.
- Type
callable