Added a debug utility module on the same level as south,html5lib etc.

- Legacy-Id: 4446
This commit is contained in:
Henrik Levkowetz 2012-06-11 15:56:51 +00:00
parent a4220324e0
commit 51a3c373c5
2 changed files with 87 additions and 0 deletions

85
debug.py Normal file
View file

@ -0,0 +1,85 @@
import sys
import inspect
import syslog
from pprint import pformat
# A debug decorator, written by Paul Butler, taken from
# http://paulbutler.org/archives/python-debugging-with-decorators/
# Additional functions and decorator functionality added by
# Henrik Levkowetz
# Number of times to indent output
# A list is used to force access by reference
__report_indent = [4]
increment = 2
debug = True
syslog.openlog("debug", syslog.LOG_PID, syslog.LOG_USER)
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
sys.stderr.write( "%s %s [#%s] ==> %s\n" % (indent, fc, call, repr(ret)))
return ret
wrap.callcount = 0
if debug:
return wrap
else:
return fn
def show(name):
if debug:
frame = inspect.stack()[1][0]
value = eval(name, frame.f_globals, frame.f_locals)
indent = ' ' * (__report_indent[0])
sys.stderr.write("%s%s: %s\n" % (indent, name, value))
def log(name):
if debug:
frame = inspect.stack()[1][0]
value = eval(name, frame.f_globals, frame.f_locals)
indent = ' ' * (__report_indent[0])
syslog.syslog("%s%s: %s" % (indent, name, value))
def pprint(name):
if 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 debug:
indent = ' ' * (__report_indent[0])
sys.stderr.write("%s%s\n" % (indent, s))

View file

@ -29,6 +29,8 @@ class PersonInfo(models.Model):
prefix, first, middle, last, suffix = self.ascii_parts()
return (first and first[0]+"." or "")+(middle or "")+" "+last+(suffix and " "+suffix or "")
def plain_name(self):
if self.ascii_short:
return self.ascii_short
prefix, first, middle, last, suffix = name_parts(self.name)
return u" ".join([first, last])
def last_name(self):