Skip to content

Instantly share code, notes, and snippets.

@piranna
Created February 9, 2012 15:23
Show Gist options
  • Save piranna/1780651 to your computer and use it in GitHub Desktop.
Save piranna/1780651 to your computer and use it in GitHub Desktop.
Decorator to print the stacked level of the function and when it goes into and goes out. It only records the decorated functions and methods, not the full program stack...
def print_done(func):
print_done.level = 0
print_done.levels = []
def wrapper(*args, **kwargs):
# Entering function
print_done.level += 1
if print_done.level > len(print_done.levels):
print_done.levels.append(1)
else:
print_done.levels[-1] += 1
l = '.'.join(str(x) for x in print_done.levels)
print '[' + l + ']', func.__name__, "..."
# Exec the function
result = func(*args, **kwargs)
# Exiting function
if print_done.level < len(print_done.levels):
print_done.levels = print_done.levels[:print_done.level]
l = '.'.join(str(x) for x in print_done.levels)
print '[' + l + ']', func.__name__, "Done."
print_done.level -= 1
# Return function result
return result
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment