Created
March 12, 2015 14:35
-
-
Save rnixx/491bf758918e3a1e13e9 to your computer and use it in GitHub Desktop.
Python: function debug decorator
This file contains hidden or 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 debug(func): | |
def wrapped(*args, **kwargs): | |
name = '{0}.{1}'.format( | |
func.__module__, | |
func.func_name | |
) | |
if args: | |
inst = args[0] | |
inst_func = getattr(inst, func.func_name, None) | |
unwrapped_func = getattr(inst_func, '_original_func', None) | |
if unwrapped_func is func: | |
name = u'{0}.{1}.{2}'.format( | |
inst.__module__, | |
inst.__class__.__name__, | |
func.func_name | |
) | |
Logger.debug(u'Debug call: {0}() [args={1}][kwargs={2}]'.format( | |
name, | |
unicode(args), | |
unicode(kwargs) | |
)) | |
f_result = func(*args, **kwargs) | |
Logger.debug(u'Debug return: {0}() [{1}]'.format( | |
name, | |
unicode(f_result) | |
)) | |
return f_result | |
wrapped._original_func = func | |
wrapped.__doc__ = func.__doc__ | |
return wrapped |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment