Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture output from pexpect

I am having trouble with pexpect. I'm trying to grab output from tralics which reads in latex equations and emits the MathML representation, like this:

1 ~/ % tralics --interactivemath
This is tralics 2.14.5, a LaTeX to XML translator, running on tlocal
Copyright INRIA/MIAOU/APICS/MARELLE 2002-2012, Jos\'e Grimm
Licensed under the CeCILL Free Software Licensing Agreement
Starting translation of file texput.tex.
No configuration file.
> $x+y=z$
<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi>   <mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>
> 

So I try to get the formula using pexpect:

import pexpect
c = pexpect.spawn('tralics --interactivemath')
c.expect('>')
c.sendline('$x+y=z$')
s = c.read_nonblocking(size=2000)
print s

The output has the formula, but with the original input at the beginning and some control chars at the end:

"x+y=z$\r\n<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi><mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>\r\n\r> \x1b[K"

I can clean the output string, but I must be missing something basic. Is there a cleaner way to get the MathML?

like image 714
Tim Avatar asked Oct 10 '12 17:10

Tim


People also ask

What does Pexpect expect return?

The important methods of pexpect. spawn class are expect(). This method waits for the child process to return a given string. The pattern specified in the except method will be matched all through the string.

What does Pexpect spawn do?

Pexpect is a Python module for spawning child applications and controlling them automatically. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers.

How do you close Pexpect spawn?

spawn. close() would close the pty which in turn would send SIGHUP to the shell which in turn would terminate (kill) the shell unless the shell is ignoring SIGHUP . It's like you close the PuTTY (or gnome-terminal , ...) window when the shell is still running.

Does Pexpect work on Windows?

Original Pexpect should work on any platform that supports the standard Python pty module. While Wexpect works on Windows platforms.


1 Answers

From what I understand you are trying to get this from pexpect:

<formula type='inline'><math xmlns='http://www.w3.org/1998/Math/MathML'><mrow><mi>x</mi>   <mo>+</mo><mi>y</mi><mo>=</mo><mi>z</mi></mrow></math></formula>

You can use a regexp instead of ">" for the matching in order to get the expected result. This is the easiest example:

c.expect("<formula.*formula>");

After that, you can access the matched string by calling the match attribute of pexpect:

print c.match

You might also try different regexps, due to the fact that the one I posted is a greedy one and it might hinder your execution time if the formulas are big.

like image 181
Catalin Luta Avatar answered Sep 21 '22 09:09

Catalin Luta