Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-notify-1.0.1.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

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

python_notify-1.0.1-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file python-notify-1.0.1.tar.gz.

File metadata

  • Download URL: python-notify-1.0.1.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

File hashes

Hashes for python-notify-1.0.1.tar.gz
Algorithm Hash digest
SHA256 cb2f53fb05ec1d1e6e62baff15f82081c0f47cd5f52ef506620bdf441a8ccf40
MD5 6d60265a8b12bd8dcf088f9b860abc42
BLAKE2b-256 09502551db97e89ba2efcd3823224049331fdcd354fce0ec3688ee9083dbf905

See more details on using hashes here.

File details

Details for the file python_notify-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_notify-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.3

File hashes

Hashes for python_notify-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d689cd1a10e0009d4200486c95bea364211ff8df2ed9612dc034cc8718470f43
MD5 f94b77c1a9452a8013e2e389264fb5a4
BLAKE2b-256 46263d107963f3c67f0fca3890ea5d427953f99fee1a586ae0b9ddfa2719cbba

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