Skip to main content

Events inspired by C# EventHandler supporting async methods

Project description

Events-asyncio

This is a modification of Events library_ that adds support for asyncio. Unlike the original library, this modification only supports Python 3.5+.

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.

.. code-block:: pycon

>>> async def something_changed(reason):
...     await some_awaitable()
...     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:

.. code-block:: pycon

>>> await 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.

.. code-block:: pycon

>>> 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.

.. code-block:: pycon

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

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

Installing

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

pip install asyncio-events

Testing

Just run: ::

python setup.py test

License

Asyncio-events is BSD licensed. See the LICENSE_ for details.

Attribution

Based on Events library by Nicola Iarocci.

.. _LICENSE: https://github.com/pyeve/events/blob/master/LICENSE .. _Zoran Isailovski: http://code.activestate.com/recipes/410686/ .. _Nicola Iarocci: https://github.com/nicolaiarocci .. _Events Library: https://github.com/pyeve/events

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

asyncio-events-0.1.4.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

asyncio_events-0.1.4-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file asyncio-events-0.1.4.tar.gz.

File metadata

  • Download URL: asyncio-events-0.1.4.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for asyncio-events-0.1.4.tar.gz
Algorithm Hash digest
SHA256 8c25f2cc4cea72107e937ef80a28f309022dbc692d086cdb9a8f242be1d2c48a
MD5 ff6532c77d5e2905e222fe2b5e2644c4
BLAKE2b-256 e4f45187707adefdd025442be0fe29a7dc4815ba609cee2dff05c82cd0575742

See more details on using hashes here.

File details

Details for the file asyncio_events-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: asyncio_events-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for asyncio_events-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9746c8825700d227dacbe7cf347d4f4b42e2eb0a711ad387a7785472f8fa843c
MD5 350d8b21b100a47a0edf83df35373be8
BLAKE2b-256 6b8f6b437f52ecafa1f7d68da2684bb9b9ce557460e8d80d1194333ac8a067b1

See more details on using hashes here.

Supported by

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