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

Discussion in 'Python' started by Peter A. Schott, Nov 11, 2005.

  1. 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
     
    Peter A. Schott, Nov 11, 2005
    #1
    1. Advertising

  2. Peter A. Schott

    Devan L Guest

    Peter A. Schott wrote:
    > 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.
     
    Devan L, Nov 11, 2005
    #2
    1. Advertising

  3. Peter A. Schott

    Mike Meyer Guest

    Peter A. Schott <> writes:
    > 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
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
     
    Mike Meyer, Nov 11, 2005
    #3
  4. Peter A.Schott wrote:

    > 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
     
    Fernando Perez, Nov 11, 2005
    #4
  5. Peter A. Schott wrote:
    > 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.

    --
    bruno desthuilliers
    python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
    p in ''.split('@')])"
     
    bruno at modulix, Nov 11, 2005
    #5
  6. 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

    "Devan L" <> wrote:

    >
    > Peter A. Schott wrote:
    > > 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.
     
    Peter A. Schott, Nov 11, 2005
    #6
  7. 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

    Mike Meyer <> wrote:

    > Peter A. Schott <> writes:
    > > 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
     
    Peter A. Schott, Nov 11, 2005
    #7
  8. Peter A. Schott

    Devan L Guest

    Peter A. Schott wrote:
    > 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
    >>> def foo(bar):

    .... 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.
     
    Devan L, Nov 11, 2005
    #8
  9. On Fri, 11 Nov 2005 01:05:56 GMT, Peter A. Schott <> wrote:

    >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'

    >>> def foo():

    ... print 'foo'
    ... def bar():
    File "<stdin>", line 3
    def bar():
    ^
    SyntaxError: invalid syntax
    >>> print 'bar'

    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'

    >>> def foo():

    ... print 'foo'
    ...
    >>> def bar():

    ... 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
     
    Bengt Richter, Nov 12, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pankaj Jain
    Replies:
    1
    Views:
    339
    Daniel Fisher\(lennybacon\)
    Nov 30, 2005
  2. Ivan Shevanski
    Replies:
    3
    Views:
    400
    Mike Meyer
    Jul 1, 2005
  3. Max Power
    Replies:
    2
    Views:
    462
    Max Power
    Dec 3, 2009
  4. sed_y
    Replies:
    0
    Views:
    1,035
    sed_y
    Feb 15, 2012
  5. Replies:
    13
    Views:
    235
Loading...

Share This Page