Source code for openmdao.util.debug

Routines to help obtain debugging information.

import os
import sys
import re
import linecache
import StringIO

from openmdao.util.log import logger, LOG_DEBUG

[docs]def strict_chk_config(strict): """check_config methods should call this to determine if they should run in strict mode or not. """ return strict or os.environ.get('OPENMDAO_STRICT_CONFIG')
[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)
debug = os.environ.get('OPENMDAO_DEBUG', '').strip() if debug and debug.lower() not in ['0', 'false']: logger.setLevel(LOG_DEBUG) DEBUG = logger.debug debug = True else: debug = False
[docs] def DEBUG(msg): pass
OpenMDAO Home