Skip to main content

notifyr - Create observers in runtime

PyPi: https://pypi.org/project/notifyr/

Description

Notifyr is a package that enables simple class observed-observer schema at runtime. Instead of building the whole observation and notification toolchain, just use python decorators and notifyr will take care of the rest.

Inspired by the Observer Design Pattern, notifyr adds the necessary methods to your classes without inheritance.

Decorators

Function decorators:

  • @target
    • Indicates that the decorated function is targeted and will trigger the observers update() everytime it runs.

Class decorators:

  • @observed
    • Adds .observers list attribute.
    • Adds .attach(obj) method that appends obj to the observers list.
    • Adds .notify() method that notifies the observers everytime the targeted functions are called
  • @observer('function_name')
    • Adds update() method that executes class's function_name() passing, as arguments, self and everything that the targeted function received (including the self argument).

Usage

Original Code:

class Dog(object):
    def __init__(self, name):
        self.name = name

    def bark(self):
        print('Woof')

    def sleep(self):
        print(self.name, 'is now asleep: ZZzzzzZzzZ...')

class Person(object):
    def __init__(self, name):
        self.name = name

    def educate_dog(self, dog):
        print(self.name + ':','Sleep,', dog.name)
        dog.sleep()

Suppose we want a person to educate a dog every time the animal barks:

from notifyr.agents import observed, observer
from notifyr.functions import target

@observed
class Dog(object):
    def __init__(self, name):
        self.name = name

    @target
    def bark(self):
        print('Woof')

    def sleep(self):
        print(self.name, 'is now asleep: ZZzzzzZzzZ...')

@observer('educate_dog')
class Person(object):
    def __init__(self, name):
        self.name = name

    def educate_dog(self, dog):
        print(self.name + ':','Sleep,', dog.name)
        dog.sleep()

And now, it is possible to archieve this by magically calling bark() after attaching a person to a dog:

d = Dog('Tobby')
p = Person('Victor')

d.attach(p) # Victor is now observing Tobby

d.bark()
# Woof
# Victor: Sleep, Tobby
# Tobby is now asleep: ZZzzzzZzzZ...

Release files for notifyr 1.1

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

Source distribution (sdist)

Source distribution for notifyr 1.1
File Size Uploaded
notifyr-1.1.tar.gz 2.5 kB Details

Built distribution (wheel)

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

Total release size:17.6 kB

Release files / notifyr-1.1.tar.gz

Download URL notifyr-1.1.tar.gz
Size 2.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5c189dfdca2976ebfe4f5e80903f8c7d7e7cbb397d0821ef0f550221f2c14e34
BLAKE2b-256 checksum
How to use checksums
2c2645ec3a162795fddebd4463cd4ec4c92dcf0b1172bf1cc4eeba82e107563b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.3

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

Download URL notifyr-1.1-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ab83ec7a42e3d7eef567c6a9997da8616cb04fc7c61cb7507fe57b1a338f9ce
BLAKE2b-256 checksum
How to use checksums
c02f4f033d42529f502fbdf49a0d6e90c4e8dac5e708d0efcd49a48ed742579f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.5.3

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files

1.0

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