forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.py
More file actions
74 lines (62 loc) · 2.16 KB
/
debug.py
File metadata and controls
74 lines (62 loc) · 2.16 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import sys
import inspect
from pprint import pformat
from django.conf import settings
# A debug decorator, written by Paul Butler, taken from
# http://paulbutler.org/archives/python-debugging-with-decorators/
# Number of times to indent output
# A list is used to force access by reference
__report_indent = [4]
increment = 2
def set_indent(i):
__report_indent[0] = i
def trace(fn): # renamed from 'report' by henrik 16 Jun 2011
"""Decorator to print information about a function
call for use while debugging.
Prints function name, arguments, and call number
when the function is called. Prints this information
again along with the return value when the function
returns.
"""
def fix(s,n=32):
if len(s) > n+3:
s = s[:n]+"..."
s = s.replace('\n',' ')
return s
def wrap(*params,**kwargs):
call = wrap.callcount = wrap.callcount + 1
indent = ' ' * __report_indent[0]
fc = "%s(%s)" % (fn.__name__, ', '.join(
[fix(repr(a)) for a in params] +
["%s = %s" % (a, fix(repr(b))) for a,b in kwargs.items()]
))
print "%s* %s [#%s]" % (indent, fc, call)
__report_indent[0] += increment
ret = fn(*params,**kwargs)
__report_indent[0] -= increment
print "%s %s [#%s] ==> %s" % (indent, fc, call, repr(ret))
return ret
wrap.callcount = 0
if settings.DEBUG:
return wrap
else:
return fn
def show(name):
if settings.DEBUG:
frame = inspect.stack()[1][0]
value = eval(name, frame.f_globals, frame.f_locals)
indent = ' ' * (__report_indent[0])
print "%s%s: %s" % (indent, name, value)
def pprint(name):
if settings.DEBUG:
frame = inspect.stack()[1][0]
value = eval(name, frame.f_globals, frame.f_locals)
indent = ' ' * (__report_indent[0])
sys.stdout.write("%s%s:\n" % (indent, name))
lines = pformat(value).split('\n')
for line in lines:
sys.stdout.write("%s %s\n"%(indent, line))
def say(s):
if settings.DEBUG:
indent = ' ' * (__report_indent[0])
print "%s%s" % (indent, s)