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))

Why?

Lets imagine 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()

We have SRP breaks, and a number of side effects unrelated to the business problem. This code is hard to test, it couples 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.0.1.tar.gz (5.7 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.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file oop-event-bus-0.0.1.tar.gz.

File metadata

  • Download URL: oop-event-bus-0.0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for oop-event-bus-0.0.1.tar.gz
Algorithm Hash digest
SHA256 cf32bd5acc6de0bb74e512e96bfa5200b3ff11efaf677d697d9e736d646ecb66
MD5 a52cd23f5be21cf767d16bb6e57b47da
BLAKE2b-256 bb09a96861780deed77ea74a6589fbc53db3a86c320e829472afd587f4587f0f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: oop_event_bus-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for oop_event_bus-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da2ea72aac7346f20093c3a90c6e9ce60eb46841438347e57cb8883402bea203
MD5 00e4b24bb7d2b53856c0a866b4de818c
BLAKE2b-256 db368b33163f47ec3ccba3bc13f976c05eac4d2f4f444224e9e8d433cb602ae1

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