Observable

class Observable(basis: Module, realign: str, realign_args: tuple[Any, ...] | None, realign_kwargs: dict[str, Any] | None)[source]

Bases: object

Object to which monitors should be registered relative to a basis module.

Parameters:
  • basis (Module) – module which monitors should be registered relative to.

  • realign (str) – name of the method bound to basis which realigns attributes relative to it.

  • realign_args (tuple[Any, ...] | None) – positional arguments prepended to output of local_remap.

  • realign_kwargs (dict[str, Any] | None) – keyword arguments added to output of local_remap.

Important

Monitors are not stored in an Observable but are only weakly referenced, primarily for the purpose of monitoring monitors.

add_monitor(name: str, attr: str, constructor: MonitorConstructor, pool: Iterable[tuple[Observable, Mapping[str, Monitor]]] | None, /, **tags: Any) Monitor[source]

Creates or alias a monitor on the observable’s basis.

Parameters:
  • name (str) – name of the monitor to add.

  • attr (str) – dot-separated attribute path, relative to this observable, to monitor, when an empty-string, the observable is directly targeted.

  • constructor (MonitorConstructor) – partial constructor for the monitor to add.

  • pool (Iterable[tuple[Observable, Mapping[str, Monitor]]] | None) – pool to search for compatible monitor, always creates a new one if None. Defaults to None.

  • **tags (Any) – tags to determine if the monitor is unique amongst monitors with the same name, targeting the same attribute (aligned to the basis).

Returns:

added or retrieved monitor.

Return type:

Monitor

Important

If a monitor name is not a valid identifier, it cannot be accessed with dot-notation via monitors.

local_remap(attr: str) tuple[tuple[Any, ...], dict[str, Any]][source]

Locally remaps an attribute for pooled monitors.

This method should alias any local attributes being referenced as required. The callback realign given on initialization will accept the output of this as positional and keyword arguments.

Parameters:

attr (str) – dot-separated attribute relative to self, to realign.

Returns:

tuple of positional arguments and keyword arguments for realign method specified on initialization.

Return type:

tuple[tuple[Any, …], dict[str, Any]]

Raises:

NotImplementedError – this must be implemented by the subclass.

property monitors: MapAccessor

Access to monitors added to this observable.

Monitors available via this property need not have been added to a pool, but must have been added or retrieved through add_monitor().

Monitors are never explicitly removed, but if all references to them are destroyed, then they will no longer be accessible.

Returns:

attribute-like accessor for monitors.

Return type:

MapAccessor

realign_attribute(attr: str) str[source]

Realigns an attribute for pooled monitors.

The class which acts as a factory for Observable should, provide a means to set the nested attribute path relative to Observable, relative to itself instead.

Parameters:

attr (str) – dot-separated attribute relative to self, to realign.

Returns:

dot-chained nested attribute name relative to the parent of self.

Return type:

str