Ask Your Question
0

How do I share a library of Sage functions

asked 2012-05-03 00:24:47 -0600

jsrn gravatar image

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 ... (more)

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2012-05-03 03:20:05 -0600

niles gravatar image

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 elliptic curves and Steenrod algebra, 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?

edit flag offensive delete link more

Comments

Wrt. 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 :)

jsrn gravatar imagejsrn ( 2012-05-03 21:59:15 -0600 )edit

On 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.

jsrn gravatar imagejsrn ( 2012-05-03 22:02:37 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-05-03 00:24:47 -0600

Seen: 268 times

Last updated: May 03 '12