moving from .sage to .py
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.
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
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).