File size: 2,182 Bytes
825942f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from box import Timer   # Single Use
timer = Timer()
from box import Code    # Multi-Use
from box import handler # Single Use
from box import ic, ib     # Multi-Use, import time: 70ms - 110ms
from box import rel2abs
Code; ic; ib; rel2abs

import sys
import json
import ast
import inspect

def query_1():
    return """{
        "a": 1,
        "b": {
            "c": 2,
            "d": 3
        }
    }"""

def query_2():
    return "{}"

def main():
    # code_line = 'm2 = D2[get()][name]["c"]'
    # variable_name = find_variable_accessed_by_key(code_line, 'b')
    # ic(variable_name)
    # return
    D1 = json.loads(query_1())
    resp2 = query_2()
    D2 = json.loads(resp2)
    name = "b"
    m1 = D1[name]["c"]
    m2 = D2[name]["c"]
    current_frame = sys._getframe()
    current_function = current_frame.f_code.co_name
    # function to source text, import if reqd
    source = inspect.getsource(current_frame.f_code)
    print(source)

def find_variable_accessed_by_key(code_line, target_key):
    import astor
    tree = ast.parse(code_line)
    for node in ast.walk(tree):
        if isinstance(node, ast.Subscript):
            subscript_node = node.slice
            ic(astor.to_source(subscript_node))
    return None

def investigate(e):
    from box.introspection import Frame
    import traceback
    tb = e.__traceback__
    frames = [ frame for frame, _ in traceback.walk_tb(tb) ]
    frame = frames[1]

    function_line_start = frame.f_code.co_firstlineno
    function_line_error = frame.f_lineno
    source = inspect.getsource(frame.f_code)
    # print source, but highlight the error line
    lines = source.split("\n")
    print(e)
    for i, line in enumerate(lines):
        if i == function_line_error - function_line_start:
            print(">>>", line)
            # we know it’s asn assignment error. we know the key. find the assignment dict that the key is referencing from the line
            # use ast.parse or anythign else if reqd
            



        else:
            print("   ", line)


if __name__ == "__main__":
    # with handler():
    #     main()
    try:
        main()
    except Exception as e:
        investigate(e)