Skip to main content

Server-side decorators for caching, tracking, and queue emission. Polyglot sibling of the Node `server-decorator` npm package.

Project description

server-decorator (Python)

Server-side decorators for caching, tracking, and queue emission. Polyglot sibling of the Node server-decorator npm package — both share the wire-format contracts in ../contracts.

Install

pip install server-decorator               # core
pip install server-decorator[redis]        # + redis adapter
pip install server-decorator[rabbitmq]     # + aio-pika adapter
pip install server-decorator[kafka]        # + aiokafka adapter
pip install server-decorator[all]          # all adapters

Requires Python 3.10+. asyncio only — no trio/anyio (Decision #10).

Quick start

Method form

from server_decorator import tracking, cache, emit_on_success, CACHE_MISS

class InMemoryCache:
    def __init__(self): self._d = {}
    def get(self, key): return self._d.get(key, CACHE_MISS)
    def set(self, key, val, ttl): self._d[key] = val

class OrderService:
    @tracking
    @cache(InMemoryCache(), ttl=300)
    @emit_on_success(use_events=True)
    async def create_order(self, customer_id: str, total: float) -> dict:
        return {"customer_id": customer_id, "total": total, "status": "pending"}

Class form (auto-wrap every public method)

from server_decorator import tracking_class

@tracking_class()
class OrderService:
    async def create_order(self, ...): ...    # auto-wrapped
    async def cancel_order(self, ...): ...    # auto-wrapped
    def _internal(self): ...                  # SKIPPED (leading underscore)

tracking_class, cache_class, and emit_on_success_class accept include, exclude, and include_private to override the default predicates.

Sync vs async

Method-level decorators auto-detect coroutines via inspect.iscoroutinefunction and wrap accordingly (Decision #5). For @emit_on_success(use_queue=True) on a sync method called outside an event loop, the side effect runs on a single-worker ThreadPoolExecutor so the caller doesn't block (D-C). To drain in-flight emits at process exit:

import atexit
from server_decorator.decorators.emit_on_success import _EMIT_EXECUTOR
atexit.register(_EMIT_EXECUTOR.shutdown, wait=True)

Caching None / falsy values

CacheAdapter.get returns CACHE_MISS (a module-level sentinel) when a key is absent — None/0/""/False are valid cached values. See ../contracts/rules/cache-adapter.md.

License

MIT.

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

server_decorator-2.0.0.tar.gz (126.9 kB view details)

Uploaded Source

Built Distribution

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

server_decorator-2.0.0-py3-none-any.whl (17.3 kB view details)

Uploaded Python 3

File details

Details for the file server_decorator-2.0.0.tar.gz.

File metadata

  • Download URL: server_decorator-2.0.0.tar.gz
  • Upload date:
  • Size: 126.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for server_decorator-2.0.0.tar.gz
Algorithm Hash digest
SHA256 42bd14b58afe1c522dbf361f139b6fcce5b482abfd34ea2f3f68358d27f84fa3
MD5 d5e54c4d91ea3abc93bb9595978941cb
BLAKE2b-256 0f16ed3476d53c7f6848d140977c8142d9f634b685886c980b776954ea664e02

See more details on using hashes here.

Provenance

The following attestation bundles were made for server_decorator-2.0.0.tar.gz:

Publisher: publish-python.yml on montionugera/node-server-decorator

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

File details

Details for the file server_decorator-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for server_decorator-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0af7511b5d23e165e60f97effce892d3f2a77cbbbe7b84f35c8c135bba253d32
MD5 8cacc2b36a3d2b68942a8761872f144d
BLAKE2b-256 1778a4edd1126b33f005b70e78654f36645a013ff32519a0b5e7e9d0d685bfcf

See more details on using hashes here.

Provenance

The following attestation bundles were made for server_decorator-2.0.0-py3-none-any.whl:

Publisher: publish-python.yml on montionugera/node-server-decorator

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