Skip to main content

Let you subscribe and listen in right manner

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: observerutil-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 8bb0f48025c79fa618e344fba886f9379805e882da263a03aa996f2251b8c6f5
MD5 58267caecbb7bda4006a71615c263694
BLAKE2b-256 6ace0c563b9ac24e01e1d20e655cf384545646cbd744f5ed1a83701c69866495

See more details on using hashes here.

File details

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

File metadata

  • Download URL: observerutil-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50c3f54f6aa0204031b5451f5726e4d9600fd422316cdc84c7def892bbb720de
MD5 2ec041e7b44dab016629a07797b6a9d7
BLAKE2b-256 deffec9e3e550f24bdc4f6892dc19e8b47c84e611ae58e821c62c3a5cd877574

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