Use sage files as python modules

asked 2016-12-02 15:46:54 -0600

MvG gravatar image

I've got a project where on the one hand I'd like to use the syntactic sugar the sage preparser adds. So I want my source files to be *.sage not *.py at least in my editor. On the other hand, I would like to make use of Python's module system: each sage file should have its own module scope, and names put there should not affect other modules using the same names. I'd also like for modules to reference on another via relative imports, to facilitate code sharing.

Does Sage offer any help for this kind of use case? Any documentation somewhere?

I guess I could manually run the preparser, then rename all the files since Foo.sage.py can't contain a module Foo.sage as dots are not valid parts of a module name there. But perhaps there is something more clever. Some hook to the import system, so it can handle these *.sage.py files or automatically crate them via preparsing if needed. Or some way to select a different extension for the sage -preparse output. Or some way to have sage.repl.load.load do whole-file preparsing, adding relevant imports in particular. Something along these lines.

edit retag flag offensive close merge delete