communicate with external process via pty

T

Tim Arnold

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
 
P

Peter Otten

Tim said:
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?

Do you know about pexpect?

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

Tim

Tim Arnold wrote:



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
 
T

Tim

Tim Arnold wrote:



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top