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.

Source Distribution

GreenRocket-0.1.tar.gz (3.4 kB view details)

Uploaded Source

File details

Details for the file GreenRocket-0.1.tar.gz.

File metadata

  • Download URL: GreenRocket-0.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for GreenRocket-0.1.tar.gz
Algorithm Hash digest
SHA256 a7cf2774748dd1d2c257c85c69102e8b2ae8bbf4aeb70143cc6a2b33d1231cb9
MD5 9b4c092d02487d8c9bae313ebaf2cb0e
BLAKE2b-256 834c3e668724893e295502fa954d788891e1545a95241d3eda4c032a8ca84bf9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page