Recursion Trace
Overview
recursion_trace is a Python package that provides a decorator to trace recursive function calls. It generates a visual recursion tree using Graphviz, making it easier to understand and debug recursive algorithms.
Features
- Trace single and mutual recursion.
- Generate a Graphviz plot to visualize the recursion tree.
- Capture function arguments, return values, and recursion depth.
- Render Animation demonstrating construction of recursion-tree
Installation
- Download the graphviz for the respective system (Windows/Mac/Linux)
pip install recursion-trace
Usage
- Decorate the in-usage recursive functions with the decorator
@trace_recursion
Example: Merge Sort with Animation
from recursion_trace import trace_recursion, show_recursion_tree
@trace_recursion # use the decorator to trace the recursion stack
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
L = merge_sort(left_half)
R = merge_sort(right_half)
return merge(L, R)
def merge(left, right):
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
if __name__ == '__main__':
arr = [3, 1, 4, 1, 5, 9, 2, 6, 5]
sorted_arr = merge_sort(arr)
show_recursion_tree(make_animation=True) # display recursion tree
# setting make_animation=True also renders an animation of recursion-tree construction
Output:
https://github.com/practice404/recursion-trace/assets/74960567/f6fbd987-5b77-42c8-9e3a-25096a8f4466
Example: Mutual Recursive Functions
from recursion_trace import trace_recursion, show_recursion_tree
@trace_recursion # use the decorator to trace the recursion stack
def is_even(n):
if n == 0:
return True
return is_odd(n - 1)
@trace_recursion # use the decorator to trace the recursion stack
def is_odd(n):
if n == 0:
return False
return is_even(n - 1)
if __name__ == '__main__':
is_even(4)
show_recursion_tree()
Output:
Author
Metadata
Release files for recursion-trace 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| recursion_trace-0.2.1.tar.gz | 3.3 kB | Without provenance | Details for recursion_trace-0.2.1.tar.gz |
Release files / recursion_trace-0.2.1.tar.gz
| Download URL | recursion_trace-0.2.1.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f7b837251def0bd500d9c8c86111faef590f2a0294f52e4e2e2f76b2c3c5286e
|
|
BLAKE2b-256 checksum How to use checksums |
8470ef4d8f2045abcd0fe5b9f0ba632e5baefc9e570712b14bd921d458fa3a5e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for recursion-trace 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.