Skip to content

Commit ba95520

Browse files
committed
Updated version of debug.py, which adds timing and profiling support functions.
- Legacy-Id: 4935
1 parent b8f7304 commit ba95520

1 file changed

Lines changed: 73 additions & 24 deletions

File tree

debug.py

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
21
import sys
2+
import time as timeutils
33
import inspect
44
import syslog
55
from pprint import pformat
6+
import cProfile
7+
try:
8+
from django.conf import settings
9+
debug = settings.DEBUG
10+
except ImportError:
11+
debug = True
12+
from decorator import decorator
613

714
# A debug decorator, written by Paul Butler, taken from
815
# http://paulbutler.org/archives/python-debugging-with-decorators/
9-
# Additional functions and decorator functionality added by
10-
# Henrik Levkowetz
16+
17+
18+
increment = 2
1119

1220
# Number of times to indent output
1321
# A list is used to force access by reference
14-
__report_indent = [4]
15-
increment = 2
16-
debug = True
17-
#syslog.openlog("debug", syslog.LOG_PID, syslog.LOG_USER)
22+
_report_indent = [4]
23+
_mark = timeutils.clock()
1824

1925
def set_indent(i):
20-
__report_indent[0] = i
26+
_report_indent[0] = i
2127

2228
def trace(fn): # renamed from 'report' by henrik 16 Jun 2011
2329
"""Decorator to print information about a function
@@ -27,59 +33,102 @@ def trace(fn): # renamed from 'report' by henrik 16 Jun 2011
2733
again along with the return value when the function
2834
returns.
2935
"""
30-
def fix(s,n=32):
36+
def fix(s,n=36):
37+
import re
38+
s = re.sub(r'\\t', ' ', s)
39+
s = re.sub(r'\s+', ' ', s)
3140
if len(s) > n+3:
3241
s = s[:n]+"..."
33-
s = s.replace('\n',' ')
3442
return s
35-
def wrap(*params,**kwargs):
43+
def wrap(fn, *params,**kwargs):
3644
call = wrap.callcount = wrap.callcount + 1
3745

38-
indent = ' ' * __report_indent[0]
39-
fc = "%s(%s)" % (fn.__name__, ', '.join(
46+
indent = ' ' * _report_indent[0]
47+
fc = "%s.%s(%s)" % (fn.__module__, fn.__name__, ', '.join(
4048
[fix(repr(a)) for a in params] +
4149
["%s = %s" % (a, fix(repr(b))) for a,b in kwargs.items()]
4250
))
4351

44-
print "%s* %s [#%s]" % (indent, fc, call)
45-
__report_indent[0] += increment
52+
sys.stderr.write("%s* %s [#%s]\n" % (indent, fc, call))
53+
_report_indent[0] += increment
54+
ret = fn(*params,**kwargs)
55+
_report_indent[0] -= increment
56+
sys.stderr.write("%s %s [#%s] ==> %s\n" % (indent, fc, call, repr(ret)))
57+
58+
return ret
59+
wrap.callcount = 0
60+
if debug:
61+
return decorator(wrap, fn)
62+
else:
63+
return fn
64+
65+
def mark():
66+
say("! mark")
67+
_mark = timeutils.clock()
68+
69+
def lap(s):
70+
tau = timeutils.clock() - _mark
71+
say("> %s: %.3fs since mark" % (s, tau))
72+
73+
def clock(s):
74+
lap(s)
75+
_mark = timeutils.clock()
76+
77+
def time(fn):
78+
"""Decorator to print timing information about a function call.
79+
"""
80+
def wrap(fn, *params,**kwargs):
81+
mark = timeutils.clock()
82+
83+
indent = ' ' * _report_indent[0]
84+
fc = "%s.%s()" % (fn.__module__, fn.__name__,)
85+
4686
ret = fn(*params,**kwargs)
47-
__report_indent[0] -= increment
48-
sys.stderr.write( "%s %s [#%s] ==> %s\n" % (indent, fc, call, repr(ret)))
87+
tau = timeutils.clock() - mark
88+
sys.stderr.write("%s| %s | %.3fs\n" % (indent, fc, tau))
4989

5090
return ret
5191
wrap.callcount = 0
5292
if debug:
53-
return wrap
93+
return decorator(wrap, fn)
5494
else:
5595
return fn
5696

5797
def show(name):
5898
if debug:
5999
frame = inspect.stack()[1][0]
60100
value = eval(name, frame.f_globals, frame.f_locals)
61-
indent = ' ' * (__report_indent[0])
101+
indent = ' ' * (_report_indent[0])
62102
sys.stderr.write("%s%s: %s\n" % (indent, name, value))
63103

64104
def log(name):
65105
if debug:
66106
frame = inspect.stack()[1][0]
67107
value = eval(name, frame.f_globals, frame.f_locals)
68-
indent = ' ' * (__report_indent[0])
108+
indent = ' ' * (_report_indent[0])
69109
syslog.syslog("%s%s: %s" % (indent, name, value))
70110

71111
def pprint(name):
72112
if debug:
73113
frame = inspect.stack()[1][0]
74114
value = eval(name, frame.f_globals, frame.f_locals)
75-
indent = ' ' * (__report_indent[0])
76-
sys.stdout.write("%s%s:\n" % (indent, name))
115+
indent = ' ' * (_report_indent[0])
116+
sys.stderr.write("%s%s:\n" % (indent, name))
77117
lines = pformat(value).split('\n')
78118
for line in lines:
79-
sys.stdout.write("%s %s\n"%(indent, line))
119+
sys.stderr.write("%s %s\n"%(indent, line))
80120

81121
def say(s):
82122
if debug:
83-
indent = ' ' * (__report_indent[0])
123+
indent = ' ' * (_report_indent[0])
84124
sys.stderr.write("%s%s\n" % (indent, s))
85125

126+
def profile(fn):
127+
def wrapper(*args, **kwargs):
128+
datafn = fn.__name__ + ".profile" # Name the data file sensibly
129+
prof = cProfile.Profile()
130+
retval = prof.runcall(fn, *args, **kwargs)
131+
prof.dump_stats(datafn)
132+
return retval
133+
return wrapper
134+

0 commit comments

Comments
 (0)