Capturing MAPLE Output via Python

How can I use the subprocess module in Python to start a MAPLE command line instance to feed and return output to the main code? For example, I would like to:

X = '1+1;'
print MAPLE(X)

To return the value "2".

The best I've seen is the SAGE wrapper around MAPLE commands, but I would not want to install and use the SAGE overhead for my purposes.

+3
source share
3 answers

Using a hint from Alex Martelli (thanks!), I came up with an explicit answer to my question. Held here in the hope that others may prove useful:

import pexpect
MW = "/usr/local/maple12/bin/maple -tu"
X = '1+1;'
child = pexpect.spawn(MW)
child.expect('#--')
child.sendline(X)
child.expect('#--')
out = child.before
out = out[out.find(';')+1:].strip()
out = ''.join(out.split('\r\n'))
print out

, MAPLE . , MAPLE.

+3

" " , , .

pexpect (, Windows: wexpect Windows), - ( ) , / /.

+3

Here is an example of how to do interactive I / O using the command line. I used something similar to create a spell check based on a command line utility ispell:

f = popen2.Popen3("ispell -a")
f.fromchild.readline() #skip the credit line

for word in words:
    f.tochild.write(word+'\n') #send a word to ispell
    f.tochild.flush()

    line = f.fromchild.readline() #get the result line
    f.fromchild.readline() #skip the empty line after the result

    #do something useful with the output:
    status = parse_status(line)
    suggestions = parse_suggestions(line)
    #etc..

The only problem is that it is a very fragile and trial and error process to make sure that you are not sending bad input and are processing all the different results that the program can produce.

0
source

Source: https://habr.com/ru/post/1728238/


All Articles