communicate with external process via pty
Tim
jtim.arnold at gmail.com
Wed Oct 10 15:23:31 EDT 2012
On Wednesday, October 10, 2012 2:58:55 AM UTC-4, Peter Otten wrote:
> Tim Arnold wrote:
>
> > import os,sys
> > import subprocess
> > import shlex
> > import pty
> > cmd = 'tralics --interactivemath'
> > (master, slave) = pty.openpty()
> > p = subprocess.Popen(shlex.split(cmd),close_fds=True,
> > stdin=slave,stdout=slave,stderr=slave)
> >
> > os.read(master,1024) # start the process
> > os.write(master,'$\sqrt{x}$\n') # feed it an equation
> > mathml.append(os.read(master,1024)) # get the mathml in a string
> > os.write(master,'$\sqrt{x}$\n') # feed more equations
> > mathml.append(os.read(master,1024)) # get the next string
>
>
> Do you know about pexpect?
>
> http://pypi.python.org/pypi/pexpect
Thanks Peter. I knew of it, but hadn't tried it. I've been playing with it today and I think it should work fine for my project. With some help from stackoverflow, this seems to do what I need:
import pexpect
c = pexpect.spawn('tralics --interactivemath')
c.expect('>')
c.sendline('$x+y=z$')
c.expect("<formula.*formula>")
print c.math.group()
c.expect('>')
c.sendline('$a+b=c$')
c.expect("<formula.*formula>")
print c.math.group()
etc.
thanks,
--Tim
More information about the Python-list
mailing list