Relay is a Python package providing an asynchronous event system in a class, enabling easy inter-method communication. It automatically validates emitted data against provided type hints and facilitates setting up complex event emitting-listening configurations through easy bindings creation.
Project description
Relay
Relay is a Python package that allows asynchronus inter-method communication between classes inheriting from Relay class, enabling one method to trigger an event or multiple events with same payload that multiple methods can listen to and respond to. Using Relay, you can create complex event emitting and listening configurations without needing to manually bind each method and handle communication logic. This is achieved through the concept of bindings that define the relationship between an event emitter method and a listening method.
Installation
You can install Relay through pip by running:
pip install relaypy-async
Features
- Not just event listeners and emitters: Relay validates the data emitted with the type hints you provide, and avoids wrong data type errors.
- Automate event handling: Pass the binding configuration once during the class instantiation and Relay will automatically call the right methods when a particular event is emitted.
- Flexible and Extendable: Relay supports multiple listeners per emitter and multiple emitters per listener.
- Async: The methods
Relaysupports must be async and bounded to a class that extendsRelay.
How to use
Here is a basic example of using Relay:
from relay import Relay, Event, Emitter, Listener
class SampleRelay(Relay):
@Relay.listens
async def listener(self, event: Event[SomeDataModel]): ...
@Relay.emits
async def emitter(self) -> SomeDataModel:
...
return SomeDataModel(...)
# or return Relay.NoEmit(SomeDataModel(...)) # if you don't want to emit
# NOTE: you can have more classes that extend Relay. They can communicate with each other.
# Create bindings
emitter_binding = Emitter(
method=SampleRelay.emitter,
channel="channel1",
event_type="event1"
)
listener_binding = Listener(
method=SampleRelay.listener,
channel="channel1",
event_type="event1"
)
# Use bindings in the Relay initialization
relay = SampleRelay(bindings_config=[emitter_binding, listener_binding])
# Emit an event (in an asyncio loop)
...
await relay.emitter()
In this example, when emitter() is called, it will emit an event whose channel and event_type are "channel1" and "event1", respectively. When this event is emitted, listener will be invoked with the corresponding event instance. Awaiting the emitter does not block the execution as the event is emitted asynchronously.
Error Handling
Relay provides robust error handling features. If the data type of the emitting event does not match with the required data type or type hints are missing/inconsistent, a TypeError will be raised.
Full Documentation
Please note that the Relay package is under development, and the existing functionality may slightly change in future versions.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file relaypy-async-0.1.0.tar.gz.
File metadata
- Download URL: relaypy-async-0.1.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44edbb8fbdeee52953b80667372944dd8af7d7f4cf58c5820172d0869567b6b0
|
|
| MD5 |
3bb44e946e247ffbc37f227e7407e176
|
|
| BLAKE2b-256 |
47452f3b4b0bef5d28123a309d3a9cca9d44ee6f9d3fc2396d8e1842512f8c24
|
File details
Details for the file relaypy_async-0.1.0-py3-none-any.whl.
File metadata
- Download URL: relaypy_async-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
41b115730a8d347582266fe9e5a7cdca524f203474c53cabee5d9dfc4d7f5040
|
|
| MD5 |
54e7290af9ecf89f6c6cd9575eb68b36
|
|
| BLAKE2b-256 |
8fa78c8ce7107151d14b79d07cfbc3cc82d5e43c92fd1217ecf2aca901b9c629
|