Ask Your Question
1

sage version of python's execfile

asked 12 years ago

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()?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 12 years ago

updated 12 years ago

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.

Preview: (hide)
link

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 ( 12 years ago )

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

John Palmieri gravatar imageJohn Palmieri ( 12 years ago )

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: 12 years ago

Seen: 834 times

Last updated: Mar 07 '13