By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

How to obtain a 'interactive session' of a script?

P: n/a
Dear list,

I have a long list of commands in the form of a script and would like to
obtain a log file as if I enter the commands one by one. (The output
will be used in a tutorial.) What would be the best way to do it? Copy
and paste is not acceptable since I make frequent changes tot he script.

Many thanks in advance.
Bo
Sep 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Bo Peng wrote:
I have a long list of commands in the form of a script and would like to
obtain a log file as if I enter the commands one by one. (The output
will be used in a tutorial.) What would be the best way to do it? Copy
and paste is not acceptable since I make frequent changes tot he script.


the first example on this page

http://effbot.org/librarybook/code.htm

shows how to execute Python code line by line.

here's a variation that echoes the script fragments with the right prompts
in front of them:

import code

SCRIPT = [line.rstrip() for line in open("myscript.py")]

script = ""
prompt = ">>>"

for line in SCRIPT:
print prompt, line
script = script + line + "\n"
co = code.compile_command(script, "<stdin>", "exec")
if co:
# got a complete statement. execute it!
exec co
script = ""
prompt = ">>>"
else:
prompt = "..."

</F>

Sep 18 '05 #2

P: n/a
The 'code' module contains 'Utilities needed to emulate Python's interactive
interpreter.'. By subclassing code.InteractiveConsole and replacing the
raw_input method with one which reads from a file, I think you can get what you
want.

The example below the classes uses StringIO so that it can be self-contained,
but you'll probably use a regular file instead.

import code, sys

class BoPeng(code.InteractiveConsole):
def __init__(self, locals=None, filename="<console>", file = None):
self.file = file or open(filename)
code.InteractiveConsole.__init__(self, locals, filename)

def raw_input(self, prompt):
l = self.file.readline()
if l == '': raise EOFError
sys.stdout.write(prompt + l)
return l.strip("\n")

session = '''\
print 3+3
for i in range(10):
print i

print "Example of a traceback:"
1/0
'''

import StringIO
b = BoPeng(file = StringIO.StringIO(session))
b.interact(None)

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFDLYekJd01MZaTXX0RAnf4AKCU84FVVK+2pgx3yQS5IB QcoK6wwACgmMaM
8zYxjYispPtHhknnges00UE=
=yUXD
-----END PGP SIGNATURE-----

Sep 18 '05 #3

P: n/a

Thank you for the suggestions and code!
import code

SCRIPT = [line.rstrip() for line in open("myscript.py")]

script = ""
prompt = ">>>"

for line in SCRIPT:
print prompt, line
script = script + line + "\n"
co = code.compile_command(script, "<stdin>", "exec")
if co:
# got a complete statement. execute it!
exec co
script = ""
prompt = ">>>"
else:
prompt = "..."


This one fails at function definition.

def fun():
a=1
b=2 <--- not included.

Still trying other methods.
Bo
Sep 18 '05 #4

P: n/a
je****@unpythonic.net wrote:
The 'code' module contains 'Utilities needed to emulate Python's interactive
interpreter.'. By subclassing code.InteractiveConsole and replacing the
raw_input method with one which reads from a file, I think you can get what you
want.


This method works fine with only one minor problem. It would stop
(waiting for user input) at help(str) command. I will have to find a way
to feed the program with'q' etc.

Bo
Sep 18 '05 #5

P: n/a
Bo Peng wrote:
import code

SCRIPT = [line.rstrip() for line in open("myscript.py")]

script = ""
prompt = ">>>"

for line in SCRIPT:
print prompt, line
script = script + line + "\n"
co = code.compile_command(script, "<stdin>", "exec")
if co:
# got a complete statement. execute it!
exec co
script = ""
prompt = ">>>"
else:
prompt = "..."


This one fails at function definition.

def fun():
a=1
b=2 <--- not included.


hmm. looks like a bug in compile_command. stripping off the trailing
newline seems to fix it:

co = code.compile_command(script[:-1], "<stdin>", "exec")

(to make things look right, you need to add an empty line after each
function definition in your code)

</F>

Sep 18 '05 #6

P: n/a
Bo Peng wrote:
This method works fine with only one minor problem. It would stop
(waiting for user input) at help(str) command. I will have to find a way
to feed the program with'q' etc.


replacing sys.stdin with something that isn't a TTY will fix this.

here's one way to do it:

class wrapper:
def __init__(self, file):
self.file = file
def isatty(self):
return 0
def __getattr__(self, key):
return getattr(self.file, key)

sys.stdin = wrapper(sys.stdin)

</F>

Sep 18 '05 #7

P: n/a
Fredrik Lundh wrote:
replacing sys.stdin with something that isn't a TTY will fix this.


This works like magic! Thank you!

Bo
Sep 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.