A high-level Python decorator library for performance, resilience, debugging, and more.
Project description
wdecorators
wdecorators is a high-level Python decorator library providing a collection of ready-to-use decorators for performance profiling, resilience (retry, timeout, rate-limiting), debugging, input sanitization, structured logging with Graylog, caching, and periodic task scheduling.
Installation
pip install wdecorators
Optional extras
# For the periodic task scheduler dashboard (FastAPI + Uvicorn + JWT)
pip install wdecorators[scheduler]
# For the Graylog FastAPI middleware
pip install wdecorators[graylog]
Quick Start
from wdecorators import benchmark, retry, time_execution, singleton
@benchmark
def my_function():
return sum(range(1000))
@retry(times=3)
def unreliable_function():
import random
if random.random() < 0.5:
raise ConnectionError("Network issue")
return "OK"
@singleton
class Database:
def __init__(self):
print("Connecting to database...")
print(my_function())
print(unreliable_function())
db1 = Database()
db2 = Database()
print(db1 is db2) # True
Decorator Reference
Performance
| Decorator | Description |
|---|---|
@benchmark |
Measures and prints function execution time |
@time_execution |
Same as benchmark (alias) |
@profile_memory |
Traces memory usage with tracemalloc |
@memoize |
In-memory cache keyed by arguments |
@disk_cache(filename) |
Persistent pickle-based cache on disk |
Resilience
| Decorator | Description |
|---|---|
@retry(times) |
Retries on any exception up to times attempts |
@retry_on_exception(retries, delay, exceptions) |
Retries on specific exception types with delay |
@timeout(seconds) |
Raises TimeoutException if execution exceeds limit |
@rate_limit(calls_per_second) |
Limits invocation rate |
@silent_fail |
Silences exceptions and returns None |
@log_exceptions |
Catches exceptions and logs error message |
Debugging
| Decorator | Description |
|---|---|
@debug_arguments |
Prints function arguments on each call |
@trace_execution |
Prints entry/exit trace messages |
@log_return |
Prints the return value |
@count_calls |
Tracks and prints call count |
@validate_types(**types) |
Runtime type checking for keyword arguments |
Security
| Decorator | Description |
|---|---|
@sanitize_input |
HTML-escapes all string arguments |
@require_authentication(user_dict) |
Checks authenticated flag before execution |
Utilities
| Decorator | Description |
|---|---|
@singleton |
Ensures a class has only one instance |
@to_json |
Converts return value to JSON string |
@periodic_execution(interval) |
Runs function periodically in a background thread |
Graylog Logging
| Decorator / Function | Description |
|---|---|
init_logger(name, graylog_host, log_level) |
Configures loguru with Graylog UDP + console sinks |
@log_exceptions(context, enable_raise) |
Logs exceptions to Graylog via loguru |
@log_execution_time(context) |
Logs function duration to Graylog |
LoggingMiddleware |
FastAPI middleware for request/response logging |
Periodic Task Scheduler
| Class / Method | Description |
|---|---|
Periodic_task_sched() |
Creates a scheduler instance |
.set_database(db_config) |
Configures SQLite or PostgreSQL backend |
.periodic_execution(interval, priority, ...) |
Decorator to register periodic tasks |
.start_api() |
Launches a FastAPI dashboard (requires [scheduler] extras) |
.verify_admin(token) |
JWT-based token verification for API routes |
Examples
Check the examples/ directory for runnable code covering every decorator:
# Run any example
python examples/benchmark_example.py
python examples/retry_example.py
python examples/timeout_example.py
python examples/singleton_example.py
# ... and many more
Periodic Tasks
from wdecorators import Periodic_task_sched
import time
controller = Periodic_task_sched()
controller.set_database()
@controller.periodic_execution(interval=5, priority="high", enable_api=True)
def critical_task():
print("Critical task running...")
@controller.periodic_execution(interval=10, priority="medium")
def secondary_task():
print("Secondary task running...")
critical_task()
secondary_task()
controller.start_api()
time.sleep(60)
Graylog Structured Logging
from wdecorators import init_logger, log_exceptions, logger
init_logger("my_app", graylog_host="192.168.1.100")
@log_exceptions(context={"send_to_graylog": True})
def process_order(order_id: int):
logger.bind(send_to_graylog=True).info(f"Processing order {order_id}")
# ... business logic
Project Structure
wdecorators/
├── wdecorators/
│ ├── __init__.py # Public API exports
│ ├── general/ # General-purpose decorators
│ │ ├── __init__.py
│ │ ├── benchmark.py
│ │ ├── count_calls.py
│ │ ├── debug_arguments.py
│ │ ├── disk_cache.py
│ │ ├── log_exceptions.py
│ │ ├── log_return.py
│ │ ├── memoize.py
│ │ ├── periodic_execution.py
│ │ ├── profile_memory.py
│ │ ├── rate_limit.py
│ │ ├── require_authentication.py
│ │ ├── retry.py
│ │ ├── retry_on_exception.py
│ │ ├── sanitize_input.py
│ │ ├── silent_fail.py
│ │ ├── singleton.py
│ │ ├── time_execution.py
│ │ ├── timeout.py
│ │ ├── to_json.py
│ │ ├── trace_execution.py
│ │ └── validate_types.py
│ ├── graylog/ # Graylog GELF logging integration
│ │ ├── __init__.py
│ │ ├── handler.py
│ │ ├── loggerg.py
│ │ └── middleware.py
│ ├── log_calls/ # Simple call logging decorator
│ │ └── log_calls.py
│ └── periodic_scheduller/ # Periodic task scheduler with dashboard
│ ├── __init__.py
│ └── controller.py
├── examples/ # Runnable examples for every decorator
│ ├── benchmark_example.py
│ ├── ...
│ ├── graylog/
│ │ ├── example_fastapi.py
│ │ └── example_script.py
│ └── periodic_task/
│ └── basic.py
├── pyproject.toml
└── README.md
Development
git clone https://github.com/wisrovi/wdecorators.git
cd wdecorators
pip install -e ".[scheduler]"
Code quality
pip install isort black
isort .
black .
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wdecorators-1.0.0.tar.gz.
File metadata
- Download URL: wdecorators-1.0.0.tar.gz
- Upload date:
- Size: 62.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
035cc8e66358e40912b9b658f2c240ce46ec5c0b9b18172f514ba7c771e04a8e
|
|
| MD5 |
d9dc5020d3ad2511aa5434bf555f58b6
|
|
| BLAKE2b-256 |
8dd00c72007ffd871add84dbe5824d9aa4d4693f5a1095beac2f6e0bb0331bac
|
File details
Details for the file wdecorators-1.0.0-py3-none-any.whl.
File metadata
- Download URL: wdecorators-1.0.0-py3-none-any.whl
- Upload date:
- Size: 47.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a05ed7102b4eb457586ee291b3d3bf7f7dce361263a7cecbdc16b1e550652eac
|
|
| MD5 |
c2d7cd2391a8e23f039e0df5206822ba
|
|
| BLAKE2b-256 |
daac35d91887cfe219553ff5b28c2ee49099dc118f6621a4344e5f3c49ae606d
|