Indented code in terminal

75 views
Skip to first unread message

Fredrik Johansson

unread,
Apr 19, 2011, 8:40:26 AM4/19/11
to sage-...@googlegroups.com
Hi all,

Maybe it's just me, but I find it terribly annoying that one cannot
paste code fragments containing indentation in a Sage terminal
session, as one can with the ordinary Python interpreter. Simple
example:

def f(x):
if x == 1:
return 2
return 1

>>> def f(x):
... if x == 1:
... return 2
... return 1
...

sage: def f(x):
....: if x == 1:
....: return 2
....: return 1
------------------------------------------------------------
File "<ipython console>", line 4
rn Integer(1)
^
SyntaxError: invalid syntax

Is this an IPython bug (and in that case can it be fixed or worked
around), or is it a preparser issue?

Fredrik

Jeroen Demeyer

unread,
Apr 19, 2011, 8:50:43 AM4/19/11
to sage-...@googlegroups.com
On 2011-04-19 14:40, Fredrik Johansson wrote:
> Hi all,
>
> Maybe it's just me, but I find it terribly annoying that one cannot
> paste code fragments containing indentation in a Sage terminal
> session, as one can with the ordinary Python interpreter. Simple
> example:
>
> def f(x):
> if x == 1:
> return 2
> return 1

I agree it is annoying, but not sure whether it can be considered a "bug".

Kiran Kedlaya

unread,
Apr 19, 2011, 10:07:49 AM4/19/11
to sage-devel
I just tried this in sage -ipython (to get directly to the ipython
interpreter) and got a similar error, so I'd call this an ipython
issue. And +1 for it being a bug.

Kiran

pipedream

unread,
Apr 19, 2011, 10:18:11 AM4/19/11
to sage-devel
sage: %cpaste
Pasting code; enter '--' alone on the line to stop.
:def f(x):
: if x == 1:
: return 2
: return 1
:--
sage: f(1)
2
sage: cpaste?
String Form: <bound method InteractiveShell.magic_cpaste of
<IPython.iplib.InteractiveShell object at 0x20c5f10>>
Namespace: IPython internal
File: /usr/local/src/sage-4.6.2/local/lib/python2.6/site-
packages/IPython/Magic.py
Definition: cpaste(self, parameter_s='')
Docstring:
Allows you to paste & execute a pre-formatted code block from
clipboard.

You must terminate the block with '--' (two minus-signs) alone
on
the line. You can also provide your own sentinel with '%paste -
s
%%' ('%%' is the new sentinel for this operation)

The block is dedented prior to execution to enable execution of
method definitions. '>' and '+' characters at the beginning of
a
line are ignored, to allow pasting directly from e-mails, diff
files and doctests (the '...' continuation prompt is also
stripped). The executed block is also assigned to variable
named
'pasted_block' for later editing with '%edit pasted_block'.

You can also pass a variable name as an argument, e.g. '%cpaste
foo'. This assigns the pasted block to variable 'foo' as
string,
without dedenting or executing it (preceding >>> and + is still
stripped)

Do not be alarmed by garbled output on Windows (it's a readline
bug). Just press enter and type -- (and press enter again) and
the
block will be what was just pasted.

IPython statements (magics, shell escapes) are not supported
(yet).

Marco Streng

unread,
Apr 19, 2011, 10:21:59 AM4/19/11
to sage-devel


On Apr 19, 3:18 pm, pipedream <jan.groenew...@gmail.com> wrote:
> sage: %cpaste
> Pasting code; enter '--' alone on the line to stop.
> :def f(x):
> :    if x == 1:
> :        return 2
> :    return 1
> :--
> sage: f(1)
> 2

Thanks!

John H Palmieri

unread,
Apr 19, 2011, 10:47:15 AM4/19/11
to sage-...@googlegroups.com

This is not a direct answer, but I would suggest setting the environment variable EDITOR to be something you like (like "/usr/bin/emacs") and then do this:

sage: ed

This will pop open an editor.  Then you can type in the function definition using all of the features of your editor:

def my_great_function(x):
    if x==1:

(etc.)  Once you do this, from the "sage:" prompt, you can type "ed my_great_function" to modify the definition.

--
John

John Cremona

unread,
Apr 19, 2011, 11:57:34 AM4/19/11
to sage-...@googlegroups.com
John, that is an absolutely brilliant feature I had no idea existed.
It should be better known -- thanks!

John

On Tue, Apr 19, 2011 at 3:47 PM, John H Palmieri <jhpalm...@gmail.com> wrote:


>
> This is not a direct answer, but I would suggest setting the environment
> variable EDITOR to be something you like (like "/usr/bin/emacs") and then do
> this:
>
> sage: ed
>
> This will pop open an editor.  Then you can type in the function definition
> using all of the features of your editor:
>
> def my_great_function(x):
>     if x==1:
>
> (etc.)  Once you do this, from the "sage:" prompt, you can type "ed
> my_great_function" to modify the definition.
>
> --
> John
>

> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to
> sage-devel+...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>

John H Palmieri

unread,
Apr 19, 2011, 3:06:21 PM4/19/11
to sage-...@googlegroups.com


On Tuesday, April 19, 2011 8:57:34 AM UTC-7, John Cremona wrote:
John, that is an absolutely brilliant feature I had no idea existed.
It should be better known -- thanks!

I discovered it completely by accident.  See <http://trac.sagemath.org/sage_trac/ticket/11219> for some advertising for it.

--
John

kcrisman

unread,
Apr 19, 2011, 3:34:46 PM4/19/11
to sage-devel


On Apr 19, 11:57 am, John Cremona <john.crem...@gmail.com> wrote:
> John, that is an absolutely brilliant feature I had no idea existed.
> It should be better known -- thanks!
>
> John
>
> On Tue, Apr 19, 2011 at 3:47 PM, John H Palmieri <jhpalmier...@gmail.com> wrote:
>
>
>
>
>
> > This is not a direct answer, but I would suggest setting the environment
> > variable EDITOR to be something you like (like "/usr/bin/emacs") and then do
> > this:
>
> > sage: ed
>
> > This will pop open an editor.  Then you can type in the function definition
> > using all of the features of your editor:
>
> > def my_great_function(x):
> >     if x==1:
>
> > (etc.)  Once you do this, from the "sage:" prompt, you can type "ed
> > my_great_function" to modify the definition.
>

One problem, these things don't seem to be preparsed.


----------------------------------------------------------------------
| Sage Version 4.6.2, Release Date: 2011-02-25 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: %bg factor(next_prime(10^30)*next_prime(10^40))
Starting job # 0 in a separate thread.
sage: jobs.status()
Completed jobs:
0 : factor(next_prime(10^30)*next_prime(10^40))

sage: jobs[0].result
23 * 37

because


Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 10^30
20
>>> 10^40
34
>>>

That should probably be mentioned!

John Cremona

unread,
Apr 19, 2011, 4:04:23 PM4/19/11
to sage-...@googlegroups.com
I have given #11219 a positive review, but also note on that ticket
that logstart/load do not work as advertised in that section of the
manual.

John

Rob Beezer

unread,
Apr 20, 2011, 2:06:24 AM4/20/11
to sage-devel


On Apr 19, 5:40 am, Fredrik Johansson <fredrik.johans...@gmail.com>
wrote:
> Maybe it's just me, but I find it terribly annoying that one cannot
> paste code fragments containing indentation in a Sage terminal
> session, as one can with the ordinary Python interpreter. Simple
> example:

FWIW, it is possible to cut/paste indented code fragments from
doctests into a terminal. Admittedly, an infrequent situation.

In a doctest, indented lines lead with three periods. These seem to
be recognized in a terminal. For example,

def f(x):
... if x == 1:
... return 2
... return 1

I frequently drag 'n' drop code hunks like this out of docstrings and
into a terminal. Then drag 'n' drop the results back into the
docstring I've found this style of writing doctests marginally more
productive than leaving out output and then cutting it out from a
trial test run.

Rob

Maarten Derickx

unread,
Apr 20, 2011, 4:17:44 AM4/20/11
to sage-devel

Maarten Derickx

unread,
Apr 21, 2011, 6:37:39 PM4/21/11
to sage-devel
I also created http://trac.sagemath.org/sage_trac/ticket/11235
This makes the IPython edit magic command automatically open the right
file (i.e. the one in SAGE_ROOT/devel) when doing for example
%edit factor

This one is already ready for review.

On Apr 20, 10:17 am, Maarten Derickx <m.derickx.stud...@gmail.com>
wrote:

Kwankyu Lee

unread,
Apr 27, 2011, 9:41:25 PM4/27/11
to sage-...@googlegroups.com
Hi Johin,

You may be interested in the ticket:

http://trac.sagemath.org/sage_trac/ticket/10289

Would you kindly review it? It's long been in "needs review" state.


Kwankyu
Reply all
Reply to author
Forward
0 new messages