profilehooks

raw JSON →
1.13.0 verified Fri May 01 auth: no python

A collection of decorators for profiling, timing, and tracing individual functions. Current version 1.13.0, requires Python >=3.7. Release cadence is intermittent, with minor updates.

pip install profilehooks
error AttributeError: module 'profilehooks' has no attribute 'cover'
cause The 'cover' function was renamed to 'coverage' in version 1.11.0.
fix
Use 'from profilehooks import coverage' instead of 'cover'.
error TypeError: 'NoneType' object is not callable when using @profile on async function
cause The @profile decorator does not support async functions.
fix
Remove @profile from async functions, or use @timecall with appropriate handling.
gotcha When using @timecall, the decorator prints timing output to stderr by default. If you capture stdout only, you'll miss the output.
fix Import sys and direct output: @timecall(stdout=sys.stdout) or use logging.
deprecated The 'cover' decorator is deprecated. Use 'coverage' instead.
fix Replace @cover with @coverage.
gotcha The @profile decorator does NOT work with async functions. Using it on an async def will raise an error or produce no profiling output.
fix Do not use @profile on async functions; use @timecall or manual profiling.

Basic usage of @timecall and @profile decorators.

from profilehooks import timecall, profile

def slow_function(n):
    total = 0
    for i in range(n):
        total += i ** 2
    return total

# Time a single call
@timecall
def timed_slow(n):
    return slow_function(n)

timed_slow(100000)

# Profile a function
@profile
def profiled_slow(n):
    return slow_function(n)

profiled_slow(100000)