Skip to main content

Take control of your event routing.

Project description

Power-Events

Take control of your event routing, effortlessly and tailored to your needs.

Description

Power-events is a lightweight yet powerful library that manages event routing by defining routes with precise conditions, inspired by FastAPI’s endpoint routing system. It simplifies the organization of complex events by applying specific routing rules defined by the user and enables actions to be triggered in response to events in a structured manner.

Key features:

  • Syntax: Lean to standards like Powertools or FastAPI.
  • Easy: Design to be easy to read and use.
  • Flexible: You can do whatever condition at any depth level in your event.
  • Fully tested: 100% coverage.
  • Fully typed: compatible out of the box with mypy !
  • Lightweight

installation

pip install power-events

That's all! Ready to use

Examples


Simple conditions

  • create a main.py with:
from typing import Any
from power_events import EventResolver

app = EventResolver()

@app.equal("type", "order_created")
def handler_order_created(event: dict[str, Any]) -> str:
  return f"Order created: {event['order_id']}"

@app.one_of("type", ["order_update", "order_delete"])
def handle_order_modification(event: dict[str, Any]) -> str:
  return f"Order modification <{event['type']}>: {event['order_id']}"
  • use routing:

N.B: assert is here to show which route has been called. In real condition, don't need to use it.

assert app.resolve({
    "type": "order_created",
    "order_id": "12345",
    "user_id": "67890"
}) == ["Order created: 12345"]
# Note: `resolve` returns a list, because you can allow multiple routes for one event.

assert app.resolve({
    "type": "order_delete",
    "order_id": "12345",
    "user_id": "67890"
})== ["Order modification <order_delete>: 12345"]

Complex conditions

In depth field condition and condition combinations

from power_events import EventResolver
from power_events.conditions import Value, Neg

app = EventResolver()

# Order created and digital purchase.
@app.when(Value("type").equals("order_created") & Value("cart.is_digital").is_truthy())
def handle_digital_purchase(event: dict) -> str:
    return f"The order created is a digital purchase: {event['order_id']}"

# Order created and physical.
@app.when(Value("type").equals("order_created") & Neg(Value("cart.is_digital").is_truthy()))
def handle_physical_purchase(event: dict) -> str:
    return f"The order created is a physical purchase: {event['order_id']}"


assert app.resolve({
    "type": "order_created",
    "order_id": "12345",
    "user_id": "67890",
    "cart": {
        "is_digital": True,
        "items": [
            "$10 voucher"
        ]
    }
}) == ["The order created is a digital purchase: 12345"]
assert app.resolve({
    "type": "order_created",
    "order_id": "12345",
    "user_id": "67890",
    "cart": {
        "is_digital": False,
        "items": [
            "keyboard"
        ]
    }
}) == ["The order created is a physical purchase: 12345"]

It's possible to perform OR, AND, NEG operation over conditions.

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

power_events-1.1.0.tar.gz (79.2 kB view details)

Uploaded Source

Built Distribution

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

power_events-1.1.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file power_events-1.1.0.tar.gz.

File metadata

  • Download URL: power_events-1.1.0.tar.gz
  • Upload date:
  • Size: 79.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for power_events-1.1.0.tar.gz
Algorithm Hash digest
SHA256 83cc0ae12277de8bd4504d5180dbf54e3160227fa67c21cae36dfcb6da0fadcc
MD5 6e38f9dc2f75d03e2cd41be432108a7e
BLAKE2b-256 cd9258aefbb3b61f3658b226daadd1dd0515b027a42f68528ab9e5dfca090945

See more details on using hashes here.

Provenance

The following attestation bundles were made for power_events-1.1.0.tar.gz:

Publisher: release.yml on mLetrone/power-events

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file power_events-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: power_events-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for power_events-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 abee676e81f9983f12c23bcd28c6f0a510a0778ab291f3993462a8596c40462d
MD5 fdc3d404ab2cec5bcf8a6941147a614c
BLAKE2b-256 b2a0774e989cdff84a662b104ddbd34a93b635fac93ed5455db4fd3bb54326eb

See more details on using hashes here.

Provenance

The following attestation bundles were made for power_events-1.1.0-py3-none-any.whl:

Publisher: release.yml on mLetrone/power-events

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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