Created
December 12, 2020 09:16
-
-
Save chrisglass/1824eb8103e9b2cbc778eef400df6dba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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