Skip to main content

harami (ハラミ) is a lightweight "Signals" 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.widget_emitter = Observable[Widget]()

    @event(WidgetEventArgs)
    def on_widget_created(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 create_widget(color: str) -> None:
        widget = Widget(color)
        self.on_widget_created(widget)
        self.widget_emitter(widget)

class MyApp:
    def __init__(self) -> None:
        self.__widget_factory = WidgetFactory()
        self.__widget_factory.on_widget_created += self.__on_widget_created_handler
        self.__widget_factory.widget_emitter += self.__widget_observer

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

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

    def run(self) -> None:
        self.__widget_factory.create_widget('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 add_handler()/remove_handler(), 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.

Metadata

Release files for harami 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for harami 2.0.0
File Size Uploaded
harami-2.0.0.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for harami 2.0.0
File Interpreter ABI Platform
harami-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.7 kB

Release files / harami-2.0.0.tar.gz

Download URL harami-2.0.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
f99377c75349d82d5b478d6d9854b31125f66110f5a806998b733aaa4ddb3f92
BLAKE2b-256 checksum
How to use checksums
eed1b96182b3ab667e3d2a0bde938c6d6f8a4c177f271a53790c8acb9799e61c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15+

Release files / harami-2.0.0-py3-none-any.whl

Download URL harami-2.0.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e13822430b79a3d9432a99f065f061c79b7491dd7adf0b3dceece74f97cf1065
BLAKE2b-256 checksum
How to use checksums
cca23ef0d516cc9cdae6aa792725911ceb6fb9803fc47f819e1b04e7344d44ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15+

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page