Skip to main content

A simple OOP event bus

Project description

Intro

This package implements an event bus based on the Mediator pattern. It is a powerful tool to avoid coupling and make components work together without any knowledge about each other (loose coupling). It helps:

  • to avoid unneeded dependencies;
  • to make testing easier;
  • to extend the functionality by adding (not changing) the units;
  • to reduce the tech debt.

Installation

pip install oop-event-bus

Usage

Create an event class with all the details you need

class UserSignedIn(Event):
    def __init__(self, email: Email):
        self.email = email

Create and register event listeners

class LogSignedInUsersListener(TypedEventListener[UserSignedIn]):
    async def __call__(self, event: UserSignedIn):
        ...

event_bus = EventBus()
event_bus.listen(LogSignedInUsersListener())

Dispatch an event

await event_bus.dispatch(UserSignedIn(user.email))

Multi-listener

It is possible to register the listener to be called for more than one event:

class MyMultiEventListener(MultiEventListener):
    def get_event_names(self) -> List[str]:
        return [TestEvent.__name__, TestEvent2.__name__]

    async def on_test_event(self, event: TestEvent):
        ...
    
    async def on_test_event2(self, event: TestEvent2):
        ...

...
event_bus.listen(MyMultiEventListener())
event_bus.dispatch(TestEvent2(...))

Example

Given the following code:

class SomethingService:
    def __init__(self, logger: Logger, mailer: Mailer, marketing: MarketingService):
        self.logger = logger
        self.mailer = mailer
        self.marketing = marketing
        
    async def do_something(self):
        self._real()
        self._business()
        self._logic()

        #side effects
        self.logger.info("We did something")

        await self.mailer.send_email("Welcome email")

        #unrelated code that should be executed when we do something
        await self.marketing.increase_marketing_counter()

There are SRP breaks, and a number of side effects unrelated to the business problem. This code is hard to test, it couples the SomethingService with the concrete implementations of logging, mailing and marketing services.

With oop-event-bus you can move all side effects and unrelated code out. Also, you will make it possible to extend functionality later without changing the code of this method.

from oop_bus import Event, EventBus

class WeDidSomething(Event):
    ...

class SomethingService:
    def __init__(self, event_bus: EventBus):
        self.event_bus = event_bus
    async def do_something(self):
        real()
        business()
        logic()

        await self.event_bus.dispatch(WeDidSomething())

You can see that every piece of non-related code was replaced with the single event_bus call here.

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

oop_event_bus-0.1.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

oop_event_bus-0.1.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: oop_event_bus-0.1.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for oop_event_bus-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4a42bd64a4fb57d3e89953c6626bb347e5ba938dff2e713ee6d145373bf8432c
MD5 de3bea5c8a511218a28512e6bf71d76c
BLAKE2b-256 0fbf6de5e555c3cdf60b9610d9a5fa3da7ecfeca88f2baf16d743208ae86ae69

See more details on using hashes here.

File details

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

File metadata

  • Download URL: oop_event_bus-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for oop_event_bus-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ea35821fb082c3c24982d6cbde063b3640fc591c2dd941c26aabf37e4bdc0f09
MD5 a3c907ce81dc7726b09d5bc534134934
BLAKE2b-256 6b947258479021ebd63e4d73f088c800f80e5de5ea97c101eaf3e55e50531c93

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