ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 21 Mar 2011 09:32:03 +0100Writing re-usable sage scriptshttps://ask.sagemath.org/question/8005/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?
ThanksThu, 17 Mar 2011 11:00:10 +0100https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/Comment by kcrisman for <p>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...).</p>
<p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?comment=21974#post-id-21974Not 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.Thu, 17 Mar 2011 11:35:17 +0100https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?comment=21974#post-id-21974Answer by NevilleS for <p>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...).</p>
<p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?answer=12209#post-id-12209I'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.Mon, 21 Mar 2011 09:32:03 +0100https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?answer=12209#post-id-12209Answer by Mike Witt for <p>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...).</p>
<p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?answer=12191#post-id-12191This 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:
<pre>
CODE = '/home/mike/Projects/Sage/'
attach CODE+'defaults.sage'
</pre>
Everything that I *always* want is in defaults.sage. Then in a particular sage
program, I do something like:
<pre>
attach CODE+'Differential_Equations/series.sage'
attach CODE+'Differential_Equations/pde_misc.sage'
attach /home/mike/Projects/Science/Electrodynamics/Electrostatics.sage
</pre>
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.Thu, 17 Mar 2011 14:39:30 +0100https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?answer=12191#post-id-12191Comment by NevilleS for <p>This may not be the "right way" to do it, but here is what I do:</p>
<p>In my home directory, in the .sage subdirectory I have the following "init.sage" file:</p>
<pre>CODE = '/home/mike/Projects/Sage/'
attach CODE+'defaults.sage'
</pre>
<p>Everything that I <em>always</em> want is in defaults.sage. Then in a particular sage
program, I do something like:</p>
<pre>attach CODE+'Differential_Equations/series.sage'
attach CODE+'Differential_Equations/pde_misc.sage'
attach /home/mike/Projects/Science/Electrodynamics/Electrostatics.sage
</pre>
<p>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.</p>
<p>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.</p>
https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?comment=21973#post-id-21973Well, that's certainly better than what I had thus far. Thanks for this.Thu, 17 Mar 2011 15:52:20 +0100https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?comment=21973#post-id-21973Comment by Felix Lawrence for <p>This may not be the "right way" to do it, but here is what I do:</p>
<p>In my home directory, in the .sage subdirectory I have the following "init.sage" file:</p>
<pre>CODE = '/home/mike/Projects/Sage/'
attach CODE+'defaults.sage'
</pre>
<p>Everything that I <em>always</em> want is in defaults.sage. Then in a particular sage
program, I do something like:</p>
<pre>attach CODE+'Differential_Equations/series.sage'
attach CODE+'Differential_Equations/pde_misc.sage'
attach /home/mike/Projects/Science/Electrodynamics/Electrostatics.sage
</pre>
<p>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.</p>
<p>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.</p>
https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?comment=21972#post-id-21972Since Sage 4.6.1, you can do `load_attach_path('/home/mike/Projects/Sage/')` and then `attach 'defaults.sage'` etc. should just work.Thu, 17 Mar 2011 20:01:33 +0100https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?comment=21972#post-id-21972Answer by niles for <p>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...).</p>
<p>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?</p>
<p>Thanks</p>
https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?answer=11697#post-id-11697See also the question on [organizing a multifile project](http://ask.sagemath.org/question/418/how-to-organize-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.Thu, 17 Mar 2011 14:54:17 +0100https://ask.sagemath.org/question/8005/writing-re-usable-sage-scripts/?answer=11697#post-id-11697