Skip to content

Instantly share code, notes, and snippets.

@kjaquier
Last active May 24, 2016 15:47
Show Gist options
  • Save kjaquier/05fd58d651520f3a8fc94d6ba8968e91 to your computer and use it in GitHub Desktop.
Save kjaquier/05fd58d651520f3a8fc94d6ba8968e91 to your computer and use it in GitHub Desktop.
Prints name, arguments and result when decorated function is called.
function trace(f) {
return function() {
var args = Array.from(arguments);
var res = f.apply(null, args);
console.debug("%s(%O) = %O", f.name, args, res);
return res;
};
}
def trace(func):
def wrapper(*args, **kwargs):
s = "{func_name}({args}{sep}{kwargs})".format(
func_name=func.__name__,
args=", ".join("{}".format(repr(x)) for x in args),
sep=(", " if args and kwargs else ""),
kwargs=", ".join("{}={}".format(k, repr(v)) for k, v in kwargs.items())
)
res = ""
try:
res = func(*args, **kwargs)
print(s, "=", repr(res))
return res
except:
print(s)
raise
wrapper.__name__ = func.__name__
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment