Skip to main content

Let you implement observer pattern from the box

Project description

observerutil is simple and powerful observer pattern tool.

How to install

You could install from PyPi:

$ python3 -m pip install observerutil

Any callable can become observer

from typing import Any
from observerutil import Observers

def func_a(message: Any):
    ...

def func_b(message: Any):
    ...

observers = Observers([func_a])
observers.add(func_b)
my_message = 'some message here'
# or
my_message = {1: 2}

# Distribute message to func_a and func_b.
# Any exceptions will be ignored.
observers.send_message(my_message)

But in this case any exception will be ignored. If you would like to catch exceptions then use Observer with error handler.

from typing import Any
from observerutil import Observer, Observers, ErrorHandler


def func_a(message: int):
    print(100 / message)


def write_exception_to_logs(exc: Exception):
    ...


observer = Observer(func_a, error_handler=write_exception_to_logs)
observers = Observers()
observers.add(func_a)

my_message = 0
observers.send_message(my_message)

If you would like to adapt message for observers in the collection then add message adapter

from typing import Any
from observerutil import Observers


def func_a(message: int):
    print(100 / message)

def convert_to_int(message: str):
    # exception of adapter is not excepted by Observers instance.
    # add try...except by self if required.
    try:
        return int(message)
    except Exception:
        return None

observers = Observers(message_adapter=convert_to_int)
observers.add(func_a)

my_message = '2'
# any exceptions of observers (funcs or Observer instances) will be excepted while sending
observers.send_message(my_message)

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

observerutil-0.1.3.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

observerutil-0.1.3-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file observerutil-0.1.3.tar.gz.

File metadata

  • Download URL: observerutil-0.1.3.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for observerutil-0.1.3.tar.gz
Algorithm Hash digest
SHA256 41e6c5e5e985a2f9a80717300d046f7f66019c51c0e60c7ec77dd03bebfcce40
MD5 9a52b9e366a35fa9747345d0d91ba369
BLAKE2b-256 be1f6dd2bb18103161f048a47fdbec628b89ca33daa25cf71c376895d8962901

See more details on using hashes here.

File details

Details for the file observerutil-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: observerutil-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for observerutil-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 31da73f4abf2b30f4920d585744d172905911facc96c1ea8f83c8553b1809137
MD5 a424050e97fd74be2c57040abf91a883
BLAKE2b-256 c8f28797665221ce32d096a6ec690acee680174e74808bbf688a3e8396fb8860

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