globals_dict
GlobalsDict
Bases: Dict[str, object]
A custom dict that is meant to be accessed in a particular way, in order to record getitems. It is used for setting as the globals when executing some code, so we can try to understand which globals were accessed.
It is meant to be used like:
- Instantiate it empty like
GlobalsDict()
- Call
setup_globals(d)
to update it with the input globals - Execute some code that uses it as globals, which will call
__setitem__
as well as our custom__getitem__
. - Call
teardown_globals()
which will return theResult
, containing the a record of all the original globals that were accessed and any new globals that were updated or added.
We cannot overload the __setitem__
method, since Python will not respect
it for custom globals, but we can overload the getitem method.
See https://stackoverflow.com/a/12185315/907060 which refers to https://bugs.python.org/issue14385
Source code in lineapy/execution/globals_dict.py
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 |
|
State
dataclass
Source code in lineapy/execution/globals_dict.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
process_getitem(k, v)
If we haven't recorded this key and its value is the same as the value in the input globals (meaning we haven't overwritten it), then record it as a getitem.
Source code in lineapy/execution/globals_dict.py
81 82 83 84 85 86 87 88 89 90 91 92 93 |
|