Skip to main content
image

wdecorators

PyPI version Python Versions License: MIT

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(auto_database, handle_signals, max_workers) Creates a scheduler instance with auto DB, signals, and thread pool
.set_database(db_config) Configures SQLite or PostgreSQL backend
.periodic_execution(interval, run_at, cron, allow_concurrent, on_success, on_error) Decorator to register periodic tasks with cron, async, & callbacks
.start_api() Launches FastAPI dashboard and /metrics Prometheus endpoint
.stop_all() Stops all registered running task executors and thread pools
.run_forever(poll_interval) Safely blocks main thread and handles graceful shutdown on interrupt
.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/performance/benchmark_example.py
python examples/resilience/retry_example.py
python examples/periodic_task/full_featured.py
# ... and many more organized by category

Periodic Tasks

from wdecorators import Periodic_task_sched
import asyncio

controller = Periodic_task_sched(auto_database=True, handle_signals=True, max_workers=5)

@controller.periodic_execution(cron="*/1 * * * *", allow_concurrent=False, enable_api=True)
async def async_cron_task():
    print("Async cron task running...")
    await asyncio.sleep(1)

@controller.periodic_execution(run_at=["04:00", "12:00", "20:00"])
def daily_reports():
    print("Daily report task running...")

async_cron_task()
daily_reports()
controller.start_api()

controller.run_forever()

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
│   ├── graylog/              # Graylog GELF logging integration
│   ├── log_calls/            # Simple call logging decorator
│   └── periodic_scheduller/  # Periodic task scheduler with dashboard & metrics
│       ├── __init__.py
│       └── controller.py
├── examples/                 # Categorized runnable examples
│   ├── debugging/            # Debugging and tracing examples
│   ├── graylog/              # Graylog integration examples
│   ├── performance/          # Benchmarking and caching examples
│   ├── periodic_task/        # Periodic task scheduler examples (basic, advance, daily_schedule, full_featured)
│   ├── resilience/           # Retry, timeout, rate-limiting examples
│   ├── security/             # Input sanitization and authentication examples
│   ├── utilities/            # Singleton, JSON, run_once examples
│   └── validation/           # Runtime type & length validation examples
├── pyproject.toml
└── README.md

Key Technologies

  • Python 3.8+: Core runtime.
  • FastAPI & Uvicorn: Web dashboard and server for monitoring periodic task schedulers.
  • PyJWT & Jinja2: Authentication and templating for scheduler dashboard.
  • Loguru: Advanced logging engine with Graylog GELF support.
  • SQLite / PostgreSQL: Database backends for task logging and execution history.
  • Pytest & Coverage: Unit testing framework and test coverage reporting.

Development

git clone https://github.com/wisrovi/wdecorators.git
cd wdecorators
pip install -e ".[scheduler]"

Code quality

pip install isort black
isort .
black .

License

MIT © William Steve Rodriguez Villamizar

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wdecorators-1.1.0.tar.gz (71.2 kB view details)

Uploaded Source

Built Distribution

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

wdecorators-1.1.0-py3-none-any.whl (51.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wdecorators-1.1.0.tar.gz
  • Upload date:
  • Size: 71.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.5

File hashes

Hashes for wdecorators-1.1.0.tar.gz
Algorithm Hash digest
SHA256 bad830ea2f5b7578ecb5c780df2f8c8245d845becb2f5f2873029b7508fc332a
MD5 aa9cab77d7915f9097dc780d61708be7
BLAKE2b-256 d0eb179ea9fe08e73059f9d96897615a945247a20213fe42896ac7077ae7ecf0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wdecorators-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 51.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.5

File hashes

Hashes for wdecorators-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e1f2f458fc849f00fd4b43957ce6b2115eba4cf425f3e5a5adb946ba0ca3ad2
MD5 967f23c87521446a6ce6fc2c3d7a3008
BLAKE2b-256 beabe0e128974e92f1776a47c429ac1057fc1ac5c9cf8c38f647e728c61e1e76

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 files

1.0.0

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page