Ask Your Question
1

moving from .sage to .py

asked 2019-10-10 10:28:53 -0600

heluani gravatar image

updated 2019-10-10 10:30:33 -0600

Hello, for the last few weeks I have been playing implementing an algebraic category in sage. I am new to both Sage and Python, and my code has grown large enough to be a large hierarchy of interdependent modules. I want to move those .sage files to .py files (eventually .pyx and adding some c code in parts) with the view to eventually add them to my local Sage library, but I am having trouble understanding the workflow in Python. My problem is how to figure out all the imports that my files will need. Within the interpreter I have available lots of symbols, so that when attaching a .sage there's no errors of global names being undefined. But if I want to keep these files as separate modules (so that I can't call from sage.all import * ) those symbols will not be present in the scope of the files.

I am used to compiled languages where I would run a compiler, find all errors, fix them as much as I can and repeat the process until no errors are found. But in the current situation I see only two options: either try to import the module from the Sage console like from mysage.myalgebra.thisalgebra import ThisAlgebra and expect some global name 'ThisName' is not defined error, fix it and continue. And on top of this, even if the symbol imports correctly, there are plenty of such errors that will popup at runtime for example trying to run ThisAlgebra.this_method if this_method uses some other undenfined global name. The other option would be trying to compile the python file, but for this I'll need to preparse the sage file. The command sage-preparse imports all again, so that's not an option.

So my question is how to get a list of all needed imports at once instead of iterating solving one by one, and subtly debugging all runtime calls.

edit retag flag offensive close merge delete

Comments

use the command line tool "pyflakes" to find undefined objects in a given py file, and use sage command "import_statements" lo get the required line to add to this file

FrédéricC gravatar imageFrédéricC ( 2019-10-10 12:49:05 -0600 )edit

Thanks, didn't know about pyflakes, that did it. Now have to deal with the subtleties of having hardcoded rational numbers in the .sage files (which are now zeroes popping out everywhere).

heluani gravatar imageheluani ( 2019-10-10 16:29:37 -0600 )edit

1 answer

Sort by » oldest newest most voted
4

answered 2019-10-10 15:37:53 -0600

tmonteil gravatar image

updated 2019-10-10 15:59:09 -0600

Here is a short recipe, assuming that your file is named your_file.sage.

From a standard terminal, type:

sage -preparse your_file.sage

A file named your_file.sage.py will appear.

Edit it and remove (or comment) the from sage.all_cmdline import * statement.

Now, install pyflakes for Python 2 [this will soon be Python 3, on Debian, the corresponding package will be pyflakes3].

Then run pyflakes your_file.sage.py from a standard terminal. You will get a lot of lines of the kind:

your_file.sage.py:XX: undefined name 'Blah'

For each such Blah, in a Sage session, run:

sage: import_statements('Blah')

Sage will give you the line to add at the beginning of your_file.sage.py.

For example, if pyflakes returns:

 your_file.sage.py:XXX: undefined name 'Integer'

The command

sage: import_statements('Integer')

gives

from sage.rings.integer import Integer

which you have to add to the beginning of your_file.sage.py.

edit flag offensive delete link more

Comments

preparsing has the drawback that adds _sage_const_x making the code less readable and often times being worse than the python integer. I ended up reverting most of these changes and having explicitly Integer(x) in the few cases that were needed

heluani gravatar imageheluani ( 2019-10-11 08:29:21 -0600 )edit

Note that in some (most) cases, a given Sage function f that was given Integer(3) will behave the same with int(3), so that you could ligthen the code even more with f(3).

tmonteil gravatar imagetmonteil ( 2019-10-11 08:47:08 -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: 2019-10-10 10:28:53 -0600

Seen: 38 times

Last updated: Oct 10