Observers

state_of_things.observers

Maintain and notify a list of observers (see https://en.wikipedia.org/wiki/Observer_pattern).

Define a class that has all observable events, which will serve as a contract to let users easily see what events can occur and what data will be available.

Below is an example observer contract for key press and release:

class KeyObserver:
    def on_press(self, key_code: str):
        pass

    def on_release(self, key_code: str, seconds_pressed: float):
        pass

Observer implementations would then subclass the observer:

class LoggingObserver(KeyObserver):
    def on_press(self, key_code: str):
        print(f"Key pressed: {key_code}")

    def on_release(self, key_code: str, seconds_pressed: float):
        print(f"Key released: {key_code} after {seconds_pressed} seconds")

Observers are maintained and notified via an Observers instance:

observers = Observers()
observers.attach(LoggingObserver())

# trigger an on_press event for the 'w' key
observers.notify("on_press", "w")

# trigger an on_release event for the 'w' key that was held for 1.2
# seconds
observers.notify("on_release", "w", 1.2)
  • Author(s): Aaron Silinskas

class state_of_things.observers.Observers

Maintain a list of observers that will be notified when an event occurs.

attach(observer: object)

Attach an observer that will be notified of events that it supports.

Parameters:

observer (object) – the observer to attach.

detach(observer: object)

Detach an observer so that it will no longer be notified when events occur.

Parameters:

observer (object) – the observer to detach.

notify(event_name: str, *params: object)

Notify all observers that an event has occurred. Each attached observer with a defined function that matches the event name will called with the passed in event’s params.

Parameters:
  • event_name (str) – event that has occurred.

  • *params (object) – optional event data.