ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 03 May 2012 22:02:37 -0500How do I share a library of Sage functionshttp://ask.sagemath.org/question/7501/how-do-i-share-a-library-of-sage-functions/My question concerns what other people usually do in this -- seemingly common -- scenario, which I have needed to think on quite a lot to accomplish. I would like to know whether my way of handling this is the envisioned one. Sorry for the question being long ;)
I am using the notebook for my day-to-day math exploration, but in order to share code between worksheets (and for overview of the individual worksheets), I place often-used code in .sage-files on a folder which I have added to the SAGE_PATH. I have a make-script to precompile these into .py-files which are then loaded at the top of each worksheet using
#auto
from sagelib import *
which works by creating an all.py file in the directory. The individual .sage files of course also need each other (in a non-cyclic way, naturally), so they also have lines like "from util import *" in the top.
The reason I'm using .sage files and not programming directly in .py should be obvious: I like the sage short-hands. Also, if I did that, I could not even use these short-hands inside the notebook, as I would have to refactor the code if I later wanted to move it to the library (which happens all the time).
The reason I'm then using Python's "import" and not Sage's "load" is for two reasons: One is the module system, so I can avoid stating every single library file in the top of each of my worksheets. The other is for efficiency: Let's say I have two files A.sage and B.sage, and B.sage uses A. If I load all first A and then B, the loading of B would cause A to be reloaded again. By this process, A can be reloaded an exponential number of times. I hope I am wrong, but it seems that this is not really handled in Sage's load. Lastly, I also like being able to use the module system once in a while, to not pollute the namespace overly.
Ok, so my Python recompiling-system works on my local machine, but then I would sometimes like to share code or run on some remote server. I can then put my .py or .sage files on a public server, like bitbucket, and in the top of the remote server's notebook worksheet, I wish to load this url. However, having used the Python module system as I mentioned above, I again cannot use Sage's "load" command, as B would have as it's first line "from A import *", but using Sage's load, module A does not exist. This can then be circumvented using code for dynamically loading modules and more manually (calling sage.misc.remote_file) retrieving the file and making sure it's imported properly (possibly precompiling it if it's a .sage file).
As I mentioned to begin with, this seems to actually work and be somewhat manageable. But the custom code I had to write for this makes me think this is not what other people do. But surely, every Sage user must amass a lot of library/commonly used code over time which needs to be loaded into worksheets? How do you handle this?Thu, 03 May 2012 00:24:47 -0500http://ask.sagemath.org/question/7501/how-do-i-share-a-library-of-sage-functions/Answer by niles for <div class="snippet"><p>My question concerns what other people usually do in this -- seemingly common -- scenario, which I have needed to think on quite a lot to accomplish. I would like to know whether my way of handling this is the envisioned one. Sorry for the question being long ;)</p>
<p>I am using the notebook for my day-to-day math exploration, but in order to share code between worksheets (and for overview of the individual worksheets), I place often-used code in .sage-files on a folder which I have added to the SAGE_PATH. I have a make-script to precompile these into .py-files which are then loaded at the top of each worksheet using</p>
<pre><code>#auto
from sagelib import *
</code></pre>
<p>which works by creating an all.py file in the directory. The individual .sage files of course also need each other (in a non-cyclic way, naturally), so they also have lines like "from util import *" in the top.</p>
<p>The reason I'm using .sage files and not programming directly in .py should be obvious: I like the sage short-hands. Also, if I did that, I could not even use these short-hands inside the notebook, as I would have to refactor the code if I later wanted to move it to the library (which happens all the time).</p>
<p>The reason I'm then using Python's "import" and not Sage's "load" is for two reasons: One is the module system, so I can avoid stating every single library file in the top of each of my worksheets. The other is for efficiency: Let's say I have two files A.sage and B.sage, and B.sage uses A. If I load all first A and then B, the loading of B would cause A to be reloaded again. By this process, A can be reloaded an exponential number of times. I hope I am wrong, but it seems that this is not really handled in Sage's load. Lastly, I also like being able to use the module system once in a while, to not pollute the namespace overly.</p>
<p>Ok, so my Python recompiling-system works on my local machine, but then I would sometimes like to share code or run on some remote server. I can then put my .py or .sage files on a public server, like bitbucket, and in the top of the remote server's notebook worksheet, I wish to load this url. However, having used the Python module system as I mentioned above, I again cannot use Sage's "load" command, as B would have as it's first line "from A import *", but using Sage's load, module A does not exist. This can then be circumvented using code for dynamically loading modules and more manually (calling sage.misc.remote_file) retrieving the file and making sure it's imported properly (possibly precompiling it if it's a .sage file).</p>
<p>As I mentioned to begin with, this seems to actually work and be somewhat manageable. But the custom code I had to write for ...<span class="expander"> <a>(more)</a></span></p></div> http://ask.sagemath.org/question/7501/how-do-i-share-a-library-of-sage-functions/?answer=13536#post-id-13536Another strategy you should consider is contributing to Sage itself! Sage's goals entail supporting all kinds of specialized research. Sage already includes code for [elliptic curves][1] and [Steenrod algebra][2], both of which I think are intended only for working mathematicians. There are probably others too. So are there parts of your library that make sense to include into Sage?
---
For the remaining parts, which are maybe idiosyncratic, or which you don't want to be public, or which aren't ready for inclusion into Sage, I guess something like what you have is the best I could come up with.
A minor improvement might be to have the top cell update a local copy of the code repository from the server, so that you have all the up-to-date `.py` source on the local machine and can use the Python module system. But maybe you've already thought of reasons this doesn't work so well.
As an aside, it sounds like somewhere early in your workflow you're converting `.sage` code to `.py` code; are you using `sage -preparse` for this?
[1]: http://www.sagemath.org/doc/reference/plane_curves.html
[2]: http://www.sagemath.org/doc/reference/sage/algebras/steenrod/steenrod_algebra.htmlThu, 03 May 2012 03:20:05 -0500http://ask.sagemath.org/question/7501/how-do-i-share-a-library-of-sage-functions/?answer=13536#post-id-13536Comment by jsrn for <p>Another strategy you should consider is contributing to Sage itself! Sage's goals entail supporting all kinds of specialized research. Sage already includes code for <a href="http://www.sagemath.org/doc/reference/plane_curves.html">elliptic curves</a> and <a href="http://www.sagemath.org/doc/reference/sage/algebras/steenrod/steenrod_algebra.html">Steenrod algebra</a>, both of which I think are intended only for working mathematicians. There are probably others too. So are there parts of your library that make sense to include into Sage?</p>
<hr/>
<p>For the remaining parts, which are maybe idiosyncratic, or which you don't want to be public, or which aren't ready for inclusion into Sage, I guess something like what you have is the best I could come up with. </p>
<p>A minor improvement might be to have the top cell update a local copy of the code repository from the server, so that you have all the up-to-date <code>.py</code> source on the local machine and can use the Python module system. But maybe you've already thought of reasons this doesn't work so well.</p>
<p>As an aside, it sounds like somewhere early in your workflow you're converting <code>.sage</code> code to <code>.py</code> code; are you using <code>sage -preparse</code> for this?</p>
http://ask.sagemath.org/question/7501/how-do-i-share-a-library-of-sage-functions/?comment=19822#post-id-19822Wrt. contributing, then most of my stuff is definitely not mature enough (or as you say, too idiosyncratic), while some of it might be contributable now or in the near future. But even if I contribute all of this, I still have this problem with the remaining :) Thu, 03 May 2012 21:59:15 -0500http://ask.sagemath.org/question/7501/how-do-i-share-a-library-of-sage-functions/?comment=19822#post-id-19822Comment by jsrn for <p>Another strategy you should consider is contributing to Sage itself! Sage's goals entail supporting all kinds of specialized research. Sage already includes code for <a href="http://www.sagemath.org/doc/reference/plane_curves.html">elliptic curves</a> and <a href="http://www.sagemath.org/doc/reference/sage/algebras/steenrod/steenrod_algebra.html">Steenrod algebra</a>, both of which I think are intended only for working mathematicians. There are probably others too. So are there parts of your library that make sense to include into Sage?</p>
<hr/>
<p>For the remaining parts, which are maybe idiosyncratic, or which you don't want to be public, or which aren't ready for inclusion into Sage, I guess something like what you have is the best I could come up with. </p>
<p>A minor improvement might be to have the top cell update a local copy of the code repository from the server, so that you have all the up-to-date <code>.py</code> source on the local machine and can use the Python module system. But maybe you've already thought of reasons this doesn't work so well.</p>
<p>As an aside, it sounds like somewhere early in your workflow you're converting <code>.sage</code> code to <code>.py</code> code; are you using <code>sage -preparse</code> for this?</p>
http://ask.sagemath.org/question/7501/how-do-i-share-a-library-of-sage-functions/?comment=19821#post-id-19821On my local machine, I have a make-script calling sage -preparse on all the .sage files which have canged.On remote server's notebooks, I'm currently downloading the .sage files to a temporary directory, then calling preparse_file_named on them, and then import <module>. As the temp dir will be wiped every once in a while, I do each time the server starts. I don't really see how to do it any other way when I don't have control over the sage server.Thu, 03 May 2012 22:02:37 -0500http://ask.sagemath.org/question/7501/how-do-i-share-a-library-of-sage-functions/?comment=19821#post-id-19821