Ask Your Question
2

preparser(False) doesn't work with attach()

asked 2014-11-05 08:57:54 +0100

Polonius gravatar image

updated 2014-11-05 14:23:21 +0100

kcrisman gravatar image

This is a more basic version of my previous question http://ask.sagemath.org/question/2469..., which had distracting details.

Background:

In William Stein's Sage for Power Users (http://wstein.org/books/sagebook/sage...), 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.

edit retag flag offensive close merge delete

Comments

I 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...

kcrisman gravatar imagekcrisman ( 2014-11-05 14:24:42 +0100 )edit

Thanks. How did you put the grey boxes around the 'code'?

Polonius gravatar imagePolonius ( 2014-11-06 10:52:46 +0100 )edit

When 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.

kcrisman gravatar imagekcrisman ( 2014-11-07 15:13:42 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2014-11-06 19:15:25 +0100

William Stein gravatar image

"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...):

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
edit flag offensive delete link more

Comments

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

Polonius gravatar imagePolonius ( 2014-11-06 19:51:01 +0100 )edit
3

answered 2014-11-05 16:16:33 +0100

tmonteil gravatar image

updated 2014-11-05 16:19:19 +0100

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

edit flag offensive delete link more

Comments

Thanks. 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.

Polonius gravatar imagePolonius ( 2014-11-06 10:02:46 +0100 )edit

OK. Thanks. Now if I start writing .py files which mostly consist of sage, I suppose I'll have lots more questions for this website.

Polonius gravatar imagePolonius ( 2014-11-06 19:54:13 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2014-11-05 08:57:54 +0100

Seen: 679 times

Last updated: Nov 06 '14