UsingAttrsSignaled

class PlasmaCalcs.tools.oop_tools.manage_attrs.UsingAttrsSignaled(obj, attrs_as_dict={}, signal_enter=None, signal_exit=None, signal_context_shift=None, signal_entered=None, signal_exited=None, signal_context_shifted=None, _unset_sentinel=ATTR_UNSET, squeeze=True, signal_target=None, **attrs_and_values)

Bases: UsingAttrs

context manager which (enter) sets attrs of obj; (exit) restores original values; (both) sends signals (e.g. to obj).

signals are sent at entry and exit, using whichever of the following are provided:

signal_enter: None or str
immediately BEFORE setting attrs, obj.signal_enter(current, new) during ENTRY
signal_exit: None or str
immediately BEFORE setting attrs, obj.signal_exit(current, new) during EXIT
signal_context_shift: None or str
immediately BEFORE setting attrs, obj.signal_context_shift(current, new) during ENTRY and EXIT.
signal_entered: None or str
immediately AFTER setting attrs, obj.signal_entered(current, new) during ENTRY
signal_exited: None or str
immediately AFTER setting attrs, obj.signal_exited(current, new) during EXIT
signal_context_shifted: None or str
immediately AFTER setting attrs, obj.signal_context_shifted(current, new) during ENTRY and EXIT.
where (current, new) are dicts for the attrs (or a single value if len(self.attrs)==1; see squeeze):
current = dict of {attr: obj.attr} for values before setting attrs.
new = dict of {attr: obj.attr} for values after setting attrs.
_unset_sentinel: any value, default ATTR_UNSET
used to indiate an attribute does not exist.
values in the dicts passed to signals will be _unset_sentinel if the attribute does not exist.
upon entry, delete any attrs with value _unset_sentinel (compared via ‘is’).
E.g. UsingAttrsSignaled(obj, _unset_sentinel=None, x=None) –> del obj.x upon entry.
squeeze: bool, default True
if True, and len(self.attrs)==1, then current and new will be single values, not dicts.
signal_target: None or object
if provided, use this object instead of self.obj, for sending signals.
— EXAMPLES —
# starting with obj.depth==0:
with UsingAttrsSignaled(obj.depth=1, signal_context_shifted=’_on_set_depth’):
# during entry, obj._on_set_depth(0, 1), immediately after setting obj.depth=1.
# during exit, obj._on_set_depth(depth_pre_exit, 0), immediately after setting obj.depth=0,
# where depth_pre_exit = obj.depth just before exiting.
# starting with obj.depth==0:
with UsingAttrsSignaled(obj.depth=1, signal_context_enter=’_on_increment_depth’, squeeze=False):
# during entry, obj._on_increment_depth({depth: 0}, {depth: 1}), immediately before setting obj.depth=1.
# starting with obj.depth==0, obj.x==3
with UsingAttrsSignaled(obj.depth=1, x=7, signal_context_exit=’_on_exit_depth_and_x’):
# during exit, first obj._on_exit_depth_and_x({depth: obj.depth, x: obj.x}, {depth: 0, x: 3}),
# immediately before setting obj.depth=0 and obj.x=3

Methods

enter_handle_attrs()

take all steps related to attrs which should occur upon entry.

exit_handle_attrs()

take all steps related to attrs which should occur upon exit.

inside_attrs()

tells whether currently inside attrs handling for self.

obj_current_attrs()

return dict of {attr: obj.attr} for current values of attrs.

obj_maybe_signal(name, current, new)

possibly send signal to obj, based on mode and signal_* attributes.

_repr_contents()

return list of contents to go in repr of self.

Attributes

_unset_sentinel

_repr_contents()

return list of contents to go in repr of self.

enter_handle_attrs()

take all steps related to attrs which should occur upon entry.

Provided separately from self.__enter__ for access to low-level functionality.
exit_handle_attrs()

take all steps related to attrs which should occur upon exit.

Provided separately from self.__exit__ for access to low-level functionality.
inside_attrs()

tells whether currently inside attrs handling for self. None if never entered; False if exited.

obj_current_attrs()

return dict of {attr: obj.attr} for current values of attrs.

obj_maybe_signal(name, current, new)

possibly send signal to obj, based on mode and signal_* attributes.

name: str, one of (‘enter’, ‘exit’, ‘context_shift’, ‘entered’, ‘exited’, ‘context_shifted’)
current: dict
dict of {attr: obj.attr} for values before setting attrs due to shifting context.
new: dict
dict of {attr: obj.attr} for values after setting attrs due to shifting context.