# Writing re-usable sage scripts

 6 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 asked Mar 17 '11 NevilleS 121 ● 2 ● 3 ● 7 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. kcrisman (Mar 17 '11)

 6 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. posted Mar 17 '11 Mike Witt 455 ● 4 ● 11 ● 26 Well, that's certainly better than what I had thus far. Thanks for this. NevilleS (Mar 17 '11)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. Felix Lawrence (Mar 17 '11)
 6 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. posted Mar 21 '11 NevilleS 121 ● 2 ● 3 ● 7
 2 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. posted Mar 17 '11 niles 3354 ● 5 ● 38 ● 92 http://nilesjohnson.net/

[hide preview]