Decorator Collection
Overview
The Decorator Collection package provides a comprehensive set of basic and advanced Python decorators that simplify a variety of tasks. Whether you're looking to implement caching, retries, logging, rate limiting, or more complex control flows like circuit breakers, this package has you covered.
Key Features:
-
Basic decorators:
- Retry on failures, memoization, logging function input/output, and singleton pattern implementation.
-
Advanced decorators:
- Throttling and debouncing function calls, type checking, enforcing rate limits, and circuit-breaking to handle failures.
The package is modularized into two main parts:
basic_decorators: Includes common decorators for everyday use.advanced_decorators: Provides decorators for more complex logic and control flows.
Installation
You can install the Decorator Collection from PyPI by running:
pip install decorator_collection
Usage
After installation, you can use decorators from either the basic_decorators or advanced_decorators modules. Here are some example use cases:
Example 1: Basic Decorators
from decorator_collection.basic_decorators import retry, memoize, log
# Memoization example
@memoize
def compute_power(x, y):
return x ** y
# Retry example
@retry(exceptions=(ValueError,), max_attempts=3, delay=2)
def risky_calculation():
raise ValueError("An error occurred!")
# Log decorator example
@log
def add(a, b):
return a + b
Example 2: Advanced Decorators
from decorator_collection.advanced_decorators import throttle, debounce, type_check
# Throttle example: Ensures this function only runs at most once per 5 seconds.
@throttle(seconds=5)
def send_email():
print("Sending email...")
# Debounce example: Only runs if there's no new call within 1 second.
@debounce(wait_time=1)
def search(query):
print(f"Searching for {query}...")
# Type-checking decorator
@type_check((int, int))
def multiply(a, b):
return a * b
Modules
basic_decorators
The basic_decorators.py module includes essential decorators for common tasks. Here's a list of available decorators:
-
@retry(exceptions, max_attempts, delay)
Automatically retries a function if it raises specific exceptions.@retry(exceptions=(ValueError,), max_attempts=3, delay=2) def risky_function(): pass
-
@memoize
Caches the results of expensive function calls based on input arguments.@memoize def expensive_computation(x, y): return x ** y
-
@log
Logs the input arguments and return value of a function.@log def add_numbers(a, b): return a + b
-
@singleton
Ensures that only one instance of a class is created.@singleton class DatabaseConnection: pass
-
@time_logger
Logs the time taken by a function to execute.@time_logger def long_running_task(): time.sleep(2)
advanced_decorators
The advanced_decorators.py module contains more complex decorators for advanced use cases:
-
@throttle(seconds)
Limits how frequently a function can be executed, useful for rate-limiting.@throttle(seconds=5) def update_status(): pass
-
@debounce(wait_time)
Ensures that a function only runs after a specified period of inactivity, useful for search input fields.@debounce(wait_time=1) def process_search(query): pass
-
@type_check(types)
Enforces type checking for function arguments.@type_check((int, int)) def multiply_numbers(a, b): return a * b
-
@circuit_breaker(failure_threshold, recovery_timeout)
Implements a circuit breaker pattern to prevent repeatedly calling failing services.@circuit_breaker(failure_threshold=2, recovery_timeout=10) def call_external_service(): pass
-
@rate_limit(max_per_second)
Limits the number of times a function can be executed per second.@rate_limit(max_per_second=5) def api_request(): pass
Requirements
- Python 3.11+
jsonschema(required for JSON validation decorators)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
We welcome contributions! Please email me and I will look at your request.
Author
- Aiden Metcalfe
Feel free to reach out with suggestions, bug reports, or feature requests.
Changelog
v1.0.0
- Initial release with 20 basic and 20 advanced decorators.
Metadata
Release files for decorator-collection 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| decorator_collection-1.0.0.tar.gz | 8.2 kB | Without provenance | Details for decorator_collection-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| decorator_collection-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for decorator_collection-1.0.0-py3-none-any.whl |
Total release size: 16.0 kB
Release files / decorator_collection-1.0.0.tar.gz
| Download URL | decorator_collection-1.0.0.tar.gz |
|---|---|
| Size | 8.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7da3d7ea93879c1fbfdc0249fbf44d7f59d39b914e9f46765fcbe65455f3bcc2
|
|
BLAKE2b-256 checksum How to use checksums |
2b47c212a0126411d07bc57c66e11419150d0b4164cad81c268b589f6666df9b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.9
|
Release files / decorator_collection-1.0.0-py3-none-any.whl
| Download URL | decorator_collection-1.0.0-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9348fbf3399862489ed5ecac807de5fa185c40b7b4121ee69f066506671edac0
|
|
BLAKE2b-256 checksum How to use checksums |
4fd8868d98e003dea984b2fe6c0262d90b44b132b506c0dc68a5b8969eef7a05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.11.9
|
Security for decorator-collection 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.