Skip to main content

A simple implementation of Observer pattern via Signals

Project description

Green Rocket is a simple implementation of Observer (or Publish/Subscribe)

design pattern using Signals.

Create specific signal from base class:

>>> from greenrocket import Signal
>>> class MySignal(Signal):
...     pass
...

Subscribe handler:

>>> @MySignal.subscribe
... def handler(signal):
...     print('handler: ' + repr(signal))
...

Fire signal:

>>> MySignal().fire()
handler: MySignal()

Note, that signal propagates over inheritance, i.e. all subscribes of base signal will be called when child signal fired:

>>> @Signal.subscribe
... def base_hadler(signal):
...     print('base_handler: ' + repr(signal))
...
>>> MySignal().fire()
handler: MySignal()
base_handler: MySignal()

Unsubscribe handler:

>>> MySignal.unsubscribe(handler)
>>> MySignal().fire()
base_handler: MySignal()

Any keyword argument passed to signal constructor becomes its attribute:

>>> s = Signal(a=1, b=2)
>>> s.a
1
>>> s.b
2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
GreenRocket-0.11.tar.gz (3.4 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page