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():
    ...

def func_b():
    ...

observers = Observers([func_a])
observers.add(func_b)

observers.notify()

It is possible to provide any args and kwargs to observer

from typing import Any
from observerutil import Observers

def func_a(message: int):
    ...

observers = Observers()
observers.add(func_a)

# Distribute message to func_a.
# Any exceptions will be ignored.
message = 0
observers.notify(message)
from typing import Any
from observerutil import Observers

def func_a(**kwargs):
    ...

observers = Observers()
observers.add(func_a)

# Distribute kwargs to func_a.
# Any exceptions will be ignored.
observers.notify(user_id=123, role='client')

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)

message = 0
observers.send_message(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)


# parameters_adapter has to implement IParametersAdapter interface
def convert_to_int(*args, **kwargs):
    # exception of adapter is not catched by Observers instance.
    # add try...except here by self if required.
    return [int(arg) for arg in args], kwargs


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

message = '2'
observers.notify(message)

More elegant way to convert each arg

from typing import Any
from observerutil import Observers, AdaptEachArg


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

observers = Observers(parameters_adapter=AdaptEachArg(int))
observers.add(func_a)

message = '2'
observers.notify(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.2.0.tar.gz (7.8 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.2.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for observerutil-0.2.0.tar.gz
Algorithm Hash digest
SHA256 722d3776f7ad2a7a977770d36e06d9f742cb0d03e80965d24e9368758ad38a92
MD5 bbc6466e993e09766aba839e43e98f3a
BLAKE2b-256 c84eede6f932cb2e354427988546c6efc2c70632ea0b5356cb04ebeb008905e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: observerutil-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60550d6cde5ee326c6ea76d5e04c755420885fa64f6832b12bb734871ae03200
MD5 42b9914289924b7afa2385c733e6ec95
BLAKE2b-256 1865c46e8c9db0e3ab5703422284f1b1c50726494f0d127a103c3504d2ce57e0

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