Preparser and continuation

49 views
Skip to first unread message

Rob Beezer

unread,
Jul 22, 2011, 6:32:03 PM7/22/11
to sage-devel
I'd like to spread the definition of a large multivariate symbolic
function across several input lines (because it will eventually end up
on a piece of paper - how quaint). Here is a simple example you can
drop on a command line, or put into a two-line cell in the notebook
without the triple dots, but physically on two lines.

T(r, t) = [r^2,
... t^2]

Preparser cannot handle the continuation:

File "<ipython console>", line 1
__tmp__=var("r,t"); T =
symbolic_expression([r**Integer(2),).function(r,t)

Workaround:

outputs = [r^2, t^2]
T(r, t) = outputs

But if you start with this, the symbolic variables are not declared
when you form the list - you lose some preparser magic. So it needs
to become:

r, t = var('r, t')
outputs = [r^2,
... t^2]
T(r, t) = outputs

Not intolerable for a huge example, but it would be nice to just have
a one-statement, multiple-continuation version possible.

Any better workarounds? Any hope the preparser could be extended to
handle this sort of thing?

Rob

Robert Bradshaw

unread,
Jul 22, 2011, 6:59:22 PM7/22/11
to sage-...@googlegroups.com

It probably could, we'd just have to have a notion of a non-breaking
linebreak (e.g. inside parens). It is already set up to handle the
fact that it gets lines one at a time from iPython.

- Robert

Maarten Derickx

unread,
Jul 23, 2011, 6:07:41 AM7/23/11
to sage-...@googlegroups.com
+1 to robert, the preparser should just behave like the python standard here.

In regular python you can make your code multiline if the parser is expecting a closing ), } or ]
If you don't have an unmatched [,{ or ( then you can also make it multiline by adding a \

Note that this behaviour also fails currently:

T(r,t)=[r^2, \
    t^2]

gives

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_14.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("VChyLHQpPVtyXjIsIFwKICAgIHReMl0="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/private/var/folders/F+/F+xRBxXCG8mKGpPo2zG+i++++TI/-Tmp-/tmph7BaW0/___code___.py", line 3
    __tmp__=var("r,t"); T = symbolic_expression([r**_sage_const_2 ,  * BackslashOperator() * ).function(r,t)
                                                                     ^
SyntaxError: invalid syntax

leif

unread,
Jul 23, 2011, 8:06:39 AM7/23/11
to sage-devel
*Slightly* related: ;-)

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

("Doctest framework fails to parse multiline input pasted from sage
interactive prompt")

Hopefully Kini or me will return to and finish that...


-leif

Rob Beezer

unread,
Jul 24, 2011, 2:54:12 PM7/24/11
to sage-devel
Thanks for the discussion. I've made a ticket:

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

which I am unlikely to pursue much further. I put it into the "user
interface" component - there may be a better choice.

Yes, I saw #10458 while investigating this, and would really like to
see that happen. I have been doing a lot of cut/paste between command-
line sessions and SageTeX'ed documents and it would avoid a lot of
tedious editing if both "..." and "....:" were supported in doctests,
or if a Sage command-line session replaced "....:" with "..." in its
output, or something else. SageTeX and my own latex->sws conversion
script could be easily adapted to support whatever changes are made.
This is one of those mildly annoying inconsistentcies between Sage,
iPython, command-line, notebook, etc that it would be nice to
rationalize.

Rob
Reply all
Reply to author
Forward
0 new messages