Skip to main content

No project description provided

Project description

AIObservable

PyPI

A simple and efficient implementation of the observable pattern.

Introduction

What sets it apart is that it doesn't represents events as a combination of a name and arguments, but instead operates on classes.

Instead of using names like "on_connect" the library encourages the use of a "ConnectEvent" class which has the arguments as its attributes. Instead of listening to a meaningless name observers instead use the event type (the class). When emitting an event we then use instances of the event class.

Apart from other benefits this especially helps with typings and eliminates the issue of having to know the function signature for each event, as the only argument is the event instance.

Using the built-in dataclasses makes it easy to avoid writing boiler-plate code for each event.

Example

import asyncio
import dataclasses

import aiobservable


@dataclasses.dataclass()
class ConnectEvent:
    user_id: int
    user_name: str

async def main():
    observable = aiobservable.Observable()

    def on_connect(event: ConnectEvent) -> None:
        print(f"{event.user_name} connected!")

    observable.on(ConnectEvent, on_connect)

    event = ConnectEvent(1, "Simon")

    # emit returns a future which resolves to None when all observers
    # are done handling the event
    await observable.emit(event)

asyncio.run(main())

Installing

You can install the library from PyPI:

pip install aiobservable

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

aiobservable-0.2.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

aiobservable-0.2.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file aiobservable-0.2.1.tar.gz.

File metadata

  • Download URL: aiobservable-0.2.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.5

File hashes

Hashes for aiobservable-0.2.1.tar.gz
Algorithm Hash digest
SHA256 84ec56b3f48dd8ae5f0f923228187bf583f6dda3e864ce242f5521903c815089
MD5 f9686700e698d4c6be482d0c67e41391
BLAKE2b-256 1cb58ecc8505cc72c1f3787f49c00e09ee7851d8991eef7d0fa7b2c251298526

See more details on using hashes here.

File details

Details for the file aiobservable-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: aiobservable-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.37.0 CPython/3.7.5

File hashes

Hashes for aiobservable-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a4924b0be09f190dd4ad0d47a7cc5a28643dd36c7ab2a90392cd7395c90b60c0
MD5 8d4852649d991bf41632e9ea14c9cd76
BLAKE2b-256 f5bd6201e009f7df18de20c9b5f6c0bb2d5d5700ad40cce0c0cb60f878d41bf9

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