Skip to main content

obsub

Latest 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 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, part of the ReactiveX project.

Description

This is based on a thread on stackoverflow (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, 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:

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.

If you have any questions about this software or encounter bugs, you’re welcome to open a new issue on github.

In case you do not want to use github for some reason, you can alternatively send an email one of us:

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.

Changelog

v0.2

From a user perspective the preservation of function signatures and a couple of bug fixes are probably most relevant. Python 2.5 is no longer tested by continuous integration, though we try to avoid unnecessary changes that might break backwards compatibility.

In addition, there are quite a number of changes that mostly concern developers.

  • Function signatures are now preserved correctly by the event decorator. This is true only for python3. On python2 there is no support for default arguments, currently

  • Some fixes to memory handling and tests thereof. This includes a more generic handling of the garbage collection process within the test suite to make it pass on pypy, too.

  • Massive refactoring of test suite from one very long doctest to more focused unit tests.

  • The documentation has been converted from Markdown to reStructuredText, since it is compatible with both PyPI and GitHub.

  • Various improvements and some streamlining of the documentation.

  • Fix package name in license.

  • Continuous integration now includes coveralls.io support.

  • Support for Python 2.5 is no longer tested using Travis CI, since they have dropped support for this version.

v0.1.1

  • Add __all__ attribute to module

  • Fix a couple of documentation issues

v0.1

Initial release

Metadata

Release files for obsub 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for obsub 0.2.1
File Size Uploaded Provenance Actions
obsub-0.2.1.tar.gz 11.2 kB Without provenance Details for obsub-0.2.1.tar.gz

Release files / obsub-0.2.1.tar.gz

Download URL obsub-0.2.1.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
682e6044585d53261dfca822ab541860f40c18a80877b8adcefbf114acc31349
BLAKE2b-256 checksum
How to use checksums
c01b747913f5955c889040ab54138f5f2ad2193306c399f94457036bffb59d66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Security for obsub 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.1 This release

1 release file No provenance

0.1.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page