Skip to content

Instantly share code, notes, and snippets.

@tcard
Last active August 29, 2015 14:06
Show Gist options
  • Save tcard/9dc4da3457c9d3cdbcad to your computer and use it in GitHub Desktop.
Save tcard/9dc4da3457c9d3cdbcad to your computer and use it in GitHub Desktop.
Print-based debugger for recursive procedures
# 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.
<<
"""
#! /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())
@ajspadial
Copy link

Should that work?

python example.py | recdebug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment