invocation-tree 0.0.8__tar.gz → 0.0.10__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- invocation_tree-0.0.10/PKG-INFO +558 -0
- invocation_tree-0.0.10/README.md +539 -0
- invocation_tree-0.0.10/images/#generator_function.py# +13 -0
- invocation_tree-0.0.10/images/compute.gif +0 -0
- invocation_tree-0.0.10/images/compute.py +22 -0
- invocation_tree-0.0.10/images/compute.py~ +22 -0
- invocation_tree-0.0.10/images/compute1.png +0 -0
- invocation_tree-0.0.10/images/compute2.png +0 -0
- invocation_tree-0.0.10/images/compute3.png +0 -0
- invocation_tree-0.0.10/images/compute4.png +0 -0
- invocation_tree-0.0.10/images/compute5.png +0 -0
- invocation_tree-0.0.10/images/compute6.png +0 -0
- invocation_tree-0.0.10/images/compute7.png +0 -0
- invocation_tree-0.0.10/images/compute8.png +0 -0
- invocation_tree-0.0.10/images/compute9.png +0 -0
- invocation_tree-0.0.10/images/create_images.sh +41 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial1.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial2.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial3.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial4.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial5.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial6.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial7.png +0 -0
- invocation_tree-0.0.10/images/generator_expression.gif +0 -0
- invocation_tree-0.0.10/images/generator_expression.py +11 -0
- invocation_tree-0.0.10/images/generator_expression.py~ +9 -0
- invocation_tree-0.0.10/images/generator_expression1.png +0 -0
- invocation_tree-0.0.10/images/generator_expression2.png +0 -0
- invocation_tree-0.0.10/images/generator_expression3.png +0 -0
- invocation_tree-0.0.10/images/generator_expression4.png +0 -0
- invocation_tree-0.0.10/images/generator_expression5.png +0 -0
- invocation_tree-0.0.10/images/generator_expression6.png +0 -0
- invocation_tree-0.0.10/images/generator_expression7.png +0 -0
- invocation_tree-0.0.10/images/generator_expression8.png +0 -0
- invocation_tree-0.0.10/images/generator_expression9.png +0 -0
- invocation_tree-0.0.10/images/generator_function.gif +0 -0
- invocation_tree-0.0.10/images/generator_function.py +12 -0
- invocation_tree-0.0.10/images/generator_function1.png +0 -0
- invocation_tree-0.0.10/images/generator_function2.png +0 -0
- invocation_tree-0.0.10/images/generator_function3.png +0 -0
- invocation_tree-0.0.10/images/generator_function4.png +0 -0
- invocation_tree-0.0.10/images/generator_function5.png +0 -0
- invocation_tree-0.0.10/images/generator_function6.png +0 -0
- invocation_tree-0.0.10/images/generator_function7.png +0 -0
- invocation_tree-0.0.10/images/generator_function8.png +0 -0
- invocation_tree-0.0.10/images/generator_function9.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline.gif +0 -0
- invocation_tree-0.0.10/images/generator_pipeline.py +28 -0
- invocation_tree-0.0.10/images/generator_pipeline.py~ +26 -0
- invocation_tree-0.0.10/images/generator_pipeline1.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline10.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline11.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline12.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline13.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline14.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline15.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline16.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline17.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline18.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline19.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline2.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline20.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline21.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline22.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline23.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline24.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline25.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline26.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline27.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline3.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline4.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline5.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline6.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline7.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline8.png +0 -0
- invocation_tree-0.0.10/images/generator_pipeline9.png +0 -0
- invocation_tree-0.0.10/images/iterable.py +44 -0
- invocation_tree-0.0.10/images/iterable.py~ +31 -0
- invocation_tree-0.0.10/images/my_list.py +20 -0
- invocation_tree-0.0.10/images/my_list.py~ +4 -0
- invocation_tree-0.0.10/images/my_range.gif +0 -0
- invocation_tree-0.0.10/images/my_range.py +41 -0
- invocation_tree-0.0.10/images/my_range.py~ +14 -0
- invocation_tree-0.0.10/images/my_range1.png +0 -0
- invocation_tree-0.0.10/images/my_range10.png +0 -0
- invocation_tree-0.0.10/images/my_range11.png +0 -0
- invocation_tree-0.0.10/images/my_range12.png +0 -0
- invocation_tree-0.0.10/images/my_range13.png +0 -0
- invocation_tree-0.0.10/images/my_range14.png +0 -0
- invocation_tree-0.0.10/images/my_range15.png +0 -0
- invocation_tree-0.0.10/images/my_range2.png +0 -0
- invocation_tree-0.0.10/images/my_range3.png +0 -0
- invocation_tree-0.0.10/images/my_range4.png +0 -0
- invocation_tree-0.0.10/images/my_range5.png +0 -0
- invocation_tree-0.0.10/images/my_range6.png +0 -0
- invocation_tree-0.0.10/images/my_range7.png +0 -0
- invocation_tree-0.0.10/images/my_range8.png +0 -0
- invocation_tree-0.0.10/images/my_range9.png +0 -0
- invocation_tree-0.0.10/images/out.txt +8 -0
- invocation_tree-0.0.10/images/permutations0.png +0 -0
- invocation_tree-0.0.10/images/permutations1.png +0 -0
- invocation_tree-0.0.10/images/permutations10.png +0 -0
- invocation_tree-0.0.10/images/permutations11.png +0 -0
- invocation_tree-0.0.10/images/permutations12.png +0 -0
- invocation_tree-0.0.10/images/permutations13.png +0 -0
- invocation_tree-0.0.10/images/permutations2.png +0 -0
- invocation_tree-0.0.10/images/permutations3.png +0 -0
- invocation_tree-0.0.10/images/permutations4.png +0 -0
- invocation_tree-0.0.10/images/permutations5.png +0 -0
- invocation_tree-0.0.10/images/permutations6.png +0 -0
- invocation_tree-0.0.10/images/permutations7.png +0 -0
- invocation_tree-0.0.10/images/permutations8.png +0 -0
- invocation_tree-0.0.10/images/permutations9.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students1.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students10.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students11.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students12.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students13.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students14.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students15.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students2.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students3.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students4.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students5.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students6.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students7.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students8.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students9.png +0 -0
- invocation_tree-0.0.10/images/tree.gv~ +22 -0
- invocation_tree-0.0.10/images/tree.pdf +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/invocation_tree/__init__.py +12 -6
- invocation_tree-0.0.10/invocation_tree.egg-info/PKG-INFO +558 -0
- invocation_tree-0.0.10/invocation_tree.egg-info/SOURCES.txt +156 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/setup.py +1 -1
- invocation_tree-0.0.8/PKG-INFO +0 -188
- invocation_tree-0.0.8/README.md +0 -169
- invocation_tree-0.0.8/images/create_images.sh +0 -16
- invocation_tree-0.0.8/images/factorial8.gv +0 -36
- invocation_tree-0.0.8/images/factorial9.gv +0 -37
- invocation_tree-0.0.8/images/permutations1.png +0 -0
- invocation_tree-0.0.8/images/permutations10.png +0 -0
- invocation_tree-0.0.8/images/permutations11.png +0 -0
- invocation_tree-0.0.8/images/permutations12.png +0 -0
- invocation_tree-0.0.8/images/permutations13.png +0 -0
- invocation_tree-0.0.8/images/permutations2.png +0 -0
- invocation_tree-0.0.8/images/permutations3.png +0 -0
- invocation_tree-0.0.8/images/permutations4.png +0 -0
- invocation_tree-0.0.8/images/permutations5.png +0 -0
- invocation_tree-0.0.8/images/permutations6.png +0 -0
- invocation_tree-0.0.8/images/permutations7.png +0 -0
- invocation_tree-0.0.8/images/permutations8.png +0 -0
- invocation_tree-0.0.8/images/permutations9.png +0 -0
- invocation_tree-0.0.8/images/students16.gv +0 -44
- invocation_tree-0.0.8/images/students17.gv +0 -50
- invocation_tree-0.0.8/images/students18.gv +0 -51
- invocation_tree-0.0.8/images/students19.gv +0 -58
- invocation_tree-0.0.8/images/students20.gv +0 -59
- invocation_tree-0.0.8/images/students21.gv +0 -60
- invocation_tree-0.0.8/images/students22.gv +0 -61
- invocation_tree-0.0.8/images/students23.gv +0 -60
- invocation_tree-0.0.8/images/students24.gv +0 -66
- invocation_tree-0.0.8/images/students25.gv +0 -68
- invocation_tree-0.0.8/images/students26.gv +0 -68
- invocation_tree-0.0.8/images/students27.gv +0 -68
- invocation_tree-0.0.8/images/students28.gv +0 -67
- invocation_tree-0.0.8/images/students29.gv +0 -68
- invocation_tree-0.0.8/images/students30.gv +0 -69
- invocation_tree-0.0.8/images/tree0.gv +0 -7
- invocation_tree-0.0.8/images/tree1.gv +0 -13
- invocation_tree-0.0.8/images/tree10.gv +0 -56
- invocation_tree-0.0.8/images/tree11.gv +0 -64
- invocation_tree-0.0.8/images/tree12.gv +0 -66
- invocation_tree-0.0.8/images/tree13.gv +0 -67
- invocation_tree-0.0.8/images/tree14.gv +0 -72
- invocation_tree-0.0.8/images/tree15.gv +0 -73
- invocation_tree-0.0.8/images/tree16.gv +0 -75
- invocation_tree-0.0.8/images/tree17.gv +0 -75
- invocation_tree-0.0.8/images/tree2.gv +0 -22
- invocation_tree-0.0.8/images/tree3.gv +0 -30
- invocation_tree-0.0.8/images/tree4.gv +0 -32
- invocation_tree-0.0.8/images/tree5.gv +0 -33
- invocation_tree-0.0.8/images/tree6.gv +0 -39
- invocation_tree-0.0.8/images/tree7.gv +0 -47
- invocation_tree-0.0.8/images/tree8.gv +0 -49
- invocation_tree-0.0.8/images/tree9.gv +0 -50
- invocation_tree-0.0.8/invocation_tree.egg-info/PKG-INFO +0 -188
- invocation_tree-0.0.8/invocation_tree.egg-info/SOURCES.txt +0 -99
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/LICENSE.txt +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/MANIFEST.in +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/create_gif.sh +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/create_images.sh~ +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial.gif +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial.py +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/factorial.py~ +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/invocation_tree.pdf +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/invocation_tree0.pdf +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/permutations.gif +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/permutations.py +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/permutations.py~ +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/permutations22.png~ +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/student.gif +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students.gif +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students.py +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/students.py~ +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/test.py +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/images/vscode.png +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/install.txt +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/invocation_tree.egg-info/dependency_links.txt +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/invocation_tree.egg-info/requires.txt +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/invocation_tree.egg-info/top_level.txt +0 -0
- {invocation_tree-0.0.8 → invocation_tree-0.0.10}/setup.cfg +0 -0
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: invocation_tree
|
|
3
|
+
Version: 0.0.10
|
|
4
|
+
Summary: Generate an invocation tree of functions calls.
|
|
5
|
+
Home-page: https://github.com/bterwijn/invocation_tree
|
|
6
|
+
Author: Bas Terwijn
|
|
7
|
+
Author-email: bterwijn@gmail.com
|
|
8
|
+
License: BSD 2-clause
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Education
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Topic :: Education
|
|
15
|
+
Classifier: Topic :: Software Development :: Debuggers
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE.txt
|
|
18
|
+
Requires-Dist: graphviz
|
|
19
|
+
|
|
20
|
+
# Installation #
|
|
21
|
+
Install (or upgrade) `invocation_tree` using pip:
|
|
22
|
+
```
|
|
23
|
+
pip install --upgrade invocation_tree
|
|
24
|
+
```
|
|
25
|
+
Additionally [Graphviz](https://graphviz.org/download/) needs to be installed.
|
|
26
|
+
|
|
27
|
+
# Invocation Tree #
|
|
28
|
+
The [invocation_tree](https://pypi.org/project/invocation-tree/) package is designed to help with **program understanding and debugging** by visualizing the **tree of function invocations** that occur during program execution. Here’s a simple example of how it works, we start with `a = 1` and compute:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
(a - 3 + 9) * 6
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import invocation_tree as invo_tree
|
|
36
|
+
|
|
37
|
+
def main():
|
|
38
|
+
a = 1
|
|
39
|
+
a = expression(a)
|
|
40
|
+
return multiply(a, 6)
|
|
41
|
+
|
|
42
|
+
def expression(a):
|
|
43
|
+
a = subtract(a, 3)
|
|
44
|
+
return add(a, 9)
|
|
45
|
+
|
|
46
|
+
def subtract(a, b):
|
|
47
|
+
return a - b
|
|
48
|
+
|
|
49
|
+
def add(a, b):
|
|
50
|
+
return a + b
|
|
51
|
+
|
|
52
|
+
def multiply(a, b):
|
|
53
|
+
return a * b
|
|
54
|
+
|
|
55
|
+
tree = invo_tree.blocking()
|
|
56
|
+
print( tree(main) )
|
|
57
|
+
```
|
|
58
|
+
Running the program and pressing <Enter> a number of times results in:
|
|
59
|
+

|
|
60
|
+
```
|
|
61
|
+
42
|
|
62
|
+
```
|
|
63
|
+
Each node in the tree represents a function call, and the node's color indicates its state:
|
|
64
|
+
|
|
65
|
+
- White: The function is currently being executed (it is at the top of the call stack).
|
|
66
|
+
- Green: The function is paused and will resume execution later (it is lower down on the call stack).
|
|
67
|
+
- Red: The function has completed execution and returned (it has been removed from the call stack).
|
|
68
|
+
|
|
69
|
+
For every function, the package displays its **local variables** and **return value**. Changes to these values over time are highlighted using bold text and gray shading to make them easy to track.
|
|
70
|
+
|
|
71
|
+
# Chapters #
|
|
72
|
+
|
|
73
|
+
[Comprehensions](#Comprehensions)
|
|
74
|
+
|
|
75
|
+
[Debugger](#Debugger)
|
|
76
|
+
|
|
77
|
+
[Recursion](#Recursion)
|
|
78
|
+
|
|
79
|
+
[Lazy Evalution](#Lazy-Evalution)
|
|
80
|
+
|
|
81
|
+
[Configuration](#Configuration)
|
|
82
|
+
|
|
83
|
+
[Troubleshooting](#Troubleshooting)
|
|
84
|
+
|
|
85
|
+
# Author #
|
|
86
|
+
Bas Terwijn
|
|
87
|
+
|
|
88
|
+
# Inspiration #
|
|
89
|
+
Inspired by [rcviz](https://github.com/carlsborg/rcviz).
|
|
90
|
+
|
|
91
|
+
# Supported by #
|
|
92
|
+
<img src="https://raw.githubusercontent.com/bterwijn/memory_graph/main/images/uva.png" alt="University of Amsterdam" width="600">
|
|
93
|
+
|
|
94
|
+
___
|
|
95
|
+
___
|
|
96
|
+
|
|
97
|
+
# Comprehensions #
|
|
98
|
+
In this more interesting example we compute which students pass a course by using list and dictionary comprehensions.
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
import invocation_tree as invo_tree
|
|
102
|
+
from decimal import Decimal, ROUND_HALF_UP
|
|
103
|
+
|
|
104
|
+
def main():
|
|
105
|
+
students = {'Ann':[7.5, 8.0],
|
|
106
|
+
'Bob':[4.5, 6.0],
|
|
107
|
+
'Coy':[7.5, 6.0]}
|
|
108
|
+
averages = {student:compute_average(grades)
|
|
109
|
+
for student, grades in students.items()}
|
|
110
|
+
passing = passing_students(averages)
|
|
111
|
+
print(passing)
|
|
112
|
+
|
|
113
|
+
def compute_average(grades):
|
|
114
|
+
average = sum(grades)/len(grades)
|
|
115
|
+
return half_up_round(average, 1)
|
|
116
|
+
|
|
117
|
+
def half_up_round(value, digits=0):
|
|
118
|
+
""" High-precision half-up rounding of 'value' to a specified number of 'digits'. """
|
|
119
|
+
return float(Decimal(str(value)).quantize(Decimal(f"1e-{digits}"),
|
|
120
|
+
rounding=ROUND_HALF_UP))
|
|
121
|
+
|
|
122
|
+
def passing_students(averages):
|
|
123
|
+
return [student
|
|
124
|
+
for student, average in averages.items()
|
|
125
|
+
if average >= 5.5]
|
|
126
|
+
|
|
127
|
+
if __name__ == '__main__':
|
|
128
|
+
tree = invo_tree.blocking()
|
|
129
|
+
tree(main) # show invocation tree starting at main
|
|
130
|
+
```
|
|
131
|
+

|
|
132
|
+
```
|
|
133
|
+
['Ann', 'Coy']
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Blocking ##
|
|
137
|
+
The program blocks execution at every function call and return statement, printing the current location in the source code. Press the <Enter> key to continue execution. To block at every line of the program (like in a debugger tool) and only where a change of value occured, use instead:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
tree = invo_tree.blocking_each_change()
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
# Debugger #
|
|
144
|
+
To visualize the invocation tree in a debugger tool, such as the integrated debugger in Visual Studio Code, use instead:
|
|
145
|
+
|
|
146
|
+
```python
|
|
147
|
+
tree = invo_tree.debugger()
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
and open the 'tree.pdf' file manually.
|
|
151
|
+

|
|
152
|
+
|
|
153
|
+
# Recursion #
|
|
154
|
+
An invocation tree is particularly helpful to better understand recursion. A simple `factorial()` example:
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
import invocation_tree as invo_tree
|
|
158
|
+
|
|
159
|
+
def factorial(n):
|
|
160
|
+
if n <= 1:
|
|
161
|
+
return 1
|
|
162
|
+
return n * factorial(n - 1)
|
|
163
|
+
|
|
164
|
+
tree = invo_tree.blocking()
|
|
165
|
+
print( tree(factorial, 4) ) # show invocation tree of calling factorial(4)
|
|
166
|
+
```
|
|
167
|
+

|
|
168
|
+
```
|
|
169
|
+
24
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
This `permutations()` example shows the depth-first nature of recursive execution:
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
import invocation_tree as invo_tree
|
|
176
|
+
|
|
177
|
+
def permutations(elements, perm, n):
|
|
178
|
+
if n==0:
|
|
179
|
+
return [perm]
|
|
180
|
+
all_perms = []
|
|
181
|
+
for element in elements:
|
|
182
|
+
all_perms.extend(permutations(elements, perm + element, n-1))
|
|
183
|
+
return all_perms
|
|
184
|
+
|
|
185
|
+
tree = invo_tree.blocking()
|
|
186
|
+
result = tree(permutations, ['L','R'], '', 2)
|
|
187
|
+
print(result) # all permutations of going Left or Right of length 2
|
|
188
|
+
```
|
|
189
|
+

|
|
190
|
+
```
|
|
191
|
+
['LL', 'LR', 'RL', 'RR']
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
## Hide Variables ##
|
|
195
|
+
In an educational context it can be useful to hide certian variables to avoid unnecessary complexity. This can for example be done with:
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
tree = invo_tree.blocking()
|
|
199
|
+
tree.hide.add('permutations.elements')
|
|
200
|
+
tree.hide.add('permutations.element')
|
|
201
|
+
tree.hide.add('permutations.all_perms')
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
# Lazy Evalution
|
|
205
|
+
An invocation tree is helpful to understand how a pipeline of generators is lazily evaluated. But to understand generators and lazy evaluation we first have to understand the Iterator Protocol.
|
|
206
|
+
|
|
207
|
+
## Iterator Protocol ##
|
|
208
|
+
The [Iterator Protocol](https://docs.python.org/3/library/stdtypes.html#iterator-types) is implemented by many different types:
|
|
209
|
+
|
|
210
|
+
`range`, `list`, `set`, `dict`, ...
|
|
211
|
+
|
|
212
|
+
which make these type iterable, meaning we can iterate over values of these types to get a sequence of values. It works by:
|
|
213
|
+
|
|
214
|
+
- first calling iter(iterable) to get an iterator
|
|
215
|
+
- then repeatedly calling next(iterator) to get each value
|
|
216
|
+
- the sequence ends when a StopIteration exceptions is raised
|
|
217
|
+
|
|
218
|
+
An example of iterable `range` and `list` in the Python interpreter looks like:
|
|
219
|
+
|
|
220
|
+
<TABLE><TR><TD>
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
$ python
|
|
224
|
+
>>> iterator = iter(range(1,4))
|
|
225
|
+
>>> next(iterator)
|
|
226
|
+
1
|
|
227
|
+
>>> next(iterator)
|
|
228
|
+
2
|
|
229
|
+
>>> next(iterator)
|
|
230
|
+
3
|
|
231
|
+
>>> next(iterator)
|
|
232
|
+
Traceback (most recent call last):
|
|
233
|
+
File "<stdin>", line 1, in <module>
|
|
234
|
+
StopIteration
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
</TD><TD>
|
|
238
|
+
|
|
239
|
+
```
|
|
240
|
+
$ python
|
|
241
|
+
>>> iterator = iter([1,2,3])
|
|
242
|
+
>>> next(iterator)
|
|
243
|
+
1
|
|
244
|
+
>>> next(iterator)
|
|
245
|
+
2
|
|
246
|
+
>>> next(iterator)
|
|
247
|
+
3
|
|
248
|
+
>>> next(iterator)
|
|
249
|
+
Traceback (most recent call last):
|
|
250
|
+
File "<stdin>", line 1, in <module>
|
|
251
|
+
StopIteration
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
</TD></TR></TABLE>
|
|
255
|
+
|
|
256
|
+
It is the Iterator Protocol that allows a for-loop to read a sequence of values from an iterable:
|
|
257
|
+
|
|
258
|
+
```python
|
|
259
|
+
iterable = range(1,4)
|
|
260
|
+
for value in iterable:
|
|
261
|
+
print(value)
|
|
262
|
+
```
|
|
263
|
+
```
|
|
264
|
+
1
|
|
265
|
+
2
|
|
266
|
+
3
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
and the same holds for many functions like `list()`, `sum()`, `max()`, `min()`, ...
|
|
270
|
+
|
|
271
|
+
<TABLE><TR><TD>
|
|
272
|
+
|
|
273
|
+
```python
|
|
274
|
+
iterable = range(1,4)
|
|
275
|
+
print('list:', list(iterable))
|
|
276
|
+
```
|
|
277
|
+
```
|
|
278
|
+
list: [1, 2, 3]
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
</TD><TD>
|
|
282
|
+
|
|
283
|
+
```python
|
|
284
|
+
iterable = range(1,4)
|
|
285
|
+
print('sum:', sum(iterable))
|
|
286
|
+
```
|
|
287
|
+
```
|
|
288
|
+
sum: 6
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
</TD></TR></TABLE>
|
|
292
|
+
|
|
293
|
+
We can define our own `My_Range` and `My_Iterator` class to see the Iterator Protocol in action.
|
|
294
|
+
```python
|
|
295
|
+
import invocation_tree as invo_tree
|
|
296
|
+
|
|
297
|
+
class My_Iterator:
|
|
298
|
+
|
|
299
|
+
def __init__(self, my_range):
|
|
300
|
+
self.my_range = my_range
|
|
301
|
+
self.value = self.my_range.start
|
|
302
|
+
|
|
303
|
+
def __repr__(self):
|
|
304
|
+
return f'My_Iterator value:{self.value}'
|
|
305
|
+
|
|
306
|
+
def __next__(self):
|
|
307
|
+
print('My_Iterator.__next__')
|
|
308
|
+
prev = self.value
|
|
309
|
+
self.value += self.my_range.step
|
|
310
|
+
if prev < self.my_range.stop:
|
|
311
|
+
return prev
|
|
312
|
+
raise StopIteration
|
|
313
|
+
|
|
314
|
+
class My_Range:
|
|
315
|
+
|
|
316
|
+
def __init__(self, start, stop, step=1):
|
|
317
|
+
self.start = start
|
|
318
|
+
self.stop = stop
|
|
319
|
+
self.step = step
|
|
320
|
+
|
|
321
|
+
def __repr__(self):
|
|
322
|
+
return f'My_Range start:{self.start} stop:{self.stop} step:{self.step}'
|
|
323
|
+
|
|
324
|
+
def __iter__(self):
|
|
325
|
+
print('My_Range.__iter__')
|
|
326
|
+
return My_Iterator(self)
|
|
327
|
+
|
|
328
|
+
def main():
|
|
329
|
+
my_range = My_Range(1, 4)
|
|
330
|
+
for i in my_range:
|
|
331
|
+
print(i)
|
|
332
|
+
|
|
333
|
+
tree = invo_tree.blocking()
|
|
334
|
+
tree(main)
|
|
335
|
+
```
|
|
336
|
+

|
|
337
|
+
|
|
338
|
+
```
|
|
339
|
+
My_Range.__iter__
|
|
340
|
+
My_Iterator.__next__
|
|
341
|
+
1
|
|
342
|
+
My_Iterator.__next__
|
|
343
|
+
2
|
|
344
|
+
My_Iterator.__next__
|
|
345
|
+
3
|
|
346
|
+
My_Iterator.__next__
|
|
347
|
+
```
|
|
348
|
+
As you can see a lot happens in `main()` to complete the for-loop:
|
|
349
|
+
- A 'my_range' object is created using its `My_Range.__init__` method.
|
|
350
|
+
- The for-loop requests an iterator using 'iter(my_range)' resulting in a `My_Range.__iter__` method call.
|
|
351
|
+
- The for-loop keeps calling 'next(iterator)' to get the sequence of values resulting in `My_Iterator.__next__` calls.
|
|
352
|
+
- At the 4th call the sequence is ended with a `StopIteration` exception.
|
|
353
|
+
|
|
354
|
+
## Generator Functions ##
|
|
355
|
+
By using `yield` instead of `return` in a function, we can create a [generator](https://docs.python.org/3/reference/expressions.html#yield-expressions) that produces a sequence of values as an iterable.
|
|
356
|
+
|
|
357
|
+
```python
|
|
358
|
+
def my_generator():
|
|
359
|
+
yield 1
|
|
360
|
+
yield 2
|
|
361
|
+
yield 3
|
|
362
|
+
|
|
363
|
+
def main():
|
|
364
|
+
for i in my_generator():
|
|
365
|
+
print(i)
|
|
366
|
+
print('sum:', sum(my_generator()))
|
|
367
|
+
|
|
368
|
+
main()
|
|
369
|
+
```
|
|
370
|
+
```
|
|
371
|
+
1
|
|
372
|
+
2
|
|
373
|
+
3
|
|
374
|
+
sum: 6
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
The generator iterable can only be used once. Call the generator again when you need a new iterable:
|
|
378
|
+
|
|
379
|
+
```python
|
|
380
|
+
def my_generator():
|
|
381
|
+
yield 1
|
|
382
|
+
yield 2
|
|
383
|
+
yield 3
|
|
384
|
+
|
|
385
|
+
def main():
|
|
386
|
+
iterable = my_generator()
|
|
387
|
+
print('sum:', sum(iterable)) # 6
|
|
388
|
+
print('sum:', sum(iterable)) # 0, a used up generator doesn't give any values
|
|
389
|
+
iterable = my_generator()
|
|
390
|
+
print('sum:', sum(iterable)) # 6
|
|
391
|
+
|
|
392
|
+
main()
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
A generator is lazy, meaning that it will only produce its values if you request them via the Iterator Protocol. That means that if you print the generator it will just print '<generator object ...>'. To print its values you can for example use `list()` that uses the Iterator Protocol to request its values and converts them to a `list` that can be printed. But then you have used the generator so it no longer has values.
|
|
396
|
+
|
|
397
|
+
```python
|
|
398
|
+
def my_generator():
|
|
399
|
+
yield 1
|
|
400
|
+
yield 2
|
|
401
|
+
yield 3
|
|
402
|
+
|
|
403
|
+
def main():
|
|
404
|
+
my_gen = my_generator()
|
|
405
|
+
print( my_gen )
|
|
406
|
+
print( list(my_gen) ) # printing uses up the generator
|
|
407
|
+
print( list(my_gen) ) # no more values available
|
|
408
|
+
print( list(my_generator()) ) # new generator
|
|
409
|
+
|
|
410
|
+
main()
|
|
411
|
+
```
|
|
412
|
+
```
|
|
413
|
+
<generator object my_generator at 0x7fd965cf0ca0>
|
|
414
|
+
[1, 2, 3]
|
|
415
|
+
[]
|
|
416
|
+
[1, 2, 3]
|
|
417
|
+
```
|
|
418
|
+
|
|
419
|
+
By using invocation_tree we can see how the Iterator Protocol works on the generator.
|
|
420
|
+
|
|
421
|
+
```python
|
|
422
|
+
import invocation_tree as invo_tree
|
|
423
|
+
|
|
424
|
+
def my_generator():
|
|
425
|
+
yield 1
|
|
426
|
+
yield 2
|
|
427
|
+
yield 3
|
|
428
|
+
|
|
429
|
+
def main():
|
|
430
|
+
return list(my_generator())
|
|
431
|
+
|
|
432
|
+
tree = invo_tree.blocking()
|
|
433
|
+
print( tree(main) )
|
|
434
|
+
```
|
|
435
|
+

|
|
436
|
+
```
|
|
437
|
+
[1, 2, 3]
|
|
438
|
+
```
|
|
439
|
+
In `main()`:
|
|
440
|
+
- The 'list(my_generator())' call requests an iterator from the generator.
|
|
441
|
+
- It keep calling next() on it to read the sequence resulting in `my_generator()` calls.
|
|
442
|
+
- When called `my_generator()` yields a value, and then pauses and saves its state, allowing it to continue from where it left off when called again.
|
|
443
|
+
- At the 4th call `my_generator()` returns None and automatically raises a StopIteration exception that signals the end of the sequence and makes `list()` return its result.
|
|
444
|
+
|
|
445
|
+
## Generator Expressions ##
|
|
446
|
+
Another way to create a generator is with a [generator expression](https://docs.python.org/3/reference/expressions.html#generator-expressions) that looks like a list comprehension except that it uses the '(' and ')' parentheses instead of the '[' and ']' brackets. A generator expression reads from an iterable and produces a generator iterable:
|
|
447
|
+
|
|
448
|
+
```python
|
|
449
|
+
import invocation_tree as invo_tree
|
|
450
|
+
|
|
451
|
+
def main():
|
|
452
|
+
my_generator = (i*10 for i in range(1,4)) # generator expression
|
|
453
|
+
return list(my_generator)
|
|
454
|
+
|
|
455
|
+
tree = invo_tree.blocking()
|
|
456
|
+
import types
|
|
457
|
+
tree.to_string[types.GeneratorType] = lambda x: 'generator' # short name for generators
|
|
458
|
+
tree.to_string[type(iter(range(0)))] = lambda x: 'iterator' # short name for iterator
|
|
459
|
+
print( tree(main) )
|
|
460
|
+
```
|
|
461
|
+

|
|
462
|
+
```
|
|
463
|
+
[10, 20, 30]
|
|
464
|
+
```
|
|
465
|
+
## Generator Pipeline ##
|
|
466
|
+
The key advantage of Python generators is their ability to create a **pipeline of computations**, where each generator handles a specific part of the process. Values are processed one at a time and flow through the pipeline lazily, meaning computations are performed only when needed. This eliminates the need to store the entire dataset in memory, such as in a list, making generators highly memory-efficient. Because the computation is split into modular steps, it’s easy to add, remove, or modify generators in the pipeline. This combination of flexibility, low memory usage, and on-demand processing makes generators ideal for handling large datasets or continuous data streams.
|
|
467
|
+
|
|
468
|
+
```python
|
|
469
|
+
import invocation_tree as invo_tree
|
|
470
|
+
|
|
471
|
+
def subtract(pipeline):
|
|
472
|
+
for a in pipeline:
|
|
473
|
+
yield a - 3
|
|
474
|
+
|
|
475
|
+
def multiply(pipeline):
|
|
476
|
+
for a in pipeline:
|
|
477
|
+
yield a * 6
|
|
478
|
+
|
|
479
|
+
def my_sum(pipeline):
|
|
480
|
+
total = 0
|
|
481
|
+
for i in pipeline:
|
|
482
|
+
total += i
|
|
483
|
+
return total # return not yield, so not lazy
|
|
484
|
+
|
|
485
|
+
def main():
|
|
486
|
+
pipeline = range(1,4)
|
|
487
|
+
pipeline = subtract(pipeline)
|
|
488
|
+
pipeline = (a + 9 for a in pipeline)
|
|
489
|
+
pipeline = multiply(pipeline)
|
|
490
|
+
return my_sum(pipeline)
|
|
491
|
+
|
|
492
|
+
tree = invo_tree.blocking()
|
|
493
|
+
import types
|
|
494
|
+
tree.to_string[types.GeneratorType] = lambda x: 'generator' # short name for generators
|
|
495
|
+
tree.to_string[type(iter(range(0)))] = lambda x: 'iterator' # short name for iterator
|
|
496
|
+
print( tree(main) )
|
|
497
|
+
```
|
|
498
|
+

|
|
499
|
+
```
|
|
500
|
+
144
|
|
501
|
+
```
|
|
502
|
+
Note that the generators are lazy but the `sum()` function is not, and that is what is pulling the values through the pipeline one at the time.
|
|
503
|
+
|
|
504
|
+
## Itertools ##
|
|
505
|
+
The pythonic (or idiomatic) way of programming in Python is not to use raw for-loops but to use iterables, generators and [itertools](https://docs.python.org/3/library/itertools.html) functions instead. See for a short introduction:
|
|
506
|
+
|
|
507
|
+
[](https://www.youtube.com/watch?v=guEDsBshGfI)
|
|
508
|
+
|
|
509
|
+
Whenever you write a for-loop, finish it and make it work correctly, but afterwards see of if you can rewrite it with generators and itertools functions. Then in time you will find you can think in terms of generators and itertools from the start. This can make your code shorter, more expressive, easier to change, use less memory, faster, and generally more correct.
|
|
510
|
+
|
|
511
|
+
# Configuration #
|
|
512
|
+
These invocation_tree configurations are available for an `Invocation_Tree` objects:
|
|
513
|
+
|
|
514
|
+
```python
|
|
515
|
+
tree = invo_tree.Invocation_Tree()
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
- **tree.filename** : str
|
|
519
|
+
- filename to save the tree to, defaults to 'tree.pdf'
|
|
520
|
+
- **tree.show** : bool
|
|
521
|
+
- if `True` the default application is open to view 'tree.filename'
|
|
522
|
+
- **tree.block** : bool
|
|
523
|
+
- if `True` program execution is blocked after the tree is saved
|
|
524
|
+
- **tree.src_loc** : bool
|
|
525
|
+
- if `True` the source location is printed when blocking
|
|
526
|
+
- **tree.each_line** : bool
|
|
527
|
+
- if `True` each line of the program is stepped through
|
|
528
|
+
- **tree.max_string_len** : int
|
|
529
|
+
- the maximum string length, only the end is shown of longer strings
|
|
530
|
+
- **tree.gifcount** : int
|
|
531
|
+
- if `>=0` the out filename is numbered for animated gif making
|
|
532
|
+
- **tree.indent** : string
|
|
533
|
+
- the string used for identing the local variables
|
|
534
|
+
- **tree.color_active** : string
|
|
535
|
+
- HTML color for active function
|
|
536
|
+
- **tree.color_paused*** : string
|
|
537
|
+
- HTML color for paused functions
|
|
538
|
+
- **tree.color_returned***: string
|
|
539
|
+
- HTML color for returned functions
|
|
540
|
+
- **tree.hide** : set()
|
|
541
|
+
- set of all variables names that are not shown in the tree
|
|
542
|
+
- **tree.to_string** : dict[str, fun]
|
|
543
|
+
- mapping from type/name to a to_string() function for custom printing of values
|
|
544
|
+
|
|
545
|
+
For convenience we provide these functions to set common configurations:
|
|
546
|
+
|
|
547
|
+
- **invo_tree.blocking(filename)**, blocks on function call and return
|
|
548
|
+
- **invo_tree.blocking_each_change(filename)**, blocks on each change of value
|
|
549
|
+
- **invo_tree.debugger(filename)**, non-blocking for use in debugger tool (open <filename> manually)
|
|
550
|
+
- **invo_tree.gif(filename)**, generates many output files on function call and return for gif creation
|
|
551
|
+
- **invo_tree.gif_each_change(filename)**, generates many output files on each change of value for gif creation
|
|
552
|
+
- **invo_tree.non_blocking(filename)**, non-blocking on each function call and return
|
|
553
|
+
|
|
554
|
+
# Troubleshooting #
|
|
555
|
+
- Adobe Acrobat Reader [doesn't refresh a PDF file](https://superuser.com/questions/337011/windows-pdf-viewer-that-auto-refreshes-pdf-when-compiling-with-pdflatex) when it changes on disk and blocks updates which results in an `Could not open 'somefile.pdf' for writing : Permission denied` error. One solution is to install a PDF reader that does refresh ([Evince](https://www.fosshub.com/Evince.html), [Okular](https://okular.kde.org/), [SumatraPDF](https://www.sumatrapdfreader.org/), ...) and set it as the default PDF reader. Another solution is to save the tree to a different [Graphviz Output Format](https://graphviz.org/docs/outputs/).
|
|
556
|
+
|
|
557
|
+
## Memory_Graph Package ##
|
|
558
|
+
The [invocation_tree](https://pypi.org/project/invocation-tree/) package visualizes function calls at different moments in time. If instead you want a detailed visualization of your data at the current time, check out the [memory_graph](https://pypi.org/project/memory-graph/) package.
|