Ask Your Question
1

Creating a module in Sage

asked 2019-06-25 12:27:38 -0500

I am new to Sage, being used to Python and I am having some trouble with adapting.

In particular, I am trying to create a 'module' in the Python sense, i.e. some set of classes that I can call with 'import'.

When I did this in python in the past, I would create a directory (say /my_module/ in my home) and include a file __'__init____.sage' in that file with a line

__all__ = ['Submodule1', 'Submodule2']

Then I would have a file 'Submodule1' and 'Submodule2'. In the file 'Submodule 1' I would define 'Class1'

and then from a file 'run.py' in home I would write

import my_module
from my_module.Submodule1 import Class1

and then I would be able to write

c=Class1()

to create an object.

I find that this is not working in Sage and the only thing I seem to be able to do is write in run.py

load("my_module/Submodule1.sage")

for each class I want to load. This has many disadvantages, for instance hiding classes from potential users so that they only exist internally.

Any suggestions on how to create and import modules in sage? What is the sage way?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
6

answered 2019-06-25 16:16:17 -0500

slelievre gravatar image

Instead of .sage files, use .py files. Then everything works pythonically.

The difference between .sage files and .py files is that when Sage reads .py files no preparsing is applied and all imports must be explicit.

For example, if you want to use matrix in such a file, you will have to use

from sage.matrix.constructor import matrix

Note that one can easily know what import statements to include, by using the import_statements function in a Sage session:

sage: import_statements('matrix')
from sage.matrix.constructor import matrix

sage: import_statements('ZZ')
from sage.rings.integer_ring import ZZ

and one can easily know how to replace Sage-specific constructs that take advantage of the Sage preparser, by asking Sage how they are preparsed:

sage: print(preparse('''
....: for i in (1 .. 7):
....:     print(i)
....: '''))

for i in (ellipsis_iter(Integer(1), Ellipsis, Integer(7))):
    print(i)

sage: print(preparse('R.<x> = PolynomialRing(QQ, 2)'))
R = PolynomialRing(QQ, Integer(2), names=('x',)); (x,) = R._first_ngens(1)
edit flag offensive delete link more

Comments

Note: There is also work in-progress to make .sage modules importable like normal Python modules. In this case they would work exactly like .pyfiles except that they would be passed through the Sage pre-processor when you import them. However, we don't know yet when that functionality will actually be available: https://trac.sagemath.org/ticket/27074

Iguananaut gravatar imageIguananaut ( 2019-07-02 07:39:25 -0500 )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

2 followers

Stats

Asked: 2019-06-25 12:27:38 -0500

Seen: 104 times

Last updated: Jun 25