An asynchronous event library
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file avents-0.8.1.tar.gz
.
File metadata
- Download URL: avents-0.8.1.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fce4a8f0017b04705bf23ceeda2011fa1a134efc30e8e4889cd94c2516b20e8 |
|
MD5 | 20a6c76de473cfbf6e72e30be5d1c729 |
|
BLAKE2b-256 | a06773dd4f9f1c654085c81ebcaa0eaf29e7ae27531cfd4a4a3ba5e54cff1c52 |
File details
Details for the file avents-0.8.1-py3-none-any.whl
.
File metadata
- Download URL: avents-0.8.1-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b41863b4edc77058e7bc6cb53c559ec8f36ee410d13d57695f23ac193d15607 |
|
MD5 | 28f0651a2c2a5e677743e13f9ff4b6a4 |
|
BLAKE2b-256 | a2ef01d80d4711950c5b1155ca8d443f14b2f8e87634cb6cdc6f96700880ee84 |