Skip to content

Instantly share code, notes, and snippets.

@chrisglass
Created December 12, 2020 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisglass/1824eb8103e9b2cbc778eef400df6dba to your computer and use it in GitHub Desktop.
Save chrisglass/1824eb8103e9b2cbc778eef400df6dba to your computer and use it in GitHub Desktop.
def dbg(result):
"""
Recover the expression giving the result, and then
print a helpful debug statement showing this
"""
import inspect
frame = inspect.stack()[1]
expr = extract_dbg(frame.code_context[0])
filename = frame.filename.split("/")[-1]
print(f"[{filename}: {frame.lineno}] {expr} = {result}")
return result
def extract_dbg(code_fragment):
# from a line of source text, try and find the expression
# given to a call to dbg
import ast
expression_options = code_fragment.split("dbg(")
if len(expression_options) != 2:
# if there are either multiple dbg statements
# or I can't find the dbg line, bail
return "???"
# get the part to the right of dbg(
expr_candidate = expression_options[1]
while expr_candidate:
try:
ast.parse(expr_candidate)
return expr_candidate
except SyntaxError:
expr_candidate = expr_candidate[:-1]
# didn't find anything, also give up
return "???"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment