Skip to main content

Anywise let you write your application anywise

Project description

Anywise

Anywise let you write your application anywise.


Source Code: https://github.com/raceychan/anywise

Documentation: On its way here...


Rationale

  1. promote best practices and enterprise architecture in python
  2. isolating bussiness logic from input ports, allowing one app for web api, kafka, flink, etc.
  3. let you write less code than other wise

Philosophy

  1. non-intrusive
  2. minimalism

Install

pip install anywise

Quck Start

from anywise import Anywise, handler_registry, inject

class UserCommand: ...
class CreateUser(UserCommand): ...
class UserEvent: ...
class UserCreated(UserEvent): ...

registry = MessageRegistry(command_base=UserCommand, event_base= UserEvent)

@registry
async def create_user(
     command: CreateUser, 
     anywise: Anywise, 
     service: UserService = inject(user_service_factory)
):
     await service.create_user(command.username, command.user_email)
     await anywise.publish(UserCreated(command.username, command.user_email))

@registry
async def notify_user(event: UserCreated, service: EmailSender):
     await service.send_greeting(command.user_email)

# at your client code

async def main():
     anywise = AnyWise()
     anywise.include([userhandler, userlistener])
     command = CreateUser()
     result = await anywise.send(command)

Tutorial

register command handler / event listeners with MessageRegistry

registry = MessageRegistry(UserCommand)
registry(hanlder_func)

use MessageRegistry to decorate / register a function or a class as handlers of a command.

when a function is registered, anywise will can through its signature, if any param is annotated as a subclass of the base command type, it will be registered as a handler of the command.

when a class is registered, anywise will scan through its pulic methods, then repeat the steps to functions.

use CommandGuard to intercept handler call

from anywise import AnyWise, GuardRegistry, handler_registry

user_registry = MessageRegistry(command_base=UserCommand)


@user_registry
async def handler_create(create_user: CreateUser, context: dict[str, ty.Any]):
    assert context["processed_by"]
    return "done"


@user_registry
async def handler_update(update_user: UpdateUser, context: dict[str, ty.Any]):
    return "done"


@user_registry.pre_handle
async def mark(command: UserCommand, context: dict[str, ty.Any]) -> None:
    if not context.get("processed_by"):
        context["processed_by"] = ["1"]
    else:
        context["processed_by"].append("1")

in this case, mark will be called before handler_update or handler_create gets called.

Features

  • builtin dependency injection
  • handler guards
  • framework integration
  • remote handler

FAQ

On its way 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

anywise-0.1.2.tar.gz (49.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

anywise-0.1.2-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file anywise-0.1.2.tar.gz.

File metadata

  • Download URL: anywise-0.1.2.tar.gz
  • Upload date:
  • Size: 49.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for anywise-0.1.2.tar.gz
Algorithm Hash digest
SHA256 03bc5e478b29fcbf6b3ac4b2081252b1f1a62fbad4622c19e4f0416de251d956
MD5 299ad621df06e7883de770b70c84fc03
BLAKE2b-256 5477720526db4f700154825daaa363dbee32c6ea42bc32bab1ef8a2c993f0aae

See more details on using hashes here.

File details

Details for the file anywise-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: anywise-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.9

File hashes

Hashes for anywise-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 242ddae55cdc3dde1256e82a0105f017c92057e1dcb38174b7bf8ac12c698cb0
MD5 aa5633d53ad366b7ce469a2a8140be76
BLAKE2b-256 f8e76116baa98e5ed8d609b8ce8987a0690eee9213aab5102f6d1f3b482b3c6e

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