Source code for openmdao.util.debug

Routines to help out with obtaining debugging information

import os
import sys
import re
import linecache

[docs]def traceit(frame, event, arg): """A function useful for tracing Python execution. Wherever you want the tracing to start, insert a call to sys.settrace(traceit).""" if event == "line": lineno = frame.f_lineno filename = frame.f_globals["__file__"] if (filename.endswith(".pyc") or filename.endswith(".pyo")): filename = filename[:-1] name = frame.f_globals["__name__"] line = linecache.getline(filename, lineno) print "%s:%s: %s" % (name, lineno, line.rstrip()) return traceit
address_rgx = re.compile(' at 0x\w+')
[docs]def dumpit(obj, stream=sys.stdout, recurse=True, ignore_address=True): """Try to dump out the guts of an object, and optionally its children.""" def _dumpit(obj, stream, recurse, indent, visited, ignore_address): if id(obj) not in visited: visited.add(id(obj)) try: dct = obj.__dict__ except AttributeError: return else: space = ' '*indent for key,value in sorted(dct.items(), cmp=lambda x,y: cmp(x[0],y[0])): if isinstance(value, dict): value = '{ ' + '\n'.join(['%s%s: %s' % (' '*(indent+3),k,v) for k,v in sorted(value.items(), cmp=lambda x,y: cmp(x[0],y[0]))]) + ' }' if ignore_address: val = address_rgx.sub('', repr(value)) else: val = value stream.writelines(['%s%s: %s' % (space,key,val), '\n']) if recurse: _dumpit(value, stream, recurse, indent+3, visited, ignore_address) _dumpit(obj, stream, recurse, 0, set(), ignore_address)
OpenMDAO Home