Skip to main content

TurboBus is an opinionated implementation of Command Responsibility Segregation pattern in python.

Project description

TurboBus

TurboBus is an opinionated implementation of Command Responsibility Segregation pattern in python.

Installation

pip install turbobus

Simple usage

Let's see an example using python typings. You can omit all the typing stuffs if you want to.

God Mode ⚡

from dataclasses import dataclass
from typing import TypeAlias

from turbobus.command import Command, CommandHandler, handler_of
from turbobus.bus import CommandBus

LogHandlerType: TypeAlias  =  "ILogHandler"

@dataclass
class LogCommand(Command[LogHandlerType]):
    content: str


class ILogHandler(CommandHandler[LogCommand, str]):
    ...


@handler_of(LogCommand)
class LogHandler(ILogHandler):
    def execute(self, cmd: LogCommand) -> str:
        return cmd.content


if __name__ == '__main__':
    bus = CommandBus()

    result = bus.execute(
        LogCommand('Hello dude!')
    )
    print(result) # Hello dude

Human Mode 🥱

from dataclasses import dataclass
from typing import TypeAlias

from turbobus.command import Command, CommandHandler, handler_of
from turbobus.bus import CommandBus

LogHandlerType: TypeAlias  =  "ILogHandler"

@dataclass
class LogCommand(Command[LogHandlerType]):
    content: str


class ILogHandler(CommandHandler[LogCommand, str]):
    ...


@handler_of(LogCommand)
class LogHandler(ILogHandler):
    def execute(self, cmd: LogCommand) -> str:
        return cmd.content


if __name__ == '__main__':
    bus = CommandBus()

    result = bus.execute(
        LogCommand('Hello dude!')
    )
    print(result) # Hello dude

Dependency injection

In many cases we're going to need to inject dependencies to our command handler. To accomplish that we have two important tools: @injectable_of decorator and inject function.

With the @injectable_of decorator we can specify a class that is implementing the functionalities of the dependency. For example:

from turbobus.injection import injectable_of, inject
from log.axioma.log import ILogger


class ILogger(ABC):

    @abstractmethod
    def logger(self, text: str) -> None:
        ...


@injectable_of(ILogger)
class Logger:

    def logger(self, text: str) -> None:
        print(text)


@command(LogCommand)
@dataclass(kw_only=True)
class LogHandler(ILogHandler):

    logger = inject(ILogger)

    def execute(self, cmd: LogCommand) -> str:
		self.logger.logger(cmd.content)
        return cmd.content

As you can see in the example above, we're defining an abstract class with the logger method. Then we're doing the implementation of the ILogger and we're indicating that in the @injectable_of(ILogger).

Then, using the inject function, TurboBus is going to map that dependency and inject the instance in the attribute.

from turbobus.bus import CommandBus

from log.axioma import LogCommand

bus = CommandBus()

result = bus.execute(
    LogCommand('Hello world')
)

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

turbobus-1.0.0a7.tar.gz (6.3 kB view details)

Uploaded Source

File details

Details for the file turbobus-1.0.0a7.tar.gz.

File metadata

  • Download URL: turbobus-1.0.0a7.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for turbobus-1.0.0a7.tar.gz
Algorithm Hash digest
SHA256 01b8c267e95b171bf90202171fea53c56abe27fae478f59e29c7ce9113303ac9
MD5 41d81375db55635bb2aa4ee9da5918a9
BLAKE2b-256 215601e274251dbbf5dfb0c3897126b24972e70913705df8268c3a777301326a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page