Skip to main content

Add your description here

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.0.0.tar.gz (72.4 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.0.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: power_events-1.0.0.tar.gz
  • Upload date:
  • Size: 72.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for power_events-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3897ef6771fe2798c6789d73d64a4b9849ebd600b27ba619480a0ab27a998ee9
MD5 5e8a8b8d41cc030da884772bd3e453e5
BLAKE2b-256 16b2d69afd93214a1b3f0c31e51437b4c1fe4e4721b5b7df3f55309fce951e12

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: power_events-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for power_events-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f9adf27c9df69a225558a73a21484813d6db053b1e9938ce0230c84f14c9ce80
MD5 3e50054f0a84f840c96bd4f5d099b108
BLAKE2b-256 4b60feb5f6f976aca2613aead4950ce7f30e841563bcdf2c345aeb1bb450c6e2

See more details on using hashes here.

Provenance

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