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.command import Command, CommandHandler, CommandBus, handler_of
from turbobus.injection import injectable
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 turbobus.command import Command, CommandHandler, handler_of
from turbobus.decorators import injectable
@dataclass
class LogCommand(Command):
content
class ILogHandler(CommandHandler):
...
@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.
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
File details
Details for the file turbobus-1.0.0a5.tar.gz
.
File metadata
- Download URL: turbobus-1.0.0a5.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70d6097a0ce8c2a296c65018408b1848ec32ea7917e1e6ed3921e5bb51e918f2 |
|
MD5 | 83d1a07835f19d9afa8c05cdf9691ca0 |
|
BLAKE2b-256 | 241d6baa5bf158b8bd6690485bf0a52ba0ee10d39c73e09d5cc543650d381387 |