Command-line tool able to take multiple commands at one time?

P

Peter A. Schott

Per subject - I realize I can copy/paste a line at a time into an interactive
session when I'm trying to debug, but was wondering if there is any tool out
there that allows me to copy sections of working Python scripts to paste into my
interactive console and let those run so I don't have to copy line-by-line.

Not sure if iPython would meet that criteria or not or even if such a beast
exists. It would be helpful for debugging some of my simpler, yet still lengthy
at times, scripts.

Thanks in advance.

-Pete Schott
 
D

Devan L

Peter said:
Per subject - I realize I can copy/paste a line at a time into an interactive
session when I'm trying to debug, but was wondering if there is any tool out
there that allows me to copy sections of working Python scripts to paste into my
interactive console and let those run so I don't have to copy line-by-line.

Not sure if iPython would meet that criteria or not or even if such a beast
exists. It would be helpful for debugging some of my simpler, yet still lengthy
at times, scripts.

Thanks in advance.

-Pete Schott

It's called IDLE (At least, the interactive session with it). Some
people dislike IDLE, though.
 
M

Mike Meyer

Peter A. Schott said:
Per subject - I realize I can copy/paste a line at a time into an interactive
session when I'm trying to debug, but was wondering if there is any tool out
there that allows me to copy sections of working Python scripts to paste into my
interactive console and let those run so I don't have to copy line-by-line.

Um, just curious - doesn't copying multiple lines at a time work now?
That works fine for me on both OS X and Unix.

<mike
 
F

Fernando Perez

Peter said:
Per subject - I realize I can copy/paste a line at a time into an interactive
session when I'm trying to debug, but was wondering if there is any tool out
there that allows me to copy sections of working Python scripts to paste into
my interactive console and let those run so I don't have to copy line-by-line.

Not sure if iPython would meet that criteria or not or even if such a beast
exists. It would be helpful for debugging some of my simpler, yet still
lengthy at times, scripts.

ipython has a %run command designed precisely to do this. You can keep your
favorite editor open and work on your code. You just need to save and type
into ipython 'run scriptname', to re-execute the code each time just as if
you'd typed it. Note that ipython uses readline, so you just need to type 'r'
and hit the up-arrow key to retrieve the previous command starting with the
letter r.

Type 'run?' (without the quotes) into ipython to get the gory details on the run
command, or have a look at the manual here (scroll down to the %run part):

http://ipython.scipy.org/doc/manual/node6.html#SECTION00062100000000000000

Feel free to ask further questions on the ipython list, I don't keep up with
c.l.py too well these days, unfortunately.

Cheers,

f
 
B

bruno at modulix

Peter said:
Per subject - I realize I can copy/paste a line at a time into an interactive
session when I'm trying to debug, but was wondering if there is any tool out
there that allows me to copy sections of working Python scripts to paste into my
interactive console and let those run so I don't have to copy line-by-line.

There's much better than that : emacs + python-mode let you evaluate
either the whole script, a single class or def statement, or an
arbitrary region in an interactive Python interpreter running as an
emacs sub-process. Of course, this interpreter don't die after, so you
can inspect/play with/do whatever with the results of this evaluation.
 
P

Peter A. Schott

I'll give it a try. I've been using PythonWin and SPE recently and hadn't
really messed with IDLE too much as I liked the somewhat more advanced features
of the other IDEs. I'll check it out again.

Also thanks to all for the IP hints - I may check those out as well.

-Pete
 
P

Peter A. Schott

OK - I justed tested and may be doing something wrong, but it didn't work when I
just tried it.

I have something like this:

X = "Value1"
Y = "Value2"
Z = "Value3"

etc at the top of my script. When I copy/paste those three lines all at once
into IDLE's interactive window, X is defined, Y and Z are not.

That's more the behaviour I was hoping for - the ability to run parts of my code
at a time in order to work through issues without too much trouble in
mostly-working code.

TIA,

-Pete Schott
 
D

Devan L

Peter said:
OK - I justed tested and may be doing something wrong, but it didn't work when I
just tried it.

I have something like this:

X = "Value1"
Y = "Value2"
Z = "Value3"

etc at the top of my script. When I copy/paste those three lines all at once
into IDLE's interactive window, X is defined, Y and Z are not.

That's more the behaviour I was hoping for - the ability to run parts of my code
at a time in order to work through issues without too much trouble in
mostly-working code.

TIA,

-Pete Schott

Oh, that kind of multi-line input. I thought you were talking about.... long_statement_here
.... even_longer_statement_here
.... return something

I would just put it into a .py and run it through IDLE, change it, and
rerun it. There are probably other IDEs which can already do that, but
I just use IDLE.
 
B

Bengt Richter

Per subject - I realize I can copy/paste a line at a time into an interactive
session when I'm trying to debug, but was wondering if there is any tool out
there that allows me to copy sections of working Python scripts to paste into my
interactive console and let those run so I don't have to copy line-by-line.
What are you pasting into? Idle? win32 console? via telnet?
The win32 console you get by running python.exe from the cmd.exe windows shell
permits multi-line copying and pasting (Alt-spacebar,e,k,<hold shift down>
<select-motions></hold shift down>,Enter and Alt-spacebar,e,p respectively).
(You can also use <hold left mouse down> and move mouse for <select-motions>
instead of arrow keys, as a substitute for <hold shift down>)

The trick is that unlike executing file-based source lines as in import or execfile,
compilation has to be triggered by something acting like EOF. In the interactive mode
that is statement by statement until a statement is followed by an indented suite.
Then the compilation and execution of the indented suite is postponed until a blank line.

This is not really a good substitute for EOF or some other explicit EOF-emulating escape
from the suite input mode, because you can't switch to an editor and copy something that might have
arbitrary blank lines and compacted code that would import fine and expect it to paste fine.
IMO this is a wart of the interactive mode that could be improved (e.g, once into a multiline
sequence of input, terminate on ctl-something instead of blank line).

By experimentation you can see what chunks you can paste. E.g., try two function defs in a
single paste of the four lines:
def foo():
print 'foo'
def bar():
print 'bar'
... print 'foo'
... def bar():
File "<stdin>", line 3
def bar():
^
SyntaxError: invalid syntax File "<stdin>", line 1
print 'bar'
^
SyntaxError: invalid syntax

vs pasting them as five lines including a separting blank line:
def foo():
print 'foo'

def bar():
print 'bar'
... print 'foo'
... ... print 'bar'
...
(I indent the paste here of interactive stuff one space to avoid
false quote highlighting in newsreaders).
Not sure if iPython would meet that criteria or not or even if such a beast
exists. It would be helpful for debugging some of my simpler, yet still lengthy
at times, scripts.

Plain vanilla gvim (popular editor) will let you invoke a program as filter on
anything you select in your editing window, and it will cut the selection and paste
the execution output in its place, so e.g., the above, starting with a screen like

# select starts on next line
def foo():
print 'foo'
def bar():
print 'bar'
#select ends at end of last line

and selecting the four lines and then typing :!python and pressing Enter,

what you get is
# select starts on next line
#select ends at beginning of this line (includes \n of last line)

Seems not very useful, since no output was generated by correct execution of the defs.
If there had been a syntax error, the same would have happened, unless stderr is captured
into the output stream.

Since my version of windows (ancient NT4) has the infamous redirection problem, I have to
invoke scripts via a single-line command file that runs python explicitly in order to capture
output by piping or redirection. So I have two versions, one that redirects stderr also. E.g.,

py24.cmd contains the single line
@d:\python-2.4b1\mingw\python24.exe %*

and py24e.cmd contains the single line
@d:\python-2.4b1\mingw\python24.exe %* 2>&1

So, now if there had been a syntax error, e.g., (restoring previous by hitting u key for undo
and editing in and example error)

# select starts on next line
def foo():
print 'foo'
def bar() # make syntax error :))
print 'bar'
#select ends at beginning of this line (includes \n of last line)

Then we do the same and use py24e and get:

# select starts on next line
File "<stdin>", line 3
def bar() # make syntax error :))
^
SyntaxError: invalid syntax
#select ends at beginning of this line (includes \n of last line)

And an undo will get us back.
Executing this way (as a filter action) you don't have to worry about
putting blank lines or not in the pasted material.

If we fix the error and put in a print for actual output, we'll get that:

# select starts on next line
def foo():
print 'foo'
def bar(): # fixed syntax error :))
print 'bar'
foo(); bar() # expect output
#select ends at beginning of this line (includes \n of last line)

And then :!py24e after selecting the five lines gets

# select starts on next line
foo
bar
#select ends at beginning of this line (includes \n of last line)


Of course, you can do these two things in parallel. Not to mention extremely
flexible possibilities of mapping keys or key-sequences to almost anything
imaginable, if you want to go beyond plain vanilla. E.g., non-destructive
results in a split window is nicer.

But filters are easy and fun to invoke.

Of csuroe, you can do tsehe two thgnis
in paellarl. Not to menotin emxteerly
fblleixe pistisibleios of mpipang keys
or kqs-ycuneeees to aosmlt annhytig
inmgibalae, if you wnat to go bnyoed
pilan vlnaila. E.g., nnoi-csudvttere
rteluss in a spilt wdionw is neicr.

(last paragraph filtered through scramble and a paragraph-justifying
script specifying 8 margin 40 wide and justify to both edges ;-)

So what platform are you on?

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top