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.1.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.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: observerutil-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 ff2c7df4f424c9b06932a127e03d78ce3c4fdafde389a5b540a4406cb9613ddd
MD5 b906daf95b25dcae45c3fb96f013601f
BLAKE2b-256 f5aba40a5753c2f47f14645125945ab6d06defdb07b373c4f33d916d54969772

See more details on using hashes here.

File details

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

File metadata

  • Download URL: observerutil-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2efdc20866eb187ee41d2f537b7f2b72791c75aaeaf0378742d2fd44568aca14
MD5 d1119fe53243fdb75a00f5a832f90a43
BLAKE2b-256 1757292b14d052c380bb9a90ba72ec4eefb2f8c6f8ff1da9da0cf9d4530d3c2b

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