Last active
November 25, 2019 07:38
-
-
Save tweakimp/423252b2b1362cd64bd7bc83852d0dde to your computer and use it in GitHub Desktop.
decorator magic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import inspect | |
import time | |
import cProfile | |
import time | |
from contextlib import contextmanager | |
import pstats | |
import sys | |
def profile(func): | |
""" | |
Decorator. | |
Prints cProfile. | |
""" | |
@functools.wraps(func) | |
def wrap(*args, **kwargs): | |
p = cProfile.Profile() | |
result = p.runcall(func, *args, **kwargs) | |
# p.print_stats() | |
with open("profile.log", "a+") as file: | |
print(f"PROFILE {func.__qualname__}\n", file=file) | |
stats = pstats.Stats(p, stream=file) | |
sortby = "cumulative" | |
stats.strip_dirs().sort_stats(sortby).print_stats(0.3) | |
return result | |
return wrap | |
@contextmanager | |
def time_this(label): | |
start = time.perf_counter_ns() | |
try: | |
yield | |
finally: | |
end = time.perf_counter_ns() | |
print(f"TIME {label}: {(end-start)/1000000000} sec") | |
def trace(original_function=None,*, name="name", file="s"): | |
def decorate(func): | |
@functools.wraps(func) | |
def wrap(*args, **kwargs): | |
caller = inspect.currentframe().f_back.f_code | |
name = caller.co_name | |
file = caller.co_filename | |
line = caller.co_firstlineno | |
argslist = ", ".join(f"{x}" for x in args) | |
kwargslist = ", ".join(f"{k}={v}" for k, v in kwargs.items()) | |
and_string = " and " if argslist and kwargslist else "" | |
start = time.perf_counter_ns() | |
result = func(*args, **kwargs) | |
end = time.perf_counter_ns() | |
print(f"TRACE:\t'{func.__qualname__}' called by '{name}' ({file}:{line})") | |
print(f"\tInput: {argslist}{and_string}{kwargslist}") | |
print(f"\tOutput: {result}") | |
print(f"\tTime {(end-start)/1_000_000_000} sec") | |
return result | |
return wrap | |
if original_function: | |
return decorate(original_function) | |
return decorate | |
# def trace(func): | |
# """ | |
# Print a trace of the input and output of a function in one line. | |
# """ | |
# def traced_func(*args, **kwargs): | |
# result = func(*args, **kwargs) | |
# if len(args) is not 0: | |
# argslist = ", ".join(f"{x}" for x in args) | |
# if len(kwargs) is not 0: | |
# argslist = argslist + ", " if len(kwargs) is not 0 else "" | |
# else: | |
# argslist = "" | |
# if len(kwargs) is not 0: | |
# kwargslist = ", ".join([f"{k}={v}" for k, v in kwargs.items()]) | |
# else: | |
# kwargslist = "" | |
# print(f"{func.__name__}({argslist}{kwargslist}) = {result}") | |
# return result | |
# return traced_func | |
@trace(name="d") | |
def test(*args, **kwargs): | |
# name = dir(inspect.currentframe().f_back.f_code) | |
# print(f"NAME {name}") | |
time.sleep(0.3) | |
return 3 | |
@trace | |
@profile | |
def test2(*args, **kwargs): | |
# name = dir(inspect.currentframe().f_back.f_code) | |
# print(f"NAME {name}") | |
time.sleep(0.3) | |
return 3 | |
def outer(*args, **kwargs): | |
test(*args, **kwargs) | |
test2(*args, **kwargs) | |
return 4 | |
a = outer() | |
a = outer("a", 3,) | |
a = outer(b=7, c="d") | |
a = outer("a", 3, b=7, c="d") | |
# print(a) | |
from functools import wraps | |
import logging | |
def logged(level, name=None, message=None): | |
""" | |
Add logging to a function. level is the logging | |
level, name is the logger name, and message is the | |
log message. If name and message aren't specified, | |
they default to the function's module and name. | |
""" | |
def decorate(func): | |
logname = name if name else func.__module__ | |
log = logging.getLogger(logname) | |
logmsg = message if message else func.__name__ | |
@wraps(func) | |
def wrapper(*args, **kwargs): | |
log.log(level, logmsg) | |
return func(*args, **kwargs) | |
return wrapper | |
return decorate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment