Skip to content

Instantly share code, notes, and snippets.

@kennethreitz
Created September 4, 2010 17:17
Show Gist options
  • Save kennethreitz/565327 to your computer and use it in GitHub Desktop.
Save kennethreitz/565327 to your computer and use it in GitHub Desktop.
The greatest trace decorator of all time
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from decorator import decorator #http://pypi.python.org/pypi/decorator
@decorator
def trace(f, *args, **kwargs):
"""Print scope, call, and argument information."""
_scope = inspect.getmodule(f).__name__
# guess that function is a method of it's class
if f.func_name in dir(args[0].__class__):
_scope += '.' + args[0].__class__.__name__
_scope += '.' + f.__name__
else:
_scope += '.' + f.__name__
print("calling %s() with \nargs: %s \nkwargs: %s" % (_scope, args, kwargs))
return f(*args, **kwargs)
# @trace added above function definition:
calling haystack.submodule.cli.start() with
args: ({'opt1': '', 'in_files': 'test.txt', 'cherry_pick': False},)
kwargs: {'debug': True}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment