Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

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.

Files for aiobservable, version 0.2.2
Filename, size File type Python version Upload date Hashes
Filename, size aiobservable-0.2.2-py3-none-any.whl (10.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size aiobservable-0.2.2.tar.gz (9.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page