Skip to content

Instantly share code, notes, and snippets.

@JeffreyMFarley
Last active October 11, 2023 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffreyMFarley/6d6ec18b064504827ad389ebbe2d20fe to your computer and use it in GitHub Desktop.
Save JeffreyMFarley/6d6ec18b064504827ad389ebbe2d20fe to your computer and use it in GitHub Desktop.
# Modified from https://cscheid.net/2017/12/11/minimal-tracing-decorator-python-3.html
import inspect
trace_indent = 0
def tracing(f):
sig = inspect.signature(f)
def do_it(*args, **kwargs):
global trace_indent
ws = ' ' * (trace_indent * 2)
params = ','.join([
f"{param.name}: {repr(args[ix-1])}"
for ix, param in enumerate(sig.parameters.values())
if param.name != 'self'
])
print(f"{ws}ENTER {f.__module__}::{f.__qualname__}({params}):")
sys.stdout.flush()
trace_indent += 1
result = f(*args, **kwargs)
trace_indent -= 1
print(f"{ws}EXIT {f.__module__}::{f.__qualname__}:")
sys.stdout.flush()
return result
return do_it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment