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 7 1846
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>
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-----
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 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
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>
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>
Fredrik Lundh wrote: replacing sys.stdin with something that isn't a TTY will fix this.
This works like magic! Thank you!
Bo This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Miki Tebeka |
last post by:
Hello All,
If there a way a script can tell Python to enter interactive mode even if
the -i command line switch was not given?
I want py2exe to create an interactive session, without writing...
|
by: Joe |
last post by:
When you run "python -i scriptname.py" after the script completes you left
at the interactive command prompt.
Is there a way to have this occur from a running program?
In other words can I...
|
by: siggy2 |
last post by:
Hi All,
(sorry for my bad english)
I wrote a __tiny__ and __stupid__ recursive script directly into
pythonwin
interactive window with a time.sleep(1) and a print before each
recursion...
I...
|
by: WJ |
last post by:
I have three ASPX pages:
1. "WebForm1.aspx" is interactive, responsible for calling a web site
(https://www.payMe.com) with $$$. It is working fine.
2. "WebForm2.aspx" is non-interactive, a...
|
by: Adam Blinkinsop |
last post by:
I'm writing a set of modules to monitor remote system services, and I'm
having a problem running my test scripts. When I pass the scripts into
python, like so:
--
$ PYTHONPATH="${TARGET_DIR}"...
|
by: Jonathan Mark |
last post by:
Some languages, such as Scheme, permit you to make a transcript of an
interactive console session. Is there a way to do that in Python?
|
by: boris.smirnov |
last post by:
Hi there,
I have a problem with setting environment variable in my script that
uses qt library. For this library I have to define a path to tell the
script whre to find it.
I have a script...
|
by: Joshua J. Kugler |
last post by:
Yes, I've read this:
http://mail.python.org/pipermail/python-list/2006-August/395943.html
That's not my problem.
I installed PlanetPlanet <http://www.planetplanet.org/via the
package's...
|
by: Anthony |
last post by:
Hi, I'm a FoxPro programmer, but I want to learn python before it's
too late. I do a lot of statistical programming, so I import SPSS
into python. In my opinion, the best features of Visual...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |