Skip to content

Commit 364fd5a

Browse files
author
sarangbishal
committed
Add Initial version
1 parent 887dae1 commit 364fd5a

File tree

9 files changed

+135
-0
lines changed

9 files changed

+135
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
test*
2+
*.png
3+
.idea
14
# Byte-compiled / optimized / DLL files
25
__pycache__/
36
*.py[cod]

.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/Recursion-Visualizer.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import inspect
2+
3+
import sys
4+
from functools import wraps
5+
from collections import OrderedDict
6+
import pydot
7+
8+
9+
graph = pydot.Dot(graph_type="digraph")
10+
node_count = 0
11+
12+
class TraceCalls(object):
13+
def __init__(self, stream=sys.stdout, indent_step=2, show_ret=False):
14+
self.stream = stream
15+
self.indent_step = indent_step
16+
self.show_ret = show_ret
17+
18+
19+
20+
def __call__(self, fn):
21+
@wraps(fn)
22+
def wrapper(*args, **kwargs):
23+
24+
kwargs = OrderedDict(sorted(kwargs.items()))
25+
argstr = ', '.join(
26+
[repr(a) for a in args] +
27+
["%s=%s" % (a, repr(b)) for a, b in kwargs.items()])
28+
29+
# Current Function
30+
current_func_name = fn.__name__
31+
current_func_args = argstr
32+
current_func_signature = f"{current_func_name}({current_func_args})"
33+
34+
# Caller Function
35+
caller_func_name = sys._getframe(1).f_code.co_name
36+
caller_func_arg_names = sys._getframe(1).f_code.co_varnames[:fn.__code__.co_argcount]
37+
38+
caller_func_args = sys._getframe(1).f_locals
39+
caller_func_args = OrderedDict(sorted(caller_func_args.items()))
40+
41+
caller_func_args = ', '.join(
42+
[f"{key}={value}" for key, value in caller_func_args.items() if key in caller_func_arg_names])
43+
caller_func_signature = f"{caller_func_name}({caller_func_args})"
44+
45+
46+
# # Child Node
47+
child_label = current_func_signature
48+
child_name = current_func_signature
49+
v = pydot.Node(name=child_name, label=child_label)
50+
graph.add_node(v)
51+
52+
# Parent Node
53+
u = None
54+
55+
if caller_func_name == '<module>':
56+
print("Start Call")
57+
print(f"Drawing for {current_func_signature}")
58+
else:
59+
print(f"Called {current_func_signature} by {caller_func_signature}")
60+
u = pydot.Node(name=caller_func_signature, label=caller_func_signature)
61+
graph.add_node(u)
62+
edge = pydot.Edge(u, v)
63+
graph.add_edge(edge)
64+
65+
result = fn(*args, **kwargs)
66+
return result
67+
68+
return wrapper
69+
70+
@TraceCalls()
71+
def fib(n, node_num):
72+
global node_count
73+
if n <= 1:
74+
return n
75+
node_count += 1
76+
left = fib(n=n - 1, node_num=node_count)
77+
node_count += 1
78+
right = fib(n=n - 2, node_num=node_count)
79+
return left +right
80+
81+
@TraceCalls()
82+
def fact(n, node_num):
83+
global node_count
84+
if n == 1:
85+
return 1
86+
87+
node_count += 1
88+
return n * fact(n=n - 1, node_num=node_count)
89+
90+
print(fib(n=4, node_num=0))
91+
graph.write_png('hawa.png')

0 commit comments

Comments
 (0)