Grabbing Output in Linux

asked 2019-11-26 01:03:50 +0200

Kraig gravatar image

updated 2019-11-26 01:05:07 +0200

Hello!

I have a great deal of code written in Pascal, and this code runs on a linux terminal. Currently, the only way I can run sage math through this program is to do the following: type a list of commands to run in sage as a concatenated strings separated by semicolons, and I then write the output to a textfile. For example 'load(''whatever'');function_call(something);... > sometextfile.txt'. This entire line is written in a call to the terminal. Effectively what is fed is sage -c that-string. The problem with this method is it adds time because sage has to open and close every time it runs. Is there a command in sage that allows me to keep the sage terminal persistent and I then ping that terminal with new commands. I assume the way to ping with new commands is to pipe from one terminal to the one running sage, but I am not a unix connisseur.

Of course, the easiest method is to run my code via python and have python run the pascal code. This is, unfortunately, not an option.

edit retag flag offensive close merge delete

Comments

1

I do not understand where is the problem. What forbids you to do:

sage: os.system("cat that-string.txt | yourPascalProgram")

For all practical purposes, Sage is a Python interpreter...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-11-26 06:29:07 +0200 )edit

Sage is also a computer algebra system. I have an entire program written in another language, so I was wondering if there was some fast way to have my program run sage to get output.

Kraig gravatar imageKraig ( 2019-11-26 14:24:27 +0200 )edit

Of course, the easiest method is to run my code via python and have python run the pascal code. This is, unfortunately, not an option.

Why not? That would probably be much cleaner.

Iguananaut gravatar imageIguananaut ( 2019-11-26 15:48:23 +0200 )edit

Ultimately, if you really wanted to keep open a Sage interpreter and essentially "screen scrape" it you could do that; most of Sage's original (and some still) interfaces to other CASes are doing exactly that, though there has been a move toward tighter low-level library interfacing since this way of doing this is woefully inefficient. But then your question becomes a Pascal programming question, of how to run a subprocess and interact with its standard I/O streams. There is nothing to be done from the Sage end (e.g. as you wrote "a command in sage") that would facilitate this. In Python we use pexpect for this; it's the Python implementation of the Expect library. Maybe there is one for Pascal.

Iguananaut gravatar imageIguananaut ( 2019-11-26 15:55:32 +0200 )edit

You might even consider making your own Sage interface to your program (that documentation might be slightly out of date but it should point in the right direction).

Iguananaut gravatar imageIguananaut ( 2019-11-26 15:59:35 +0200 )edit