To sum up the question: we have a file myfile.sage
and the question is to turn it into a myfile.py
file.
Automatic
The automatic way is to run in the terminal:
$ sage --preparse myfile.sage
which will produce a file myfile.sage.py
that is a preparsed version of the original file.
In terms of imports, it adds the following import:
from sage.all_cmdline import * # import sage library
Manual
Import, load, run your code and fix each problem as it comes.
Regarding missing imports, use the import_statements
function.
sage: import_statements(var)
from sage.calculus.var import var
sage: import_statements(exists)
from sage.misc.misc import exists
sage: import_statements(forall)
from sage.misc.misc import forall
sage: import_statements(DiGraph)
from sage.graphs.digraph import DiGraph
This will give minimal imports that make the code run in Python (with access to the Sage library).
If you want to remove access to the Sage library, it's different.
Look into all
and any
to replace use of forall
and exists
.
Regarding var
, SymPy certainly has similar functionality,
and would be lighter than Sage.
Regarding graphs, see what graph libraries have the functionality you need.
In CoCalc, are you using a
.sagews
document or a.ipynb
document?Is it a document you can share (eg via CoCalc's share functionality)?
Otherwise can you share a simplified document that illustrates the question?
Thanks for engaging with this. It's a .py document, but I am not sure why it's listed that way: I wrote it in Sage several years ago, and I think it originally was a .sagews file.
I did share the file with you just now.
Among the issues that I'm having:
(1) Declarations like
var('skunks', 'rabbits', 'quadrupeds', 'deer', 'pests', 'beautiful_creatures', 'ugly_creatures', 'birds', 'bats', 'horses', 'ducks', 'pos', 'neg')
(2) the use of 'exists' and 'forall' in dealing with lists
(3) the use of graph operations like making graphs with DiGraph, and also matrix opearations that are native here that might require a package in Python.