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

2005-09-18 Thread Bo Peng
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

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

2005-09-18 Thread Fredrik Lundh
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

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

2005-09-18 Thread jepler
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

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

2005-09-18 Thread Bo Peng
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

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

2005-09-18 Thread Bo Peng
[EMAIL PROTECTED] 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

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

2005-09-18 Thread Fredrik Lundh
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

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

2005-09-18 Thread Fredrik Lundh
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

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

2005-09-18 Thread Bo Peng
Fredrik Lundh wrote: replacing sys.stdin with something that isn't a TTY will fix this. This works like magic! Thank you! Bo -- http://mail.python.org/mailman/listinfo/python-list