1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
obsub
=====
|Version| |License|
Small python module that implements the observer pattern via a
decorator.
**Deprecation notice**
This module has been unmaintained since around 2014. The authors have
moved on to other alternatives to event handling. There is also
`observed <https://github.com/DanielSank/observed>`_ by @DanielSank, which
was partially inspired by *obsub*, however, has not seen many updates lately.
@milibopp has been writing with functional reactive programming (FRP), but
not for Python.
FRP is a higher-level abstraction than the observer pattern, that essentially
is a purely functional approach to unidirectional dataflow, composing your
programs of event stream transformations. Experience has shown, that is easier
to compose and to test than the raw observer pattern. A solid implementation in
Python is `RxPY <https://github.com/ReactiveX/RxPY>`_, part of the ReactiveX
project.
Description
-----------
This is based on a `thread on stackoverflow
<http://stackoverflow.com/questions/1904351/python-observer-pattern-examples-tips>`_
(the example of C#-like events by Jason Orendorff), so I don't take any
credit for the idea. I merely made a fancy module with documentation and
tests out of it, since I needed it in a bigger project. It is quite
handy and I've been using it in a couple of projects, which require some
sort of event handling.
Thus it is `licensed as
CC0 <http://creativecommons.org/publicdomain/zero/1.0/>`__, so basically
do-whatever-you-want to the extent legally possible.
Installation
------------
*obsub* is available on PyPI, so you can simply install it using
``pip install obsub`` or you do it manually using ``setup.py`` as with
any python package.
Usage
-----
The ``event`` decorator from the ``obsub`` module is used as follows:
.. code:: python
from obsub import event
# Define a class with an event
class Subject(object):
@event
def on_stuff(self, arg):
print('Stuff {} happens'.format(arg))
# Now define an event handler, the observer
def handler(subject, arg):
print('Stuff {} is handled'.format(arg))
# Wire everything up...
sub = Subject()
sub.on_stuff += handler
# And try it!
sub.on_stuff('foo')
You should now get both print messages from the event itself and the
event handler function, like so:
::
Stuff foo happens
Stuff foo is handled
Contribution and feedback
-------------------------
*obsub* is developed on `github <https://github.com/milibopp/obsub>`__.
If you have any questions about this software or encounter bugs, you're welcome
to open a `new issue on github <https://github.com/milibopp/obsub/issues/new>`__.
In case you do not want to use github for some reason, you can alternatively
send an email one of us:
- `Emilia Bopp <contact@ebopp.de>`__
- `André-Patrick Bubel <code@andre-bubel.de>`__
- `Thomas Gläßle <t_glaessle@gmx.de>`__
Feel free to contribute patches as pull requests as you see fit. Try to be
consistent with PEP 8 guidelines as far as possible and test everything.
Otherwise, your commit messages should start with a capitalized verb for
consistency. Unless your modification is completely trivial, also add a message
body to your commit.
Credits
-------
Thanks to Jason Orendorff on for the idea on stackoverflow. I also want
to thank @coldfix and @Moredread for contributions and feedback.
.. |Version| image:: https://img.shields.io/pypi/v/obsub.svg
:target: https://pypi.python.org/pypi/obsub/
:alt: Latest Version
.. |License| image:: https://img.shields.io/pypi/l/obsub.svg
:target: https://pypi.python.org/pypi/obsub/
:alt: License
|