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.2.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiobservable-0.2.2.tar.gz
  • Upload date:
  • Size: 9.0 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.2.tar.gz
Algorithm Hash digest
SHA256 b42ad82b1e631362750f1c518c19b215ac34141e01c06c3510d3eef9236b8bd2
MD5 99a285850c4756d225ac7bff96b57b6b
BLAKE2b-256 e08b57a1cb44eb66dc10954bef0571d3a1caac1faea8632a7444631dc1536f4d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiobservable-0.2.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 00a282a90e918d5b57e88cbe97a3f8d5ae7847ac86d7e23135953bf464739171
MD5 e75642a634dad78422d0a424e50a1269
BLAKE2b-256 775151dd0fee247a915d9cd59dc66d20e39b36711e56b353236b334fa8ad357c

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