Search code examples
pythonsubprocesspexpectmaple

Grabbing the output of MAPLE via Python


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

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

To return the value of "2".

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


Solution

  • Using the tip from Alex Martelli (thank you!), I've came up with an explicit answer to my question. Posting here in hopes that others may find 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
    

    The parsing of the output is needed as MAPLE deems it necessary to break up long outputs onto many lines. This approach has the advantage of keeping a connection open to MAPLE for future computation.