Skip to main content
Author: Price Hiller
Contributors:
    - Jacob B. Sanders
Description: A python library for emitting and reading events asynchronously.
Type: Library
License: BSD 3-Clause License
Copyright: 2021, Price Hiller

Async Events

A(n) OOP asynchronous Python library capable of emitting custom events based on string matching.

Table Of Contents

Installation

  • From pip
python3 -m pip install avents

Installation from Source

python[3] setup.py install

Usage

Basic Usage

Using the listen decorator you can define a function to listen for an event like so and attach a parser for the events:

from avents import listen # the listener that matches for a string via `listen("some string")`
from avents import Event # an event object defining a name of the event and the event content
from avents import parse # the parser required to emit events via `await parse(Event("some event", "event content")

# The listener
@listen("event", "another event") # A listener can listen to multiple events
async def example_event_function(event: Event):
    print(f"Received event: \"{event.name}\", with content: \"{event.content}\"")

# The parser
async def parser():
    # The actual parsing of an event below
    await parse(Event(name="event", content="example event content"))
    await parse(Event(name="another event", content="another event for the same handler"))

A full, runnable, example:

import asyncio # Required to run the application

# avents imports
from avents import listen # the listener that matches for a string via `listen("some string")`
from avents import Event # an event object defining a name of the event and the event content
from avents import parse # the parser required to emit events via `await parse(Event("some event", "event content")

# The listener
@listen("event")
async def your_event(event: Event):
    print(f"Received event: \"{event.name}\", with content: \"{event.content}\"")

async def main():
    # The actual parsing of an event below
    await parse(Event(name="event", content="this is some event content"))


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Advanced Usage

Event Types

Instead of using raw strings in the listen decorator, for larger projects it may become much more practical to use a class container of events to listen for.

Avents provides a base event type class to inherit from, that being BaseEventType which is an enum object.

To define event type enums inherit from the said base class:

from avents import BaseEventType

class CustomEventType(BaseEventType):
    SOME_EVENT: str = "an event"

This can then be used in conjunction with the listen decorator to maintain consistency throughout a larger project like so:

from avents import BaseEventType
from avents import listen
from avents import Event

class CustomEventType(BaseEventType):
    SOME_EVENT: str = "an event"

@listen(CustomEventType.SOME_EVENT)
async def some_event(event: Event):
    print(event)

Custom Event Listeners

In the scenario in which you may want to parse a base event and then pass it to further sub event parsing creating a custom event listener can save quite the headache.

In a basic use case we only used the EventListener as provided to us by avents which is exposed by listen and parse in more simplistic use cases.

With the custom listener inheriting the base EventListener exposes custom listening:

from avents import EventListener

class CustomEventListener(EventListener):
    ...

Or you can include a constructor as well:

from avents import EventListener

class CustomEventListener(EventListener):

    def __init__(self):
        super().__init__()

Once a custom event listener has been created the listen and parse methods are immediately available:

from avents import EventListener
from avents import Event

class CustomEventListener(EventListener):
    ...

async def example_event_emitter():
    for i in range(2):
        await CustomEventListener.parse(Event(name=str(i), content=i))

@CustomEventListener.listen("0", "1", "2") # Listen() can take multiple events to listen for
async def listen_for_one(event: Event):
    print(event)

It can be extended further by incorporating the EventListener class as the base event handler in the following pattern:

from avents import EventListener
from avents import Event

class CustomEventListener(EventListener):
    ...

async def example_event_emitter():
    for i in range(2):
        await EventListener.parse(Event(name=str(i), content=i))

@EventListener.listen("0")      
async def base_event_handler(event: Event):
    await CustomEventListener.parse(Event(name=event.content + 1, 
                                          content="We incremented to generate a design pattern")
                                    )

@CustomEventListener.listen("1")
async def listen_for_one(event: Event):
    print(event)

Notice that we use the base EventListener for the initial parsing of an emitted event by example_event_emitter which will then be passed to the base_event_handler when an event of name 0 is emitted which then does subparsing for the CustomEventListener class.

This can be extended to create entire command structures or other such hierarchical structures.

Metadata

Release files for avents 0.8.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 avents 0.8.1
File Size Uploaded
avents-0.8.1.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for avents 0.8.1
File Interpreter ABI Platform
avents-0.8.1-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / avents-0.8.1.tar.gz

Download URL avents-0.8.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9fce4a8f0017b04705bf23ceeda2011fa1a134efc30e8e4889cd94c2516b20e8
BLAKE2b-256 checksum
How to use checksums
a06773dd4f9f1c654085c81ebcaa0eaf29e7ae27531cfd4a4a3ba5e54cff1c52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release files / avents-0.8.1-py3-none-any.whl

Download URL avents-0.8.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b41863b4edc77058e7bc6cb53c559ec8f36ee410d13d57695f23ac193d15607
BLAKE2b-256 checksum
How to use checksums
a2ef01d80d4711950c5b1155ca8d443f14b2f8e87634cb6cdc6f96700880ee84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4

Release history Release notifications | RSS feed

This release

0.8.1 This release

2 release files

0.8.0

2 release files

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