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.
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.bus import Command, CommandHandler, CommandBus
from turbobus.decorators import command, injectable
LogHandlerType: TypeAlias = "ILogHandler"
@dataclass
class LogCommand(Command[LogHandlerType]):
content: str
class ILogHandler(CommandHandler[str]):
...
@command(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 turbobus.bus import Command, CommandHandler
from turbobus.decorators import command, injectable
@dataclass
class LogCommand(Command):
content
class ILogHandler(CommandHandler):
...
@command(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
decorator and injecting
function.
With the injectable decorator we can specify a class that is implementing the functionalities of the dependency. For example:
from turbobus.decorators import injectable
from log.axioma.log import ILogger
class ILogger(ABC):
@abstractmethod
def logger(self, text: str) -> None:
...
@injectable(ILogger)
class Logger:
def logger(self, text: str) -> None:
print('from logger', text)
@command(LogCommand)
@dataclass(kw_only=True)
class LogHandler(ILogHandler):
logger = injecting(ILogger)
def execute(self, cmd: LogCommand) -> str:
if self.logger is not None:
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(ILogger)
.
Then, using the injecting
function, TurboBus is going to map that dependency and inject the instance in the attribute.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file turbobus-1.0.0a0.tar.gz
.
File metadata
- Download URL: turbobus-1.0.0a0.tar.gz
- Upload date:
- Size: 2.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e09a9bfe3f8de16d6cca83f8d760ca7e505dded92e102c29388738c7f34dfd8 |
|
MD5 | 92f19f4964ae6e39c09df353ca827379 |
|
BLAKE2b-256 | ae5a35f2be9b7b6ef78fe679d700275e3169d1f1a9bf1893986bc1e454c8fa9a |
File details
Details for the file turbobus-1.0.0a0-py3-none-any.whl
.
File metadata
- Download URL: turbobus-1.0.0a0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 488950851d9e65811fc7c39920b564259e077d0ac278c5315db2d529c11052c5 |
|
MD5 | d2cfcff4eb63b7014c7cba9bc6cb7900 |
|
BLAKE2b-256 | ea2bb3a2927033cb69c40aa0b0308c7966f0f3eb0208992f211f1e27dbf8ab18 |