ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 07 Nov 2014 15:13:42 +0100preparser(False) doesn't work with attach()https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/ This is a more basic version of my previous question http://ask.sagemath.org/question/24696/preparsefalse-still-not-working/, which had distracting details.
Background:
In William Stein's Sage for Power Users (http://wstein.org/books/sagebook/sagebook.pdf), end of Section 2.2 and top of page 13, there is an example of how to turn the preparser off in the sage interpreter. The example works as claimed and here it is copied from my own computer:
sage: preparser(False)
sage: N=2/3 + 2^3
sage: N
1
sage: preparser(True)
sage: M=2/3 + 2^3
sage: M
26/3
But now suppose that I want to put (almost) exactly the same lines into a .sage file called 'preparsertest.sage', close sage, reopen it, and then type attach('preparsertest.sage') into the interpreter. Naively I expect the same behaviour but that's not what I get.
Here is the content of 'preparsertest.sage':
preparser(False)
N=2/3 + 2^3
preparser(True)
M=2/3+2^3
Here is the output from the interpreter:
sage: attach('preparsertest.sage')
sage: N
26/3
sage: M
26/3
So it seems that when I attach 'preparsertest.sage', the line preparser(False) is not being read.
What gives?
P.S. I tried to figure out how to attach a screen shot but it didn't work, and I didn't find instructions on ask.sagemath.org.
Wed, 05 Nov 2014 08:57:54 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/Comment by Polonius for <p>This is a more basic version of my previous question <a href="http://ask.sagemath.org/question/24696/preparsefalse-still-not-working/">http://ask.sagemath.org/question/2469...</a>, which had distracting details. </p>
<p>Background:</p>
<p>In William Stein's Sage for Power Users (<a href="http://wstein.org/books/sagebook/sagebook.pdf">http://wstein.org/books/sagebook/sage...</a>), end of Section 2.2 and top of page 13, there is an example of how to turn the preparser off in the sage interpreter. The example works as claimed and here it is copied from my own computer:</p>
<pre><code>sage: preparser(False)
sage: N=2/3 + 2^3
sage: N
1
sage: preparser(True)
sage: M=2/3 + 2^3
sage: M
26/3
</code></pre>
<p>But now suppose that I want to put (almost) exactly the same lines into a .sage file called 'preparsertest.sage', close sage, reopen it, and then type attach('preparsertest.sage') into the interpreter. Naively I expect the same behaviour but that's not what I get.</p>
<p>Here is the content of 'preparsertest.sage':</p>
<pre><code>preparser(False)
N=2/3 + 2^3
preparser(True)
M=2/3+2^3
</code></pre>
<p>Here is the output from the interpreter:</p>
<pre><code>sage: attach('preparsertest.sage')
sage: N
26/3
sage: M
26/3
</code></pre>
<p>So it seems that when I attach 'preparsertest.sage', the line preparser(False) is not being read. </p>
<p>What gives?</p>
<p>P.S. I tried to figure out how to attach a screen shot but it didn't work, and I didn't find instructions on <a href="http://ask.sagemath.org">ask.sagemath.org</a>.</p>
https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24797#post-id-24797Thanks. How did you put the grey boxes around the 'code'?Thu, 06 Nov 2014 10:52:46 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24797#post-id-24797Comment by kcrisman for <p>This is a more basic version of my previous question <a href="http://ask.sagemath.org/question/24696/preparsefalse-still-not-working/">http://ask.sagemath.org/question/2469...</a>, which had distracting details. </p>
<p>Background:</p>
<p>In William Stein's Sage for Power Users (<a href="http://wstein.org/books/sagebook/sagebook.pdf">http://wstein.org/books/sagebook/sage...</a>), end of Section 2.2 and top of page 13, there is an example of how to turn the preparser off in the sage interpreter. The example works as claimed and here it is copied from my own computer:</p>
<pre><code>sage: preparser(False)
sage: N=2/3 + 2^3
sage: N
1
sage: preparser(True)
sage: M=2/3 + 2^3
sage: M
26/3
</code></pre>
<p>But now suppose that I want to put (almost) exactly the same lines into a .sage file called 'preparsertest.sage', close sage, reopen it, and then type attach('preparsertest.sage') into the interpreter. Naively I expect the same behaviour but that's not what I get.</p>
<p>Here is the content of 'preparsertest.sage':</p>
<pre><code>preparser(False)
N=2/3 + 2^3
preparser(True)
M=2/3+2^3
</code></pre>
<p>Here is the output from the interpreter:</p>
<pre><code>sage: attach('preparsertest.sage')
sage: N
26/3
sage: M
26/3
</code></pre>
<p>So it seems that when I attach 'preparsertest.sage', the line preparser(False) is not being read. </p>
<p>What gives?</p>
<p>P.S. I tried to figure out how to attach a screen shot but it didn't work, and I didn't find instructions on <a href="http://ask.sagemath.org">ask.sagemath.org</a>.</p>
https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24793#post-id-24793I think you may have to have some minimum "karma" in order to attach screenshots. There is a little icon at the top of the question area next to the bold, italic, etc. which looks like a computer screen; probably it's disabled until a certain point. Sadly, this is a necessary thing given the amount of spam we get...Wed, 05 Nov 2014 14:24:42 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24793#post-id-24793Comment by kcrisman for <p>This is a more basic version of my previous question <a href="http://ask.sagemath.org/question/24696/preparsefalse-still-not-working/">http://ask.sagemath.org/question/2469...</a>, which had distracting details. </p>
<p>Background:</p>
<p>In William Stein's Sage for Power Users (<a href="http://wstein.org/books/sagebook/sagebook.pdf">http://wstein.org/books/sagebook/sage...</a>), end of Section 2.2 and top of page 13, there is an example of how to turn the preparser off in the sage interpreter. The example works as claimed and here it is copied from my own computer:</p>
<pre><code>sage: preparser(False)
sage: N=2/3 + 2^3
sage: N
1
sage: preparser(True)
sage: M=2/3 + 2^3
sage: M
26/3
</code></pre>
<p>But now suppose that I want to put (almost) exactly the same lines into a .sage file called 'preparsertest.sage', close sage, reopen it, and then type attach('preparsertest.sage') into the interpreter. Naively I expect the same behaviour but that's not what I get.</p>
<p>Here is the content of 'preparsertest.sage':</p>
<pre><code>preparser(False)
N=2/3 + 2^3
preparser(True)
M=2/3+2^3
</code></pre>
<p>Here is the output from the interpreter:</p>
<pre><code>sage: attach('preparsertest.sage')
sage: N
26/3
sage: M
26/3
</code></pre>
<p>So it seems that when I attach 'preparsertest.sage', the line preparser(False) is not being read. </p>
<p>What gives?</p>
<p>P.S. I tried to figure out how to attach a screen shot but it didn't work, and I didn't find instructions on <a href="http://ask.sagemath.org">ask.sagemath.org</a>.</p>
https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24808#post-id-24808When you are typing, you should see some icons (just like the ones I mentioned) - and another one of those looks like 101010 or something. Highlight the code, and then click that. It will indent each line 4 spaces (you can also do this by hand, of course) and that is what is required to tell askbot this is code.Fri, 07 Nov 2014 15:13:42 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24808#post-id-24808Answer by William Stein for <p>This is a more basic version of my previous question <a href="http://ask.sagemath.org/question/24696/preparsefalse-still-not-working/">http://ask.sagemath.org/question/2469...</a>, which had distracting details. </p>
<p>Background:</p>
<p>In William Stein's Sage for Power Users (<a href="http://wstein.org/books/sagebook/sagebook.pdf">http://wstein.org/books/sagebook/sage...</a>), end of Section 2.2 and top of page 13, there is an example of how to turn the preparser off in the sage interpreter. The example works as claimed and here it is copied from my own computer:</p>
<pre><code>sage: preparser(False)
sage: N=2/3 + 2^3
sage: N
1
sage: preparser(True)
sage: M=2/3 + 2^3
sage: M
26/3
</code></pre>
<p>But now suppose that I want to put (almost) exactly the same lines into a .sage file called 'preparsertest.sage', close sage, reopen it, and then type attach('preparsertest.sage') into the interpreter. Naively I expect the same behaviour but that's not what I get.</p>
<p>Here is the content of 'preparsertest.sage':</p>
<pre><code>preparser(False)
N=2/3 + 2^3
preparser(True)
M=2/3+2^3
</code></pre>
<p>Here is the output from the interpreter:</p>
<pre><code>sage: attach('preparsertest.sage')
sage: N
26/3
sage: M
26/3
</code></pre>
<p>So it seems that when I attach 'preparsertest.sage', the line preparser(False) is not being read. </p>
<p>What gives?</p>
<p>P.S. I tried to figure out how to attach a screen shot but it didn't work, and I didn't find instructions on <a href="http://ask.sagemath.org">ask.sagemath.org</a>.</p>
https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?answer=24800#post-id-24800 "I thought that the following are equivalent: entering a bunch of lines into the sage interpreter, hitting return after each one, or putting those same lines into a .sage file and then attaching that .sage file in the interpreter." They are not equivalent. When you load a .sage file, Sage reads the file into memory, runs the preparser on it, then calls the execfile command on the resulting string. It thus runs the entire contents of the file in one single call to execfile.
More precisely, here's the actual source code in the sage library, from src/sage/misc/preparser.py (https://github.com/sagemath/sage/blob/master/src/sage/misc/preparser.py):
elif fpath.endswith('.sage'):
from sage.misc.attached_files import load_attach_mode
load_debug_mode, attach_debug_mode = load_attach_mode()
if (attach and attach_debug_mode) or ((not attach) and load_debug_mode):
# Preparse to a file to enable tracebacks with
# code snippets. Use preparse_file_named to make
# the file name appear in the traceback as well.
# See Trac 11812.
exec_file_is(fpath)
execfile(preparse_file_named(fpath), globals)
else:
# Preparse in memory only for speed.
exec_file_is(fpath)
exec preparse_file(open(fpath).read()) + "\n" in globals
Thu, 06 Nov 2014 19:15:25 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?answer=24800#post-id-24800Comment by Polonius for <p>"I thought that the following are equivalent: entering a bunch of lines into the sage interpreter, hitting return after each one, or putting those same lines into a .sage file and then attaching that .sage file in the interpreter." They are not equivalent. When you load a .sage file, Sage reads the file into memory, runs the preparser on it, then calls the execfile command on the resulting string. It thus runs the entire contents of the file in one single call to execfile. </p>
<p>More precisely, here's the actual source code in the sage library, from src/sage/misc/preparser.py (<a href="https://github.com/sagemath/sage/blob/master/src/sage/misc/preparser.py">https://github.com/sagemath/sage/blob...</a>):</p>
<pre><code>elif fpath.endswith('.sage'):
from sage.misc.attached_files import load_attach_mode
load_debug_mode, attach_debug_mode = load_attach_mode()
if (attach and attach_debug_mode) or ((not attach) and load_debug_mode):
# Preparse to a file to enable tracebacks with
# code snippets. Use preparse_file_named to make
# the file name appear in the traceback as well.
# See Trac 11812.
exec_file_is(fpath)
execfile(preparse_file_named(fpath), globals)
else:
# Preparse in memory only for speed.
exec_file_is(fpath)
exec preparse_file(open(fpath).read()) + "\n" in globals
</code></pre>
https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24801#post-id-24801OK. Thank you for the clarification. It seems the ultimate answer to my question is 'don't write .sage files if you want to use some pure python along the way'.Thu, 06 Nov 2014 19:51:01 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24801#post-id-24801Answer by tmonteil for <p>This is a more basic version of my previous question <a href="http://ask.sagemath.org/question/24696/preparsefalse-still-not-working/">http://ask.sagemath.org/question/2469...</a>, which had distracting details. </p>
<p>Background:</p>
<p>In William Stein's Sage for Power Users (<a href="http://wstein.org/books/sagebook/sagebook.pdf">http://wstein.org/books/sagebook/sage...</a>), end of Section 2.2 and top of page 13, there is an example of how to turn the preparser off in the sage interpreter. The example works as claimed and here it is copied from my own computer:</p>
<pre><code>sage: preparser(False)
sage: N=2/3 + 2^3
sage: N
1
sage: preparser(True)
sage: M=2/3 + 2^3
sage: M
26/3
</code></pre>
<p>But now suppose that I want to put (almost) exactly the same lines into a .sage file called 'preparsertest.sage', close sage, reopen it, and then type attach('preparsertest.sage') into the interpreter. Naively I expect the same behaviour but that's not what I get.</p>
<p>Here is the content of 'preparsertest.sage':</p>
<pre><code>preparser(False)
N=2/3 + 2^3
preparser(True)
M=2/3+2^3
</code></pre>
<p>Here is the output from the interpreter:</p>
<pre><code>sage: attach('preparsertest.sage')
sage: N
26/3
sage: M
26/3
</code></pre>
<p>So it seems that when I attach 'preparsertest.sage', the line preparser(False) is not being read. </p>
<p>What gives?</p>
<p>P.S. I tried to figure out how to attach a screen shot but it didn't work, and I didn't find instructions on <a href="http://ask.sagemath.org">ask.sagemath.org</a>.</p>
https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?answer=24794#post-id-24794This is perhaps not a satisfactory answer: when using attach, the fact that the file is preparsed or not depends on its name. If it ends with ``.sage`` then it will be preparsed, if it ends with ``.py`` then it will not be preparsed. It seems the preparsing is done before executing the files, so the ``preparser`` commands you write inside the files are useless (the preparsing was already done when those lines are executed).
So, if you want to use both preparsed and unpreparsed things within files, you should use a mix of files, some files ending with ``.py`` and some files ending with ``.sage``.
Note that the preparsing is mainly a trick to make the interactive mode easier. If you aim at maintaining a set of files on the long term, it is advised to use only ``.py`` files, which are true Python. Note also that Sage source code is made of ``.py`` files and do not use any kind of preparsing, so this advice may be useful if you plan to let your code eventually enter Sage source code. If you want to migrate old ``.sage`` files into ``.py`` files, you can use the ``preparse`` function to help in the translation.
Wed, 05 Nov 2014 16:16:33 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?answer=24794#post-id-24794Comment by Polonius for <p>This is perhaps not a satisfactory answer: when using attach, the fact that the file is preparsed or not depends on its name. If it ends with <code>.sage</code> then it will be preparsed, if it ends with <code>.py</code> then it will not be preparsed. It seems the preparsing is done before executing the files, so the <code>preparser</code> commands you write inside the files are useless (the preparsing was already done when those lines are executed).</p>
<p>So, if you want to use both preparsed and unpreparsed things within files, you should use a mix of files, some files ending with <code>.py</code> and some files ending with <code>.sage</code>.</p>
<p>Note that the preparsing is mainly a trick to make the interactive mode easier. If you aim at maintaining a set of files on the long term, it is advised to use only <code>.py</code> files, which are true Python. Note also that Sage source code is made of <code>.py</code> files and do not use any kind of preparsing, so this advice may be useful if you plan to let your code eventually enter Sage source code. If you want to migrate old <code>.sage</code> files into <code>.py</code> files, you can use the <code>preparse</code> function to help in the translation.</p>
https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24802#post-id-24802OK. Thanks. Now if I start writing .py files which mostly consist of sage, I suppose I'll have lots more questions for this website.Thu, 06 Nov 2014 19:54:13 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24802#post-id-24802Comment by Polonius for <p>This is perhaps not a satisfactory answer: when using attach, the fact that the file is preparsed or not depends on its name. If it ends with <code>.sage</code> then it will be preparsed, if it ends with <code>.py</code> then it will not be preparsed. It seems the preparsing is done before executing the files, so the <code>preparser</code> commands you write inside the files are useless (the preparsing was already done when those lines are executed).</p>
<p>So, if you want to use both preparsed and unpreparsed things within files, you should use a mix of files, some files ending with <code>.py</code> and some files ending with <code>.sage</code>.</p>
<p>Note that the preparsing is mainly a trick to make the interactive mode easier. If you aim at maintaining a set of files on the long term, it is advised to use only <code>.py</code> files, which are true Python. Note also that Sage source code is made of <code>.py</code> files and do not use any kind of preparsing, so this advice may be useful if you plan to let your code eventually enter Sage source code. If you want to migrate old <code>.sage</code> files into <code>.py</code> files, you can use the <code>preparse</code> function to help in the translation.</p>
https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24796#post-id-24796Thanks. Then there is something I really don't understand. I thought that the following are equivalent: entering a bunch of lines into the sage interpreter, hitting return after each one, or putting those same lines into a .sage file and then attaching that .sage file in the interpreter. Doesn't attach read the lines one by one?
Anyhow, about .sage versus .py. I've been doing that. I write some .sage file that produces a bunch of matrices, makes them into numpy arrays, and the saves those numpy arrays into a file. Then I write another .py file which loads those numpy arrays and applies some python modules to them. But this is rather awkward and (at least the first time) time consuming.
Maybe it is the situation not your answer that is unsatisfactory.Thu, 06 Nov 2014 10:02:46 +0100https://ask.sagemath.org/question/24790/preparserfalse-doesnt-work-with-attach/?comment=24796#post-id-24796