Skip to main content

Events

The C# language provides a handy way to declare, subscribe to and fire events. Technically, an event is a “slot” where callback functions (event handlers) can be attached to - a process referred to as subscribing to an event. Here is a handy package that encapsulates the core to event subscription and event firing and feels like a “natural” part of the language.

>>> def something_changed(reason):
...     print "something changed because %s" % reason

>>> from events import Events
>>> events = Events()
>>> events.on_change += something_changed

Multiple callback functions can subscribe to the same event. When the event is fired, all attached event handlers are invoked in sequence. To fire the event, perform a call on the slot:

>>> events.on_change('it had to happen')
'something changed because it had to happen'

By default, Events does not check if an event can be subscribed to and fired. You can predefine events by subclassing Events and listing them. Attempts to subscribe to or fire an undefined event will raise an EventsException.

>>> class MyEvents(Events):
...     __events__ = ('on_this', 'on_that', )

>>> events = MyEvents()

# this will raise an EventsException as `on_change` is unknown to MyEvents:
>>> events.on_change += something_changed

You can also predefine events for a single Events instance by passing an iterator to the constructor.

>>> events = Events(('on_this', 'on_that'))

# this will raise an EventsException as `on_change` is unknown to events:
>>> events.on_change += something_changed

Unsubscribing

There may come a time when you no longer want to be notified of an event. In this case, you unsubscribe in the natural counterpart to += by using -=.

# We no longer want to be notified, take us out of the event callback list
>>> events.on_change -= something_changed

You may also want to unsubscribe for memory management reasons. The Events() instance will hold a reference something_changed. If this is a member method of an object, and the lifetime of the Events() instance is greater than that object, it will keep it around longer than would be the normal case.

Documentation

Complete documentation is available at http://events.readthedocs.org

Installing

Events is on PyPI so all you need to do is:

pip install events

Testing

Just run:

python setup.py test

Or use tox to test the package under all supported Pythons: 2.7, 3.4+

Licenseing

Events is BSD licensed. See the LICENSE for details.

Contributing

Please see the Contribution Guidelines.

Attribution

Based on the excellent recipe by Zoran Isailovski, Copyright (c) 2005.

Download files

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

Source Distribution

Events-0.4.tar.gz (5.6 kB view details)

Uploaded Source

File details

Details for the file Events-0.4.tar.gz.

File metadata

  • Download URL: Events-0.4.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.12.1 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.17

File hashes

Hashes for Events-0.4.tar.gz
Algorithm Hash digest
SHA256 01d9dd2a061f908d74a89fa5c8f07baa694f02a2a5974983663faaf7a97180f5
MD5 a852849482d50c9ff3c61cb35ff96496
BLAKE2b-256 f62bb92ae30db60cb3c2043da3b72abf30158c92cc77922280b45e9edf36bbf8

See more details on using hashes here.

Supported by

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