Created
February 9, 2012 15:23
-
-
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...
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
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