Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #3323 > unrolled thread

[Q] ipython: Multiple commands on the same line and newlines

Started byPhil Winder <philipwinder@gmail.com>
First post2011-04-16 06:55 -0700
Last post2011-04-17 04:22 +0000
Articles 11 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  [Q] ipython: Multiple commands on the same line and newlines Phil Winder <philipwinder@gmail.com> - 2011-04-16 06:55 -0700
    Re: [Q] ipython: Multiple commands on the same line and newlines Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-04-16 18:29 +0200
      Re: ipython: Multiple commands on the same line and newlines Phil Winder <philipwinder@gmail.com> - 2011-04-16 09:37 -0700
        Re: ipython: Multiple commands on the same line and newlines Andrea Crotti <andrea.crotti.0@gmail.com> - 2011-04-17 14:11 +0200
          Re: ipython: Multiple commands on the same line and newlines Phil Winder <philipwinder@gmail.com> - 2011-04-17 11:40 -0700
            Re: ipython: Multiple commands on the same line and newlines Alexander Kapps <alex.kapps@web.de> - 2011-04-17 21:39 +0200
    Re: [Q] ipython: Multiple commands on the same line and newlines Chris Angelico <rosuav@gmail.com> - 2011-04-17 02:33 +1000
    Re: [Q] ipython: Multiple commands on the same line and newlines Terry Reedy <tjreedy@udel.edu> - 2011-04-16 12:39 -0400
      Re: [Q] ipython: Multiple commands on the same line and newlines harrismh777 <harrismh777@charter.net> - 2011-04-18 01:01 -0500
        Re: [Q] ipython: Multiple commands on the same line and newlines Chris Angelico <rosuav@gmail.com> - 2011-04-18 16:12 +1000
    Re: [Q] ipython: Multiple commands on the same line and newlines "OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net> - 2011-04-17 04:22 +0000

#3323 — [Q] ipython: Multiple commands on the same line and newlines

FromPhil Winder <philipwinder@gmail.com>
Date2011-04-16 06:55 -0700
Subject[Q] ipython: Multiple commands on the same line and newlines
Message-ID<8dc6c9e4-e50e-401f-95d9-42f80a90196c@l30g2000vbn.googlegroups.com>
Hi,
I'm having a go at using ipython as a command prompt for data
analysis. Coming from Matlab, I'm used to typing multiple commands on
the same line then using the up arrow to go through my history.
How can I write multiple python commands on the same line?
E.g. "x = 0; while x < 10: x = x + 1;" returns an "invalid syntax"
error on the 'e' in while.

Also, how can I produce a new line, without it running the command? I
would have expected a ctrl-enter or shift-enter to produce the
expected results.
E.g. I want:
"x = 0; <ctrl-enter>
while x < 10: <ctrl-enter>
    x = x + 1; <ctrl-enter>
" <enter to run>
It seems to work automatically for the "while xxx:", but combinations
of keys+enter do not work for "normal" lines.

Cheers,
Phil

[toc] | [next] | [standalone]


#3331

FromAndrea Crotti <andrea.crotti.0@gmail.com>
Date2011-04-16 18:29 +0200
Message-ID<mailman.432.1302971361.9059.python-list@python.org>
In reply to#3323
Phil Winder <philipwinder@gmail.com> writes:

> Hi,
> I'm having a go at using ipython as a command prompt for data
> analysis. Coming from Matlab, I'm used to typing multiple commands on
> the same line then using the up arrow to go through my history.
> How can I write multiple python commands on the same line?
> E.g. "x = 0; while x < 10: x = x + 1;" returns an "invalid syntax"
> error on the 'e' in while.
>
> Also, how can I produce a new line, without it running the command? I
> would have expected a ctrl-enter or shift-enter to produce the
> expected results.
> E.g. I want:
> "x = 0; <ctrl-enter>
> while x < 10: <ctrl-enter>
>     x = x + 1; <ctrl-enter>
> " <enter to run>
> It seems to work automatically for the "while xxx:", but combinations
> of keys+enter do not work for "normal" lines.
>
> Cheers,
> Phil

Well when you do something like

while x < 10:

it doesn't execute anything, but goes to newline and waits for the rest.

for
x = 10

what's the difference for you if it gets evaluated before or after?
Anyway you can you also %cpaste if you want to write more code

Anyway to me this works perfectly:
In [1]: x = 0

In [2]: while x < 10: print x; x += 1
   ...: 
0
1
2
3
4
5
6
7
8
9

[toc] | [prev] | [next] | [standalone]


#3336 — Re: ipython: Multiple commands on the same line and newlines

FromPhil Winder <philipwinder@gmail.com>
Date2011-04-16 09:37 -0700
SubjectRe: ipython: Multiple commands on the same line and newlines
Message-ID<0eb06f93-88d0-4ff8-b21f-bb3264305d76@34g2000pru.googlegroups.com>
In reply to#3331
On Apr 16, 5:29 pm, Andrea Crotti <andrea.crott...@gmail.com> wrote:
> Phil Winder <philipwin...@gmail.com> writes:
> > Hi,
> > I'm having a go at using ipython as a command prompt for data
> > analysis. Coming from Matlab, I'm used to typing multiple commands on
> > the same line then using the up arrow to go through my history.
> > How can I write multiple python commands on the same line?
> > E.g. "x = 0; while x < 10: x = x + 1;" returns an "invalid syntax"
> > error on the 'e' in while.
>
> > Also, how can I produce a new line, without it running the command? I
> > would have expected a ctrl-enter or shift-enter to produce the
> > expected results.
> > E.g. I want:
> > "x = 0; <ctrl-enter>
> > while x < 10: <ctrl-enter>
> >     x = x + 1; <ctrl-enter>
> > " <enter to run>
> > It seems to work automatically for the "while xxx:", but combinations
> > of keys+enter do not work for "normal" lines.
>
> > Cheers,
> > Phil
>
> Well when you do something like
>
> while x < 10:
>
> it doesn't execute anything, but goes to newline and waits for the rest.
>
> for
> x = 10
>
> what's the difference for you if it gets evaluated before or after?
> Anyway you can you also %cpaste if you want to write more code
>
> Anyway to me this works perfectly:
> In [1]: x = 0
>
> In [2]: while x < 10: print x; x += 1
>    ...:
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9

Yes, that does not produce an error, but it does not "work". Please
refer to my first post. Try the first code, you will get a syntax
error. Placing things on one line makes for easy history scrollback.
In your version you will have 2 lines of history for the x = 0 term
and the while ... term. I don't want to have to press up twice,
especially when the code was in the distant past! Also cpaste might be
ok for scripting, but it looks too clumsy to use at the command line.

Cheers,
Phil

[toc] | [prev] | [next] | [standalone]


#3391 — Re: ipython: Multiple commands on the same line and newlines

FromAndrea Crotti <andrea.crotti.0@gmail.com>
Date2011-04-17 14:11 +0200
SubjectRe: ipython: Multiple commands on the same line and newlines
Message-ID<mailman.458.1303042271.9059.python-list@python.org>
In reply to#3336
Phil Winder <philipwinder@gmail.com> writes:

> Yes, that does not produce an error, but it does not "work". Please
> refer to my first post. Try the first code, you will get a syntax
> error. Placing things on one line makes for easy history scrollback.
> In your version you will have 2 lines of history for the x = 0 term
> and the while ... term. I don't want to have to press up twice,
> especially when the code was in the distant past! Also cpaste might be
> ok for scripting, but it looks too clumsy to use at the command line.
>
> Cheers,
> Phil

Well I guess that's the way it is with the interpreter..
But I don't see the sense in doing everything from there, just write the
code to a file and use %edit from ipython to change and run it, it's
quite nice and easy too.

[toc] | [prev] | [next] | [standalone]


#3421 — Re: ipython: Multiple commands on the same line and newlines

FromPhil Winder <philipwinder@gmail.com>
Date2011-04-17 11:40 -0700
SubjectRe: ipython: Multiple commands on the same line and newlines
Message-ID<b09e38ed-0fb7-4803-89be-51786b89d69d@o15g2000prn.googlegroups.com>
In reply to#3391
On Apr 17, 1:11 pm, Andrea Crotti <andrea.crott...@gmail.com> wrote:
> Phil Winder <philipwin...@gmail.com> writes:
> > Yes, that does not produce an error, but it does not "work". Please
> > refer to my first post. Try the first code, you will get a syntax
> > error. Placing things on one line makes for easy history scrollback.
> > In your version you will have 2 lines of history for the x = 0 term
> > and the while ... term. I don't want to have to press up twice,
> > especially when the code was in the distant past! Also cpaste might be
> > ok for scripting, but it looks too clumsy to use at the command line.
>
> > Cheers,
> > Phil
>
> Well I guess that's the way it is with the interpreter..
> But I don't see the sense in doing everything from there, just write the
> code to a file and use %edit from ipython to change and run it, it's
> quite nice and easy too.

Ok, thanks all. It's a little disappointing, but I guess that you
always have to work in a different way when you move to a new
language. Andrea's %edit method is probably the best compromise, but
this now means that I will have to learn all the (obscure) shortcuts
for vi!

Cheers,
Phil

[toc] | [prev] | [next] | [standalone]


#3423 — Re: ipython: Multiple commands on the same line and newlines

FromAlexander Kapps <alex.kapps@web.de>
Date2011-04-17 21:39 +0200
SubjectRe: ipython: Multiple commands on the same line and newlines
Message-ID<mailman.472.1303069187.9059.python-list@python.org>
In reply to#3421
On 17.04.2011 20:40, Phil Winder wrote:
> Ok, thanks all. It's a little disappointing, but I guess that you
> always have to work in a different way when you move to a new
> language. Andrea's %edit method is probably the best compromise, but
> this now means that I will have to learn all the (obscure) shortcuts
> for vi!

As you can read in "Python IDE/text-editor" thread. Learning either 
Vim or Emacs will pay off in the long run,

Anyway, IPython honors the $EDITOR environment variable. Just set it 
to whatever editor you prefer.

[toc] | [prev] | [next] | [standalone]


#3334

FromChris Angelico <rosuav@gmail.com>
Date2011-04-17 02:33 +1000
Message-ID<mailman.434.1302971588.9059.python-list@python.org>
In reply to#3323
On Sun, Apr 17, 2011 at 2:29 AM, Andrea Crotti
<andrea.crotti.0@gmail.com> wrote:
> for
> x = 10
>
> what's the difference for you if it gets evaluated before or after?

I have the same issue in IDLE sometimes, and the reason it's annoying
relates to the up-arrow key (Alt-P in IDLE). I can retrieve one entire
command, but if that command requires a "prefix statement" to set
things up (like initializing a dictionary to empty), that has to be
separate.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#3337

FromTerry Reedy <tjreedy@udel.edu>
Date2011-04-16 12:39 -0400
Message-ID<mailman.436.1302972019.9059.python-list@python.org>
In reply to#3323
On 4/16/2011 9:55 AM, Phil Winder wrote:
> Hi,
> I'm having a go at using ipython as a command prompt for data
> analysis. Coming from Matlab, I'm used to typing multiple commands on
> the same line then using the up arrow to go through my history.
> How can I write multiple python commands on the same line?

You can write multiple *simple* statements using ';'.

> E.g. "x = 0; while x<  10: x = x + 1;" returns an "invalid syntax"
> error on the 'e' in while.

All compound statements, like while, must start on own line.

> Also, how can I produce a new line, without it running the command?

Use an editor, as with IDLE, rather than a shell. Interactive mode runs 
*1* statement (including simple;simple) at a time.

> would have expected a ctrl-enter or shift-enter to produce the
> expected results.
> E.g. I want:
> "x = 0;<ctrl-enter>

This is one statement

> while x<  10:<ctrl-enter>
>      x = x + 1;<ctrl-enter>

This is another.

I can understanding wanting to rerun initialized loops with one enter, 
but you cannot. Sorry.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#3458

Fromharrismh777 <harrismh777@charter.net>
Date2011-04-18 01:01 -0500
Message-ID<VsQqp.9358$vC5.5966@newsfe01.iad>
In reply to#3337
Terry Reedy wrote:
> You can write multiple *simple* statements using ';'.

> All compound statements, like while, must start on own line.

>> E.g. I want:
>> "x = 0;<ctrl-enter>
>
> This is one statement

>> while x<  10:<ctrl-enter>
>>      x = x + 1;<ctrl-enter>


Lutz has a very nice write-up entitled "Why Indentation Syntax?"

Lutz, Mark, "Learning Python: Powerful Object Oriented Programming,"
     4th ed, (Sebastopol: O'Reilly, 2009), 266 -271.

     He makes the point clear that only simple statements may be chained 
together on a single line with  ;  and that compound statements (like 
while) "must still appear on lines of their own" (Lutz, 269).

     It might be nice (as an option) to be able to disengage the forced 
indentation syntax rules of Python. In other words, provide indentation 
syntax by default and allow an option via environment variable to engage 
an alternate (more C-like) blocking syntax.

     The forced indentation syntax is great for readability (and 
frankly, I like the appearance /low clutter) but it is inconvenient in 
some situations, like the one at the top of the thread.

     Just an idea (probably already been beaten to death long before my 
time)   :)

kind regards,
m harris

[toc] | [prev] | [next] | [standalone]


#3460

FromChris Angelico <rosuav@gmail.com>
Date2011-04-18 16:12 +1000
Message-ID<mailman.499.1303107124.9059.python-list@python.org>
In reply to#3458
On Mon, Apr 18, 2011 at 4:01 PM, harrismh777 <harrismh777@charter.net> wrote:
>    It might be nice (as an option) to be able to disengage the forced
> indentation syntax rules of Python. In other words, provide indentation
> syntax by default and allow an option via environment variable to engage an
> alternate (more C-like) blocking syntax.
>

You can do that with a future directive.

from __future__ import braces

That's two underscores before and after the word "future".
http://docs.python.org/reference/simple_stmts.html#future-statements

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#3369

From"OKB (not okblacke)" <brenNOSPAMbarn@NObrenSPAMbarn.net>
Date2011-04-17 04:22 +0000
Message-ID<Xns9EC9D9CD4E14OKB@81.169.183.62>
In reply to#3323
Phil Winder wrote:

> Hi,
> I'm having a go at using ipython as a command prompt for data
> analysis. Coming from Matlab, I'm used to typing multiple commands on
> the same line then using the up arrow to go through my history.
> How can I write multiple python commands on the same line?
> E.g. "x = 0; while x < 10: x = x + 1;" returns an "invalid syntax"
> error on the 'e' in while.
> 
> Also, how can I produce a new line, without it running the command? I
> would have expected a ctrl-enter or shift-enter to produce the
> expected results.
> E.g. I want:
> "x = 0; <ctrl-enter>
> while x < 10: <ctrl-enter>
>     x = x + 1; <ctrl-enter>
> " <enter to run>
> It seems to work automatically for the "while xxx:", but combinations
> of keys+enter do not work for "normal" lines.

    	You might want to take a look at DreamPie ( 
http://dreampie.sourceforge.net/ ), which provides the second option you 
indicate (and thus makesthe first unnecessary).  I've found it quite 
convenient for interactive use.


-- 
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is
no path, and leave a trail."
	--author unknown

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web