Ask Your Question
1

export sage file as python

asked 2020-08-23 23:30:15 +0200

Larry Moss gravatar image

I have a CoCalc notebook running a SageMath program. What I have runs under SageMath 9.1. Someone wants to include my stuff in a Python work environment. So the first step is to convert it to Python. I tried 'preparse', but that doesn't seem to work from the terminal window.

Is there a working automatic translation to help with this conversion?

edit retag flag offensive close merge delete

Comments

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?

slelievre gravatar imageslelievre ( 2020-08-24 05:38:55 +0200 )edit

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.

Larry Moss gravatar imageLarry Moss ( 2020-08-25 00:20:00 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-08-25 02:17:18 +0200

slelievre gravatar image

updated 2020-08-25 02:33:32 +0200

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.

edit flag offensive delete link more

Comments

Thanks very much for all of this! This should go a long way towards doing what I need to do here.

Larry Moss gravatar imageLarry Moss ( 2020-08-25 03:58:23 +0200 )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: 2020-08-23 23:30:15 +0200

Seen: 106 times

Last updated: Aug 25 '20