IncrementableAttrManager

class PlasmaCalcs.tools.oop_tools.manage_attrs.IncrementableAttrManager(obj, default=0, *, step=1)

Bases: object

an incrementable attribute, and methods to manage other attributes when it is incremented or decremented.

Methods

__getstate__()

return state for pickling.

__int__()

return int(self.value)

__setstate__(state)

set state from pickling.

clear()

set self.value to self.default, and empty self._using_at.

increment()

context manager which increments self.value upon entry, and restores old value upon exit.

use_obj_attrs_at(value, *[, ...])

tell self to set attrs of obj when self.value == value.

use_obj_attrs_at_next(**attrs_and_values)

tell self to set attrs of obj when self.increment() is next entered.

using_obj_attrs_at(value, **attrs_and_values)

context manager which tells self to set attrs of obj when self.value == value.

using_obj_attrs_at_next(**attrs_and_values)

context manager which tells self to set attrs of obj when self.increment() is next entered.

_on_decremented(prev, new)

called when self.value is decremented, i.e. when exiting the 'incremented' context.

_on_incremented(prev, new)

called when self.value is incremented, i.e. when entering the 'incremented' context.

_remove_commands_after_decrementing_to(value)

remove commands from self.using_at after decrementing self to value.

Attributes

_unset_sentinel

incrementable_attr_manager_value

alias to value

obj

gets self._obj(), or just self._obj if not self._obj_is_weakref.

__getstate__()

return state for pickling. (pickle can’t handle weakrefs. pickling is required by multiprocessing.)

__int__()

return int(self.value)

__setstate__(state)

set state from pickling. (pickle can’t handle weakrefs. pickling is required by multiprocessing.)

_on_decremented(prev, new)

called when self.value is decremented, i.e. when exiting the ‘incremented’ context.

_on_incremented(prev, new)

called when self.value is incremented, i.e. when entering the ‘incremented’ context.

_remove_commands_after_decrementing_to(value)

remove commands from self.using_at after decrementing self to value.

Removes any (context, rmv) pair for which rmv==value.
If any of these contexts have not been exited, raise AssertionError.
clear()

set self.value to self.default, and empty self._using_at.

increment()

context manager which increments self.value upon entry, and restores old value upon exit.

CAUTION: this is a context manager; it will not increment self.value until entered,
via “with” statement, e.g. “with self.increment(): …”.
property incrementable_attr_manager_value

alias to value

property obj

gets self._obj(), or just self._obj if not self._obj_is_weakref.

use_obj_attrs_at(value, *, remove_after_decrementing_to=UNSET, **attrs_and_values)

tell self to set attrs of obj when self.value == value.

More precisely:
- when self incremented OR decremented to value,
UsingAttrs(self.obj, attrs_and_values).enter_handle_attrs()
- when self incremented beyond value OR decremented beyond value,
UsingAttrs(…).exit_handle_attrs()
remove_after_decrementing_to: UNSET, None, or int
- when self decremented to remove_after_decrementing_to,
also remove these “use_obj_attrs_at” commands.
Note: this should always be < value, otherwise will raise ValueError.
UNSET –> use self.default.
None –> never remove these commands as a result of decrementing.
int –> remove these commands as soon as value is decremented to this value.
Note: to decrease chances of mistakes / ambiguity,
raise ValueError if value <= self.value, or if value <= remove_after_decrementing_to
value <= self.value, OR
value <= remove_after_decrementing_to.
(This method is only responsible for putting these attrs and values into self._using_at;
the “real work” happens in self.incremented, _on_incremented, and _on_decremented.)
use_obj_attrs_at_next(**attrs_and_values)

tell self to set attrs of obj when self.increment() is next entered.

Equivalent to self.use_obj_attrs_at(self.value + self.step, **attrs_and_values),
with remove_after_decrementing_to = self.value.
using_obj_attrs_at(value, **attrs_and_values)

context manager which tells self to set attrs of obj when self.value == value.

Equivalent to calling self.use_obj_attrs_at(…) upon entry (and doing nothing upon exit).
using_obj_attrs_at_next(**attrs_and_values)

context manager which tells self to set attrs of obj when self.increment() is next entered.

Equivalent to calling self.use_obj_attrs_at_next(…) upon entry (and doing nothing upon exit).