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
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):
    return int(message)

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.2.tar.gz (7.3 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.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: observerutil-0.1.2.tar.gz
  • Upload date:
  • Size: 7.3 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.2.tar.gz
Algorithm Hash digest
SHA256 3237f2773f7fb7db4d7ed5fffb359230a45efbcc882c7209b689cc1c8f5f8c9e
MD5 d4fc2ac402f2da277961d924f4d10126
BLAKE2b-256 6ebd24bd5aac7d6fb24f62b2efef7a3bbfee4e29f4207efd0212a67b335f4835

See more details on using hashes here.

File details

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

File metadata

  • Download URL: observerutil-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 accd585ffa27e4182db7574402d8ed256e9e2b074e37b109c0a8f90f3f9226f6
MD5 5fa3a1d42eec42e640e520b38b900295
BLAKE2b-256 5debac0f5a086aac6067c28bdbf53452ccfc3dcf60c670d130eb92481a00baf9

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