Giter Club home page Giter Club logo

greenrocket's Introduction

Green Rocket

Green Rocket is a simple and compact implementation of Observer (or Publish/Subscribe) design pattern via signals.

Create specific signal using base one:

>>> 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()

If you are using asyncio, you can also use coroutines as handlers and fire signal asynchronously using await Signal.afire() or yield from Signal().afire(). Method afire() works well with synchronous handlers too.

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

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

Unsubscribe handler:

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

The handler is subscribed using weak reference. So if you create and subscribe a handler in local scope (for example inside a generator), it will be unsubscribed automatically.

>>> def gen():
...     @MySignal.subscribe
...     def local_handler(signal):
...         print('local_handler: ' + repr(signal))
...     yield 1
...
>>> for value in gen():
...     MySignal(value=value).fire()
...
local_handler: MySignal(value=1)
base_handler: MySignal(value=1)
>>> import gc                    # PyPy fails the following test without
>>> _ = gc.collect()             # explicit call of garbage collector.
>>> MySignal(value=2).fire()
base_handler: MySignal(value=2)
>>> Signal.unsubscribe(base_handler)

As you can see above, signal constructor accepts keyword arguments. These arguments are available as signal's attributes:

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

Signal suppresses any exception which is raised on handler call. It uses logger named greenrocket from standard logging module to log errors and debug information.

The library also provides Watchman class as a convenient way for testing signals.

Create watchman for specific signal:

>>> from greenrocket import Watchman
>>> watchman = Watchman(MySignal)

Fire signal:

>>> MySignal(x=1).fire()

Test signal:

>>> watchman.assert_fired_with(x=1)
>>> watchman.assert_fired_with(x=2)          # DOCTEST: +ellipsis
Traceback (most recent call last):
  ...
AssertionError: Failed assertion on MySignal.x: 1 != 2
>>> watchman.assert_fired_with(x=1, y=2)     # DOCTEST: +ellipsis
Traceback (most recent call last):
  ...
AssertionError: MySignal has no attribute y

Watchman object saves each fired signal to its log:

>>> watchman.log
[MySignal(x=1)]
>>> MySignal(x=2).fire()
>>> watchman.log
[MySignal(x=1), MySignal(x=2)]

The method assert_fired_with tests the last signal from the log by default:

>>> watchman.assert_fired_with(x=2)

But you can specify which one to test:

>>> watchman.assert_fired_with(-2, x=1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.