Last active
August 29, 2015 14:06
-
-
Save tcard/9dc4da3457c9d3cdbcad to your computer and use it in GitHub Desktop.
Print-based debugger for recursive procedures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# If you run this program: | |
def fibo(n): | |
print '>>' | |
ret = 1 if n == 0 or n == 1 else fibo(n - 1) + fibo(n - 2) | |
print "Got %d, returning %d." % (n, ret) | |
print '<<' | |
return ret | |
fibo(5) | |
# And put its output as input to recdebug, you get: | |
""" | |
>> | |
>> | |
>> | |
>> | |
Got 1, returning 1. | |
<< | |
>> | |
Got 0, returning 1. | |
<< | |
Got 2, returning 2. | |
<< | |
>> | |
Got 1, returning 1. | |
<< | |
Got 3, returning 3. | |
<< | |
>> | |
>> | |
Got 1, returning 1. | |
<< | |
>> | |
Got 0, returning 1. | |
<< | |
Got 2, returning 2. | |
<< | |
Got 4, returning 5. | |
<< | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys | |
def recdebug(input): | |
output = '' | |
lvl = 0 | |
for l in input: | |
if l == "<<": | |
lvl -= 1 | |
output += ("\t" * lvl) + l + '\n' | |
if l == ">>": | |
lvl += 1 | |
return output | |
if __name__ == '__main__': | |
import sys | |
print recdebug(sys.stdin.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Should that work?
python example.py | recdebug