/
science_tracker.py
executable file
·89 lines (65 loc) · 2.92 KB
/
science_tracker.py
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
87
88
89
import inspect
bibliography: list = []
bibliography_id: list = []
science_tracking: list = []
track_science_references: bool = False
track_each_reference_call: bool = False
def hexID(obj: str) -> str:
return "{}".format(id(obj))
def science_reference(short_purpose: str, reference: str) -> None:
"""Marker acting as a reference for the origin of specific information
Acts as a marker in code where particular algorithms/data/... originates. General execution of code silently passes
these markers, but remembers how and where they were called. Which markers were passed in a particular program run
can be recalled with print_references().
Arguments:
short_purpose: Identify the thing being referenced (string)
reference: The reference itself, in any sensible format.
"""
global bibliography, bibliography_id, science_tracking, track_science_references
if not track_science_references:
return
stack = inspect.stack()
frame, path, line, function, context, index = stack[1]
del stack
arguments = inspect.formatargvalues(*inspect.getargvalues(frame))
availableObjects = {}
availableObjects.update(frame.f_globals)
availableObjects.update(frame.f_locals)
del frame
func_id = hexID(availableObjects[function])
identifier = "{} [{}]".format(function, func_id)
call_record = "{}{}".format(function, arguments)
add_anyway = False
if identifier not in bibliography_id:
bibliography_id.append(identifier)
bibliography.append(reference)
add_anyway = True
if track_each_reference_call or add_anyway:
science_tracking.append((call_record, short_purpose, bibliography_id.index(identifier) + 1))
def print_references() -> None:
"""recall the science_reference markers passed, print out the references."""
global bibliography, bibliography_id, science_tracking, track_each_reference_call
"List of references encountered while executing"
if not track_each_reference_call: print(
"showing first call only - override with solcore.track_science(track_each_call=True)")
for record, purpose, index in science_tracking:
print("[{}] {} - {}".format(index, purpose, record))
print()
for i, b in enumerate(bibliography):
print("[{}] - {}".format(i + 1, b))
def track_science(track_each_call: bool = False):
"""configure science references -- determine whether or not each call separately or only the first for each
reference."""
global track_science_references, track_each_reference_call
track_science_references = True
track_each_reference_call = track_each_call
if __name__ == "__main__":
def myRandomFunctionA() -> None:
myRandomFunctionB(d=1)
def myRandomFunctionB(d) -> None:
a = 1
science_reference("doing something smart", "My Awesome Book, by me.")
track_science()
myRandomFunctionA()
myRandomFunctionA()
print_references()