File size: 932 Bytes
58627fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import traceback
import inspect


class Provenance:
    def __init__(self) -> None:
        self.initial_stacktrace = self.stacktrace()

    def stacktrace(self):
        trace = inspect.stack()
        output = []

        for frame in trace[2:-1]:
            try:
                frame = f'{frame.filename}:{frame.lineno}:{frame.function}:   {frame.code_context[0].strip()}'
                output.append(frame)
            except:
                output.append(None)

        return output

    def toDict(self):  # for ujson
        self.serialization_stacktrace = self.stacktrace()
        return dict(self.__dict__)


if __name__ == '__main__':
    p = Provenance()
    print(p.toDict().keys())

    import ujson
    print(ujson.dumps(p, indent=4))


    class X:
        def __init__(self) -> None:
            pass
        
        def toDict(self):
            return {'key': 1}
    
    print(ujson.dumps(X()))