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.2.0.tar.gz (82.5 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.2.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: power_events-1.2.0.tar.gz
  • Upload date:
  • Size: 82.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for power_events-1.2.0.tar.gz
Algorithm Hash digest
SHA256 a9fd24c3a2f53a195602b55d3391ea926959e85a066335baa0cce7bed18935e0
MD5 38c0bd0c10bf397c46fc922a39f2bbf2
BLAKE2b-256 1658e18cabbc517f99185d9a8160109bbb4d17fdea9b3fc1fc3806b1111ba512

See more details on using hashes here.

Provenance

The following attestation bundles were made for power_events-1.2.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.2.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for power_events-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82f24f3ca42d9da17eba8a55aa17ec8f7d0ca4b38aaaa96f9776efb15858e740
MD5 424da76f062ed1848984e280c8ef9456
BLAKE2b-256 2f96d7ddd1fa62d7719af344e5d96b928113cab046b2e8f56069531e45d7c51e

See more details on using hashes here.

Provenance

The following attestation bundles were made for power_events-1.2.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