Skip to main content

Py-Peeper

A very simple, minimalist, single-threaded and broadcast-routed approach to the observable pattern in Python.

Installation

pip install pypeeper

Usage

Implementing an Observable class

Every class can be converted to observable just by subclassing it from Observer. If class attribute "pattern" is overwritten with a valid regex expression, only matching attributes defined within the class constructor will be observed. Otherwise, all attributes will be observed.

from pypeeper import Observable, Observer

class AnyClass(Observable):

    def __init__(self):
        self.attribute_a = False
        self.whatever_attr = 0

    def set_attribute(self, value):
        self.attribute_a = value

Implementing an Observer class

Any class can be Observer of all Observable classes defined as shown above. To define how the class should react on Observable's changes, just override "notify" method from Observer class.

class ObserverClass(Observer):

    def notify(self, class_name, object_id, attribute_name, old, new):
        print(class_name, object_id, attribute_name, old, new)

Instantiate and play

my_observable = AnyClass()
my_observer = ObserverClass()

my_observable.set_attribute(True)
my_observable.attribute_a = 10
my_observable.whatever_attr = None

Console output

>>> AnyClass 2448221506696 attribute_a False True
>>> AnyClass 2448221506696 attribute_a True 10
>>> AnyClass 2448221506696 whatever_attr 0 None

Metadata

Release files for pypeeper 1.1

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

Built distribution (wheel)

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

Release files / pypeeper-1.1-py3-none-any.whl

Download URL pypeeper-1.1-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be1c1e7f28ff9b089a23e31873e4a1e140699e73bf17b9ac8016bdb5507851de
BLAKE2b-256 checksum
How to use checksums
c5bd9bcf15b336084445db24328d1f9ff5847ac028cf63fd6c929d687a54fccc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.8

Release history Release notifications | RSS feed

This release

1.1 This release

1 release file

1.0

1 release file

0.3

1 release file

0.2

1 release file

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