Source code for uw.local.debug

"""Debug utilities.

Functions which may be useful for debugging.
"""

from functools import wraps
import timeit

[docs]def stopwatch (msg): """Create a decorator which may be used to time execution of a function. :param str msg: A message to print out when the wrapped function is timed. :return: A decorator which times the function to which it is applied and prints the time along with the specified message. """ def decorator (f): @wraps (f) def wrapper (*args, **keys): start_time = timeit.default_timer () result = f (*args, **keys) print ('%s: %s' % (msg, timeit.default_timer () - start_time)) return result return wrapper return decorator
[docs]def sw (msg, f): """Call a function and time its execution. :param str msg: A message to print out when the wrapped function is timed. :param f: The function to time. This must expect no parameters. Providing a no-parameter lambda expression is likely to be useful. :return: The result of calling the wrapped function. """ return stopwatch (msg) (f) ()