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
  • removed (dict, optional) – Old keys and values that were just removed.

  • added (dict, optional) – New keys and values that were just added.

  • changed (dict, optional) – Updated keys and their previous values.

removed

Old keys and values that were just removed.

Type

dict

added

New keys and values that were just added.

Type

dict

changed

Updated keys and their previous values.

Type

dict

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

list

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.

clear()[source]

Remove all items from the dict.

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.

pop(key, value=<undefined>)[source]

Remove specified key and return the corresponding value. If key is not found, the default value is returned if given, otherwise KeyError is raised.

Parameters
  • key (A hashable object.) – Key to the dict item.

  • value (any) – Value to return if key is absent.

popitem()[source]

Remove and return some (key, value) pair as a tuple. Raise KeyError if dict is empty.

Returns

key_value – Some 2-tuple from the dict.

Return type

tuple

Raises

KeyError – If the dict is empty

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

name

The name of the attribute on the object.

Type

str

name_items

The name of the items event trait that the trait dict will fire when mutated.

Type

str

notifier(trait_dict, removed, added, changed)[source]

Fire the TraitDictEvent with the provided parameters.

Parameters
  • trait_dict (dict) – The complete dictionary.

  • removed (dict) – Dict of removed items.

  • added (dict) – Dict of added items.

  • changed (dict) – Dict of changed items.