Skip to main content

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 Relay supports must be async and bounded to a class that extends Relay.

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 some_listener(self, event: Event[SomeDataModel]): ...

    @Relay.emits
    async def some_emitter(self) -> SomeDataModel:
        ...
        return SomeDataModel(...)
        # or return Relay.NoEmit(SomeDataModel(...))  # if you don't want to emit 
    
    @Relay.emits
    @Relay.listens
    async def unused_emitter_and_listener(self, Event) -> SomeDataModel:
        ...
        if event.data > 10:
            return Relay.Emit(SomeDataModel(...))
        else:
            return SomeDataModel(...)

# NOTE: you can have more classes that extend Relay. They can communicate with each other.

# Create bindings
emitter_binding = Emitter(
    method=SampleRelay.some_emitter, 
    channel="channel1", 
    event_type="event1"
)
listener_binding = Listener(
    method=SampleRelay.some_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.some_emitter()

In this example, when some_emitter() is called, it will emit an event whose channel and event_type are "channel1" and "event1", respectively. When this event is emitted, some_listener will be invoked with the corresponding event instance. Awaiting the some_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


Download files

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

Source Distribution

relaypy-async-0.1.1.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

relaypy_async-0.1.1-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file relaypy-async-0.1.1.tar.gz.

File metadata

  • Download URL: relaypy-async-0.1.1.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for relaypy-async-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d5487b7333dd938f6713592c5dec28ee0c5198196ffbdb7ce73fe8dbb2832072
MD5 7b95fc2150a81bcb08610181dd216482
BLAKE2b-256 400b21fe7ce17e9e7177ca3d87318bf506db66f75ce048ace42c5339725d6814

See more details on using hashes here.

File details

Details for the file relaypy_async-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: relaypy_async-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.4

File hashes

Hashes for relaypy_async-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1565ec893b9591347433cd45808cb73b7368e4ea448fcd0a3347f3031b12c54c
MD5 d6daf137134f8bc6e20d6bdb16c34a21
BLAKE2b-256 7881b4f1afb7090f5c87102304d07cbd3310e5f988567e60439067dc1a277af6

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