Skip to main content

A lightweight 'Event' and 'Observable' library.

Project description

harami is a lightweight "Event" and "Observable" library for Python.

This README is only a high-level introduction to harami. For more detailed documentation, please view the official docs at https://harami.readthedocs.io.

Installation

harami can be installed from pypi through the usual means:

pip install harami

Usage

Let's try a "learn by example" approach. The following code is a Widget Factory implementation. The Widget Factory raises an event on Widget Creation, it also emits the Widgets it creates through an Observable. The MyApp class creates an instance of the Widget Factory, adds an event handler, adds an observer, and finally requests the factory create a Widget.

from harami import *

class Widget:
    color:str
    def __init__(self, color:str = None) -> None:
        self.color = color

class WidgetEventArgs(EventArgs):
    @property
    def widget(self) -> Widget:
        return self.args[0]

class WidgetFactory:
    def __init__(self) -> None:
        self.widgetEmitter = Observable[Widget]()

    @event(WidgetEventArgs)
    def onWidgetCreated(self, widget:Widget) -> None:
        # this is an "Event Source", it does not require an
        # implementation, but one can be provided if it
        # makes sense for your application
        pass

    def createWidget(color:str) -> None:
        widget = Widget(color)
        self.onWidgetCreated(widget)
        self.widgetEmitter(widget)

class MyApp:
    def __init__(self) -> None:
        self.__widgetFactory = WidgetFactory()
        self.__widgetFactory.onWidgetCreated += self.__onWidgetCreatedHandler
        self.__widgetFactory.widgetEmitter += self.__widgetObserver

    def __onWidgetCreatedHandler(sender:object, e:WidgetEventArgs) -> None:
        print(f'Widget Created: color is {e.widget.color}')

    def __widgetObserver(widget:Widget) -> None:
        print(f'Widget Emitted: color is {e.widget.color}')

    def run(self) -> None:
        self.__widgetFactory.createWidget('red')

MyApp().run()

When executed the program outputs the following:

Widget Created: color is red
Widget Emitted: color is red

Notables..

Things not obvious given the example above:

  • Events and Observables can be declared at a module scope (classes are not required.)
  • There are no visibility restrictions (public vs. private) for events/observables nor handlers/observers.
  • Event Handlers can be async, whether or not the Event Source is async.
  • Event Sources can be async, whether or not Event Handlers are async.
  • Event Handlers receive an EventArgs, which you can optionally subclass as seen in the example.
  • All *args and **kwargs passed to an Event Source are forwarded via an args attribute of type tuple and a kwargs attribute of type dict, both accessible via EventArgs.
  • An Event Source does not need to be parameterized, in such cases EventArgs.empty will be forwarded.
  • An EventArgs subclass does not need to be specified via @event (it defaults to EventArgs).
  • Observers can be async, even though observables do not expose an async/coro signature.
  • Observables provide a value-assignment syntax, ex: myObserver.state = 'foo', this may help simplify using observables to back properties.
  • Events offer addHandler()/removeHandler(), and observables offer attach()/detach(), each as alternatives to +=/-= syntax as seen in the example.
  • Last, but not least, Observables can be used as Event Handlers, and Event Sources can be used as Observers.

This library is meant to be lightweight and not have dependencies on other libraries, as such it has an intentionally narrow focus.

Contact

You can reach me on Discord or open an Issue on Github.

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

harami-0.0.5.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

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

harami-0.0.5-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file harami-0.0.5.tar.gz.

File metadata

  • Download URL: harami-0.0.5.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3+

File hashes

Hashes for harami-0.0.5.tar.gz
Algorithm Hash digest
SHA256 449efa1242dd7144094e4b3008cccaad99ba14da8f78a5f9fdb9394264ce1fc0
MD5 46a62a84b654609ff7ccabf45b878499
BLAKE2b-256 6a8dcd358d0ea4655027e7535b9c6fbfc582d780d3e524e5e5d5b34d5086e10d

See more details on using hashes here.

File details

Details for the file harami-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: harami-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3+

File hashes

Hashes for harami-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 bbd5313bb3cc72b4ab3f3f7a40b569b996101dbf90bdb3935c6e56f9bc9bee79
MD5 220854c83c097eb1e845f06df66997e2
BLAKE2b-256 8d1da02735615abd64f6e70740f0e74671b69818ab0cb54c35e218902b0d7563

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