Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

communicate with external process via pty

45 views
Skip to first unread message

Tim Arnold

unread,
Oct 9, 2012, 3:44:49 PM10/9/12
to
I have an external process, 'tralics' that emits mathml when you feed it
latex equations. I want to get that mathml into a string.

The problem for me is that tralics wants to talk to a tty and I've never
done that before; it basically starts its own subshell.

I have the following code which works for simple things. I'm not sure
this is the best way though: basically I got this from google...

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


Any suggestions for improvement?
thanks,
--Tim

Peter Otten

unread,
Oct 10, 2012, 2:59:04 AM10/10/12
to pytho...@python.org
Do you know about pexpect?

http://pypi.python.org/pypi/pexpect

Tim

unread,
Oct 10, 2012, 3:23:31 PM10/10/12
to pytho...@python.org
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


Tim

unread,
Oct 10, 2012, 3:23:31 PM10/10/12
to comp.lan...@googlegroups.com, pytho...@python.org
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

0 new messages