Implementation of observer design pattern in python3
Project description
python-notify
python-notify is an object-oriented implementation of the observer design pattern in python3 supporting signature checking at connection stage, checking of passed arguments at call stage, notifiable collections as List, Dict and decorator @notifiable_property
Setup
Library can be installed with pip:
pip3 install python-notify
Simple examples
Signal
import typing
from notify.signal import Signal
class Obj:
event_happened = Signal(data=typing.Mapping)
def event_handler(data: typing.Mapping):
print(f'{data=}')
obj = Obj()
obj.event_happened.connect(event_handler)
obj.event_happened.notify({'key': 'value'}) # Will print "data={'key': 'value'}"
Collections
This package implements specialized container data types providing alternatives to Python's general purpose built-in containers, dict, list, with signals for state changes.
List signals
items_added = Signal(items=typing.Tuple[object], pos=Position)
items_changed = Signal(past_items=typing.Tuple[object], new_items=typing.Tuple[object], pos=Position)
items_removed = Signal(items=typing.Tuple[object], pos=Position)
items_cleared = Signal()
# Where Position = t.Union[int, slice]
List example
import typing as t
from notify.collections import List
def on_items_added(items: t.Iterable[object], pos: int):
print(items, pos)
obj = List((1, 2, 3))
obj.items_added.connect(on_items_added)
obj.extend((4, 5))
# will print: (4, 5), 3
Dict signals:
key_added = Signal(key=object, item=object)
key_changed = Signal(key=object, past_item=object, new_item=object)
key_removed = Signal(key=object)
@notifiable_property
notifiable_property decorator is alternative to Python's @property decorator with signals for state changes.
from notify.property import notifiable_property
class Obj:
def __init__(self, value):
self._value = None
self.value = value
@notifiable_property
def value(self):
return self._value
@value.setter
def value(self, value):
self._value = value * 2
@value.deleter
def value(self):
del self._value
obj = Obj(value=1)
assert obj.value == 2
def on_value_changed(past_value, new_value):
print(f'{past_value=}, {new_value=}')
obj.value_changed.connect(on_value_changed)
obj.value = 2
# Will print 'past_value=2, new_value=4'
assert obj.value == 4
def on_value_removed(past_value):
print(f'{past_value=}')
obj.value_removed.connect(on_value_removed)
del obj.value
# Will print 'past_value=4'
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
Hashes for python_notify-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d689cd1a10e0009d4200486c95bea364211ff8df2ed9612dc034cc8718470f43 |
|
MD5 | f94b77c1a9452a8013e2e389264fb5a4 |
|
BLAKE2b-256 | 46263d107963f3c67f0fca3890ea5d427953f99fee1a586ae0b9ddfa2719cbba |