Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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?