Ask Your Question
1

sage version of python's execfile

asked 2013-03-07 20:24:57 +0100

Benjamin Young gravatar image

What is the sage analogue of python's "execfile()"? How do I ask sage to preprocess and execute a text file full of sage commands?

Up till now I've just been using execfile() on the sage command line prompt, because that's what I do in python sometimes. However I just learned the hard way that this is the wrong thing to do; it misses a vital preprocessing step. For example, if I ask for the type of 1/2 on the sage command line, the following happens:

sage: print type(1/2)
<type 'sage.rings.rational.Rational'>

Great! that's what I want. However if I put the following into the file "uhoh.py":

print type(1/2)

and then I issue the following command on the sage command line:

execfile("uhoh.py")

then I get the output

<type 'int'>

Yes, the integer 1/2. I want the last hour of debugging time back.

Of course what's happening is that the text file is being interpreted in pure python, in which 1/2 really does represent an integer division. What is the right thing to do here, rather than execfile()?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-03-07 21:34:05 +0100

updated 2013-03-07 21:36:00 +0100

You can do load("file.sage") or attach("file.sage"). Make sure to use a ".sage" suffix, so Sage will know to preparse it. See the tutorial for a bit more information.

edit flag offensive delete link more

Comments

Thank you! I'm embarrassed I didn't know that, despite it being in the tutorial... I guess I never worked through it, since I already knew python. Oh, well.

Benjamin Young gravatar imageBenjamin Young ( 2013-03-07 22:56:16 +0100 )edit

Well, it is in one of the very last sections of the tutorial...

John Palmieri gravatar imageJohn Palmieri ( 2013-03-08 10:50:00 +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

Stats

Asked: 2013-03-07 20:24:57 +0100

Seen: 757 times

Last updated: Mar 07 '13