A collection of useful Python decorators including timing and caching
Project description
Python Decorators Utils
A collection of useful Python decorators including:
- Timing decorator: Measures function execution time
- Cache decorator: Implements function result caching
- Debug decorator: Logs function calls and returns
- Execution counter: Counts function calls
- Retry decorator: Retries failed function calls
Installation
pip install python-decorators-utils
Usage
from python_decorators_utils import timing_decorator, cache_decorator
from python_decorators_utils import debug_decorator, execution_counter
from python_decorators_utils import retry_decorator
# Timing Decorator - Measures execution time
@timing_decorator
def slow_function():
time.sleep(1)
return "done"
# Cache Decorator - Memoizes results
@cache_decorator
def fibonacci(n):
if n < 2: return n
return fibonacci(n-1) + fibonacci(n-2)
# Debug Decorator - Logs function calls
@debug_decorator
def greet(name):
return f"Hello, {name}"
# Execution Counter - Tracks calls
@execution_counter
def counted_function():
return "I'm being counted!"
print(counted_function.get_count()) # Get number of calls
# Retry Decorator - Handles failures
@retry_decorator(max_retries=3)
def may_fail():
# Will retry up to 3 times if fails
return risky_operation()
Features
- All decorators preserve function metadata using
functools.wraps - Type hints supported
- Compatible with Python 3.11+
- Can be used individually or stacked together
- Uses closures for maintaining state (like in execution counter)
Contributing
Pull requests are welcome. Please ensure tests pass before submitting.
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 python_decorators_utils-0.1.5.tar.gz.
File metadata
- Download URL: python_decorators_utils-0.1.5.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a089b99e35ac276af40592bb0778fe2f07cca0c3983d1d7e323bdecd8e0316dc
|
|
| MD5 |
3168ac1d333b52fca17ebbccc6efe9e1
|
|
| BLAKE2b-256 |
2a18a67e30a22dd194335b103d7517c7b5e453ee81f7275ace319ed5a2adbf11
|
File details
Details for the file python_decorators_utils-0.1.5-py3-none-any.whl.
File metadata
- Download URL: python_decorators_utils-0.1.5-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f5afa3012272815a05313503dea51eb057f4a1a13ff39b0b5258ef3d50e42b5
|
|
| MD5 |
a3283f8391e0e683236df7eae5271eda
|
|
| BLAKE2b-256 |
86b71f573f8601f09e6f6bbf0c6ac50b5a38460c889475ffb0e9a8c1fb758de6
|