so_magic.utils.notification module

Typical subject/observers pattern implementation. You can see this pattern mentioned also as event/notification or broadcast/listeners.

Provides the Observer class, serving as the interface that needs to be implemented by concrete classes; the update method needs to be overrode. Concrete Observers react to the notifications/updates issued by the Subject they had been attached to.

Provides the Subject class, serving with mechanisms to subscribe/unsubscribe (attach/detach) observers and also with a method to “notify” all subscribers about events.

class so_magic.utils.notification.Observer[source]

Bases: so_magic.utils.notification.ObserverInterface, abc.ABC

abstract update(*args, **kwargs)None

Receive an update (from a subject); handle an event notification.

class so_magic.utils.notification.Subject(*args, **kwargs)[source]

Bases: so_magic.utils.notification.SubjectInterface

The Subject owns some important state and can notify observers.

Both the _state and _observers attributes have a simple implementation, but can be overrode to accommodate for more complex scenarios.

The observers/subscribers are implemented as a python list. In more complex scenarios, the list of subscribers can be stored more comprehensively (categorized by event type, etc.).

The subscription management methods provided are ‘attach’ and ‘detach’ to add or remove a subscriber respectively

add(*observers)[source]

Subscribe multiple observers at once.

attach(observer: so_magic.utils.notification.Observer)None[source]

Attach an observer to the subject; subscribe the observer.

detach(observer: so_magic.utils.notification.Observer)None[source]

Detach an observer from the subject; unsubscribe the observer.

notify()None[source]

Trigger an update in each subscriber/observer.

property state