Utility for showing script execution?
Fredrik Lundh
fredrik at pythonware.com
Thu Feb 15 02:45:50 EST 2001
Paul F. Dubois wrote:
> I'm looking for a utility that will take a Python .py file and execute it,
> producing output that shows the "input" lines, marked with a prompt, and
> then the resulting output, so that one has an output text file that
> simulates what a user would see. Surely someone already did this?
of course!
# adapted from code-example-1.py in
# the eff-bot guide to the python standard library
import code
import string
SCRIPT = """
a = 5
for i in range(a):
print i
print 'hello'
"""
chunk = ""
for line in string.split(SCRIPT, "\n"):
chunk = chunk + line + "\n"
co = code.compile_command(chunk, "<stdin>", "exec")
if co:
# got a complete statement. execute it!
prompt = ">>>"
if string.count(chunk, "\n") == 1:
print prompt, chunk,
else:
for line in string.split(chunk, "\n"):
print prompt, line
prompt = "..."
exec co
chunk = ""
Cheers /F
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list