Reactions (objetto.reactions)

Reactions.

@objetto.reactions.reaction(func=None, priority=None)

Decorates an object’s method into a custom reaction. Reaction methods are called automatically when an action propagates up the hierarchy during the ‘PRE’ and ‘POST’ phases.

. code:: python

>>> from objetto.applications import Application
>>> from objetto.objects import Object, attribute
>>> from objetto.reactions import reaction
>>> class MyObject(Object):
...     value = attribute(int, default=0)
...
...     @reaction
...     def __on_received(self, action, phase):
...         if not self._initializing:
...             print(("LAST -", action.change.name, phase))
...
...     @reaction(priority=1)
...     def __on_received_first(self, action, phase):
...         if not self._initializing:
...             print(("FIRST -", action.change.name, phase))
...
>>> app = Application()
>>> my_obj = MyObject(app)
>>> my_obj.value = 42
('FIRST -', 'Update Attributes', <Phase.PRE: 'PRE'>)
('LAST -', 'Update Attributes', <Phase.PRE: 'PRE'>)
('FIRST -', 'Update Attributes', <Phase.POST: 'POST'>)
('LAST -', 'Update Attributes', <Phase.POST: 'POST'>)
Parameters
  • func (function) – Method to be decorated or None.

  • priority (int or None) – Priority.

Returns

Decorated custom reaction method or decorator.

Return type

objetto.reactions.CustomReaction

class objetto.reactions.CustomReaction(func, priority=None)

Custom method-like that gets called whenever an action is sent through the object.

Inherits from:
Parameters
  • func (function) – Function.

  • priority (int or None) – Priority.

__call__(obj, action, phase)

Run function.

Parameters
to_dict()

Convert to dictionary.

Returns

Dictionary.

Return type

dict[str, Any]

property func

Function.

Return type

function

class objetto.reactions.UniqueAttributes(*names, **incrementers)

Asserts that children have unique attributes within a collection. Initialize with attribute names and optional incrementer functions.

Inherits from:
Parameters
  • names (str) – Attribute names.

  • incrementers (function) – Incrementer functions.

__call__(obj, action, phase)

React to new children or children’s attribute changes.

Parameters
to_dict()

Convert to dictionary.

Returns

Dictionary.

Return type

dict[str, Any]

property names

Names.

Return type

tuple[str]

property incrementers

Incrementer functions.

Return type

objetto.states.DictState[str, function]

class objetto.reactions.LimitChildren(minimum=None, maximum=None)

Limit the number of children.

Inherits from:
Parameters
  • minimum (int or None) – Minimum.

  • maximum (int or None) – Maximum.

__call__(obj, action, phase)

React to atomic changes.

Parameters
to_dict()

Convert to dictionary.

Returns

Dictionary.

Return type

dict[str, Any]

property minimum

Minimum.

Return type

int or None

property maximum

Maximum.

Return type

int or None

class objetto.reactions.Limit(minimum=None, maximum=None)

Limit the number of values.

Inherits from:
Parameters
  • minimum (int or None) – Minimum.

  • maximum (int or None) – Maximum.

__call__(obj, action, phase)

React to atomic changes.

Parameters
  • obj – Object.

  • action – Action.

  • phase – Phase.

to_dict()

Convert to dictionary.

Returns

Dictionary.

Return type

dict[str, Any]

property minimum

Minimum.

Return type

int or None

property maximum

Maximum.

Return type

int or None