command-line batch-mode showing input along with output

37 views
Skip to first unread message

asciisi...@gmail.com

unread,
Aug 24, 2020, 8:36:10 PM8/24/20
to sympy
Maxima and Maple can batch-execute a text file of commands, showing input interspersed with output:

$ maxima -b t.mac
using Lisp CLISP 2.49+ (2010-07-17)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) batch("t.mac")

read and interpret /t.mac
(%i2) 1+2
(%o2)                                  3
(%i3) 2+3
(%o3)                                  5
(%o4)                /t.mac

$

This is often my preferred method of interacting. Mathematica can't. Sympy can't as far as I know because Python doesn't work like that in batch mode from the command line, as far as I know. For Sympy, to get the effect I want, I have to enter the input lines as individual cells in a Jupyter notebook. If I then need to copy paste the cells to another notebook, I can't, because JupyterLab lacks that functionality.

Aaron Meurer

unread,
Aug 24, 2020, 8:53:06 PM8/24/20
to sympy
You can put code in a .py file and use %run in IPython or the notebook
to run it. Or in pure Python you can add it to a function, import the
file, and run the function (using a function for this case is
recommended because while Python's 'import' will run the code in the
body of the file, it will only import a module once and cache it after
that).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/d4913e80-156a-48fa-914b-27b16049b71cn%40googlegroups.com.

Oscar

unread,
Aug 24, 2020, 9:13:01 PM8/24/20
to sympy
Maybe there could be an option to isympy to do something like this. Give a file t.py like

# t.py
1 + 1
2 + 2

I can do this:

$ isympy -q < t.py

In [1]: Out[1]: 2

In [2]: Out[2]: 4

In [3]: Do you really want to exit ([y]/n)? 

Exiting ...


That almost works but it doesn't show the input.

You can make a simple Python script to do this e.g.:

# run.py
from sympy import *
import sys
filename, = sys.argv[1:]
with open(filename) as inputfile:
    for n, line in enumerate(map(str.strip, inputfile)):
        print('(i%d): %s' % (n, line))
        print('(o%d): %s' % (n, eval(line)))

With that you have:

$ python run.py t.py
(i0): 1 + 1
(o0): 2
(i1): 2 + 2
(o1): 4

--
Oscar
Reply all
Reply to author
Forward
0 new messages