# Writing re-usable sage scripts

I'm looking to write some reusable code to do things like parse a particular file format outputted by some of my tools, but I don't see a particularly obvious "right way" to do this. I can define a python module, add it to the python path using the init.sage file, and then import it, but then I don't get some of the nice features I would get if I just loaded a .sage file from my current directory (namely tab-complete...).

Is there a preferred directory where I can save .sage files for use with the "load" commmand? If so, is there a way to specify a directory that is not in the SAGE_ROOT area, so I can source-control my specific .sage files independent of the sage installation?

Thanks

edit retag close merge delete

1

Not sure about your question, but I would use 'attach' instead of load, all things being equal. It makes it really easy for me to make quick changes to the code of my module and not have to reload.

( 2011-03-17 05:35:17 -0600 )edit

Sort by » oldest newest most voted

I'm going to repost Felix's comment as my chosen answer, since it could be missed in the comments there:

Since Sage 4.6.1, you can do load_attach_path('/home/mike/Projects/Sage/') and then attach 'defaults.sage' etc. should just work.

This works reasonably well for me since I can add my "library" path to the sage path and define sage files there.

more

This may not be the "right way" to do it, but here is what I do:

In my home directory, in the .sage subdirectory I have the following "init.sage" file:

CODE = '/home/mike/Projects/Sage/'
attach CODE+'defaults.sage'


Everything that I always want is in defaults.sage. Then in a particular sage program, I do something like:

attach CODE+'Differential_Equations/series.sage'
attach CODE+'Differential_Equations/pde_misc.sage'
attach /home/mike/Projects/Science/Electrodynamics/Electrostatics.sage


Note that while a couple of the things I needed were in my default "CODE" place, one of them was just in a random directory, so I had to include the whole path.

This certainly isn't perfect, but it's the best thing that I have found to do. I would also be interested in hearing other ideas. Figuring out the best was to "organize" my sage code has been an ongoing issue for me. I have never found a reasonable way to do it for sage notebooks.

more

Well, that's certainly better than what I had thus far. Thanks for this.

( 2011-03-17 09:52:20 -0600 )edit
1

Since Sage 4.6.1, you can do load_attach_path('/home/mike/Projects/Sage/') and then attach 'defaults.sage' etc. should just work.

( 2011-03-17 14:01:33 -0600 )edit

See also the question on organizing a multifile project for some ideas similar to those of @mike-witt. Unfortunately the conclusion there too was that there isn't really a good existing framework for things like this.

more