def exc_dict():
"""Expects to be called from within an exception handling block as in:
try:
foo()
except Exception:
print exc_dict()
Creates a nice array of dictionaries each of which describes
a part of the traceback:
[
{'file': '/usr/local/src/project/test.py',
'function': 'run_test',
'line': '99'},
{'file': '/usr/local/src/project/module.py',
'function': 'foo',
'line': '199'}
]
"""
exception = traceback.format_exc().splitlines()
es = []
for line in exception:
if line != exception[0] and line != exception[-1]:
line = line.split(', ')
line = [l.strip().replace('"', '').split(' ', 1) for l in line]
dd = {'file': '', 'line': '', 'function': ''}
for p in line:
if p[0] == 'File':
dd['file'] = os.path.abspath(p[1])
elif p[0] == 'line':
dd['line'] = p[1]
elif p[0] == 'in':
dd['function'] = p[1]
if dd['file'] or dd['line'] or dd['function']:
es.append(dd)
return es
Created
February 5, 2014 16:06
-
-
Save stavxyz/8826972 to your computer and use it in GitHub Desktop.
create useful little dicts related to an exception
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment