通过 Python 获取 MAPLE 的输出 [英] Grabbing the output of MAPLE via Python

查看:30
本文介绍了通过 Python 获取 MAPLE 的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使用 Python 中的 subprocess 模块来启动 MAPLE 的命令行实例,以将输出提供给主代码并将其返回?例如我想:

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)

返回2"的值.

我所见过的最好的是围绕 MAPLE 命令的 SAGE 包装器,但我不想为我的目的安装和使用 SAGE 的开销.

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.

推荐答案

使用 Alex Martelli 的提示(谢谢!),我对我的问题给出了明确的答案.在这里发帖希望其他人可能会觉得有用:

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

需要对输出进行解析,因为 MAPLE 认为有必要将长输出分解为多行.这种方法的优点是可以保持与 MAPLE 的连接以供将来计算.

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.

这篇关于通过 Python 获取 MAPLE 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆