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
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.)
- The names you use for events, observables, handlers, and observers are not relevant, name things as you please.
- The scoping (public vs. private) of members and functions is not important, scope as you please.
- 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
*argsand**kwargspassed to an Event Source are forwarded via anargsattribute of typetupleand akwargsattribute of typedict, both accessible onEventArgsand anyEventArgssubclass. You can use these in the raw for downstream method invocations or wrap them behind type-hinted properties as seen in the example above. - Observers can be async, even though observables do not expose an async/coro signature.
- Observables provide a value-assignment syntax, ex:
myObserver.state = 'foo'if that is preferred, this also simplifies using an observable to back properties. - Events offer
addHandler()andremoveHandler(), observables offerattach()anddetach(), each as respective alternatives to the+=and-=shorthand syntax 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file harami-0.0.3.tar.gz.
File metadata
- Download URL: harami-0.0.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
601d3c0cc6953cb16c39f9080399f83fe0db2d8d422efdc446c943a634d70b09
|
|
| MD5 |
33c764099aff62a76b8b53e965bb8358
|
|
| BLAKE2b-256 |
4115ab6d5fbf275266b8e87bac32ca8f774892a70fdd557b490e4d4105d277b5
|
File details
Details for the file harami-0.0.3-py3-none-any.whl.
File metadata
- Download URL: harami-0.0.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f5ab8880ed1374024cb313ab58cd2ee7f794b7a5eec0be9ababb4676910bc6b
|
|
| MD5 |
b61e03e25648cad922334f42f5ccca8d
|
|
| BLAKE2b-256 |
dfe8c481d39f726eebe9bc37e1847d4b92fc41b415ca65f9f44ba81d8e5c8f4a
|