Skip to main content

Troopy Build Status

Command bus library for python. Makes using message bus pattern in your application easy.

What is a command/message?

Commands are objects, some kind of an imperative informing what behavior client expects from application. Commands can bear some information (client's input) required to fulfill the job. It is recommended to use dataclasses when you declare a command for your own convenience.

What is a command handler?

Command handler is a function or callable object, that accepts command as a parameter to perform specific task.

Advantages of using command bus/message bus

  • Command can be created anytime/anywhere by your client and as long as it is hand over to command bus it will be handled
  • You can slim your services layer and dependencies, as each handler perform one specific task
  • Testing your application can be more precise and easier

Features

  • Fast and simple
  • Flexible solution which can be used everywhere
  • Works well with dataclasses
  • Custom factories for command handlers

Installation

pip install troopy

Basic Usage

from troopy import CommandBus, command
from dataclasses import dataclass


class HelloHandler:
    def __call__(self, command: "SignUp") -> None:
        print("Hello user {command.username}!")


@command(HelloHandler)  # attach command to its handler
@dataclass
class SayHello:
    username: str


command_bus = CommandBus()
command_bus.dispatch(SayHello(username="Tom"))

HelloHandler is class which encapsulates our business logic (in this scenario welcomes user), any callable can be used as a command handler, as long as it is a function or class declaration without __init__ method.

SayHello is a command class which carries some data it is attached to HelloHandler with @attach decorator. @attach decorator allows the library to understand which handler is responsible for which command. It is also possible to use troopy.MessageRouter directly to attach command to its handler (example available here)

The above example will print Hello user Tom as a result.

Setting factory for command handler

It is possible to use custom function for factoring command handlers, consider the following example:

import sqlite3
from troopy import CommandBus, command
from dataclasses import dataclass

db = sqlite3.connect('example.db') 


class UserRegistrationHandler:
    def __init__(self, db):
        self.db = db
    def __call__(self, command: "RegisterUser") -> None:
        cursor = self.db.cursor()
        cursor.execute("INSER INTO users VALUES (?, ?)", (command.username, command.password))
        self.db.commit()


@command(UserRegistrationHandler)  # attach command to its handler
@dataclass
class RegisterUser:
    username: str
    password: str

def command_handler_factory(cls):
    return cls(db)

command_bus = CommandBus(handler_factory=command_handler_factory)
command_bus.dispatch(RegisterUser(username="Tom", password="secret"))

As you can probably tell UserRegistrationHandler requires sqlite db connection in order to work properly, with command_handler_factory we are able to provide this connection to the object, so RegisterUser command can be handled properly.

For more examples please check examples directory

Release files for troopy 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for troopy 0.1.0
File Size Uploaded
troopy-0.1.0.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for troopy 0.1.0
File Interpreter ABI Platform
troopy-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.6 kB

Release files / troopy-0.1.0.tar.gz

Download URL troopy-0.1.0.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c8ab7a301c60ddb7b7b04fa336bc91ace0c85e12996be1bc6a61f807266c2ec8
BLAKE2b-256 checksum
How to use checksums
f88467c41f3c76768d7a11d558cdc3599e1925dc2b52839a8a4deea804008bad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.2 Darwin/19.5.0

Release files / troopy-0.1.0-py3-none-any.whl

Download URL troopy-0.1.0-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0bb444ffee8405f2c2b8d3bc191c2b8b540d59264105b07bc85512742d2ffe00
BLAKE2b-256 checksum
How to use checksums
6763215b24e1e772210a5914698e610abf9cb2406cf12505b57cc60dd2d1facf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.2 Darwin/19.5.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page