Bare bones library to vizualize recursion with one line of code.
Project description
About viz.recurse
viz.recurse is a bare-bones library to easily visualize recursion without any significant overhead on the part of the user. This library utilizes common graph and visualization libraries (networkx, matplotlib, pygraphviz).
Latest PyPi release [1.1.3]
Visit https://pypi.org/project/vizrecurse/1.1.3/
Requirements
Python 3.10.13
pip 23.0.1
(graphviz compatibility)
brew install graphviz
(MacOS)
pip3 install -r requirements.txt
If running into problems installing pygraphviz
:
export GRAPHVIZ_DIR="$(brew --prefix graphviz)"
pip install pygraphviz \
--config-settings=--global-option=build_ext \
--config-settings=--global-option="-I$GRAPHVIZ_DIR/include" \
--config-settings=--global-option="-L$GRAPHVIZ_DIR/lib"
Usage
Attach @visualize
decorator to the top of your recursive function and run as normal. e.g.
@visualize
def my_function(*args, **kwargs): ...
res = my_function(x=1,y=2)
draw()
Examples
Visit the examples folder to run these for yourself. Each is runnable as a standalone script within the context.
How it works (execution flow)
@visualize
def toh(args, kwargs): ...
<__name__ = __main__ context> # <-- [snapshot_1] prev on call stack
This calls visualize(func) # no impact
visualize(func) his returns inner(*args, **kwargs)
inner(*args, **kwargs) is executed # <-- [snapshot_1] cur on call stack, [snapshot_2] prev on call stack
this calls custom function toh()
toh() calls visualize(func)
this returns inner(*args, **kwargs)
inner(*args, **kwargs) is executed # <-- [snapshot_2] cur on call stack
...
A poem from our silicon friends
In the realm of code where logic flows, A concept profound like a river that knows, There lies recursion, subtle and grand, A method that holds its own hand.
Oh, recursion, you elusive sprite, A function that calls itself in the night. You start with a base case, firm and true, A stopping point in the labyrinth's view.
Within your loops, a story we find, A mirrored path of the wandering mind. You take a step forward, then call once more, Repeating the journey of those gone before.
In Fibonacci's sequence or fractal's embrace, Your essence reveals a recursive trace. The Tower of Hanoi, a puzzle unfurled, Solved by the echoes of recursive world.
Beware, dear coder, of infinite plight, Without a base case, you're lost in the night. An endless call, a stack overflow, A recursive loop with nowhere to go.
Yet with careful craft and logic precise, Recursion becomes a coder's delight. Elegant, clear, a beauty retained, A self-referential dance unchained.
So here’s to recursion, a loop within, A cycle that folds, yet begins again. In the code’s deep heart, it softly sings, The endless echo of programming’s rings.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for vizrecurse-1.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40adaefadbf405b21f122b070eb0fe2c1a1aa7290fc3853155f5facce2fc0bdf |
|
MD5 | 67dcff3347d067e7fa4f2ea1424cee2a |
|
BLAKE2b-256 | 90112bb98f33f38a5fe6e762c8d622d347dd240fbced5b6f866f8cecfde20e09 |