-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogged-debug.py
More file actions
48 lines (38 loc) · 1.09 KB
/
logged-debug.py
File metadata and controls
48 lines (38 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import pprint
import inspect
import logging
import functools
logging.basicConfig(level=logging.DEBUG)
def debug(function):
"""
debug decorator print function information and parameters.
"""
@functools.wraps(function)
def _debug(*args, **kwargs):
result = None
try:
result = function(*args, **kwargs)
finally:
# extract the signarture from the function
signature = inspect.signature(function)
# fill the arguments
arguments = signature.bind(*args, **kwargs)
arguments.apply_defaults()
logging.debug(
"%s(%s): %s"
% (
function.__qualname__,
", ".join(
"%s=%r" % (k, v)
for k, v in arguments.arguments.items()
),
pprint.pformat(result) if result is not None else "",
)
)
return _debug
@debug
def spam(a, b=123):
return "this message is a spam"
spam(1)
spam(1, 456)
spam(b=1, a=456)