Skip to main content

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


Download files

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

Source Distribution

python_decorators_utils-0.1.5.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

python_decorators_utils-0.1.5-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

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

Hashes for python_decorators_utils-0.1.5.tar.gz
Algorithm Hash digest
SHA256 a089b99e35ac276af40592bb0778fe2f07cca0c3983d1d7e323bdecd8e0316dc
MD5 3168ac1d333b52fca17ebbccc6efe9e1
BLAKE2b-256 2a18a67e30a22dd194335b103d7517c7b5e453ee81f7275ace319ed5a2adbf11

See more details on using hashes here.

File details

Details for the file python_decorators_utils-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for python_decorators_utils-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7f5afa3012272815a05313503dea51eb057f4a1a13ff39b0b5258ef3d50e42b5
MD5 a3283f8391e0e683236df7eae5271eda
BLAKE2b-256 86b71f573f8601f09e6f6bbf0c6ac50b5a38460c889475ffb0e9a8c1fb758de6

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