Skip to main content

A collection of basic and advanced Python decorators

Project description

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:

  1. @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
    
  2. @memoize
    Caches the results of expensive function calls based on input arguments.

    @memoize
    def expensive_computation(x, y):
        return x ** y
    
  3. @log
    Logs the input arguments and return value of a function.

    @log
    def add_numbers(a, b):
        return a + b
    
  4. @singleton
    Ensures that only one instance of a class is created.

    @singleton
    class DatabaseConnection:
        pass
    
  5. @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:

  1. @throttle(seconds)
    Limits how frequently a function can be executed, useful for rate-limiting.

    @throttle(seconds=5)
    def update_status():
        pass
    
  2. @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
    
  3. @type_check(types)
    Enforces type checking for function arguments.

    @type_check((int, int))
    def multiply_numbers(a, b):
        return a * b
    
  4. @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
    
  5. @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.

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

decorator_collection-1.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

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

decorator_collection-1.0.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file decorator_collection-1.0.0.tar.gz.

File metadata

  • Download URL: decorator_collection-1.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for decorator_collection-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7da3d7ea93879c1fbfdc0249fbf44d7f59d39b914e9f46765fcbe65455f3bcc2
MD5 6892a19dfe231f50af39e20d5849bd9a
BLAKE2b-256 2b47c212a0126411d07bc57c66e11419150d0b4164cad81c268b589f6666df9b

See more details on using hashes here.

File details

Details for the file decorator_collection-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for decorator_collection-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9348fbf3399862489ed5ecac807de5fa185c40b7b4121ee69f066506671edac0
MD5 1bc61cecec8ee371573e97546614b030
BLAKE2b-256 4fd8868d98e003dea984b2fe6c0262d90b44b132b506c0dc68a5b8969eef7a05

See more details on using hashes here.

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