napari.utils.events.EventedDict#

class napari.utils.events.EventedDict(data: Mapping[_K, _T] | None = None, basetype: type[_T] | Sequence[type[_T]] = ())[source]#

Bases: TypedMutableMapping[_K, _T]

Mutable dictionary that emits events when altered.

This class is designed to behave exactly like builtin dict, but will emit events before and after all mutations (addition, removal, and changing).

Parameters:
  • data (Mapping, optional) – Dictionary to initialize the class with.

  • basetype (type of sequence of types, optional) – Type of the element in the dictionary.

Events:
  • changing (key (K)) – emitted before an item at key is changed
    • changed (key (K, old_value: T, value: T)) – emitted when item at key is changed from old_value to value

    • adding (key (K)) – emitted before an item is added to the dictionary with key

    • added (key (K, value: T)) – emitted after value was added to the dictionary with key

    • removing (key (K)) – emitted before key is removed from the dictionary

    • removed (key (K, value: T)) – emitted after key was removed from the dictionary

    • updated (key, K, value (T)) – emitted after value of key was changed. Only implemented by subclasses to give them an option to trigger some update after value was changed and this class did not register it. This can be useful if the basetype is not an evented object.

inherited-members:

Methods

clear()

copy()

Return a shallow copy of the dictionary.

get(k[,d])

items()

key(value)

Return first instance of value.

keys()

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem()

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[,d])

update([E, ]**F)

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values()

Attributes

Details

key(value: _T) _K | None[source]#

Return first instance of value.