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.Fri, 21 Jul 2017 22:43:17 +0200Use sage files as python moduleshttps://ask.sagemath.org/question/35883/use-sage-files-as-python-modules/ 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.Fri, 02 Dec 2016 22:46:54 +0100https://ask.sagemath.org/question/35883/use-sage-files-as-python-modules/Comment by dan_fulea for <p>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 <code>*.sage</code> not <code>*.py</code> 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.</p>
<p>Does Sage offer any help for this kind of use case? Any documentation somewhere?</p>
<p>I guess I could manually run the preparser, then rename all the files since <code>Foo.sage.py</code> can't contain a module <code>Foo.sage</code> 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 <code>*.sage.py</code> files or automatically crate them via preparsing if needed. Or some way to select a different extension for the <code>sage -preparse</code> output. Or some way to have <code>sage.repl.load.load</code> do whole-file preparsing, adding relevant imports in particular. Something along these lines.</p>
https://ask.sagemath.org/question/35883/use-sage-files-as-python-modules/?comment=38351#post-id-38351I do not know the specific situation which needs the syntactic sugar, but
- either extending the sage package
suite with own packages, respecting
the same scheme,
- or using eclipse to
bundle dependencies in projects (and
using in each routine clear
namespaces, so no `from sage.all import
*`) would be ok.
The last is working for me to 80%, although the partial eclipse solution has its own vein.Fri, 21 Jul 2017 22:43:17 +0200https://ask.sagemath.org/question/35883/use-sage-files-as-python-modules/?comment=38351#post-id-38351