473,499 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Filtering through an external process

Anyone know if there's code around to filter text through an external
process? Sort of like the Emacs "filter-region" command. For
example, say I have a program that reads input in English and outputs
it in Pig Latin. I want my Python script to call the program, pipe
some input into it and read the output:

english = "hello world"
pig_latin = ext_filter("pig_latin", english)

should set pig_latin to "ellohay orldway".

Note that you can't just call popen2, jam the english through it and
then read the pig latin, because the subprocess can block if you give
it too much input before reading the output, and in general there's no
way to know how much buffering the subprocess is willing to do. So a
proper solution has to use asynchronous i/o and keep polling the
output side, or else separate threads for reading and writing.

This is something that really belongs in the standard library. I've
needed it several times and rather than going to the trouble of coding
and debugging it, I've always ended up using a temp file instead,
which is a kludge.
Jul 18 '05 #1
4 1346

"Paul Rubin" <http://ph****@NOSPAM.invalid> wrote in message
news:7x***************@ruckus.brouhaha.com...
Anyone know if there's code around to filter text through an external
process? Sort of like the Emacs "filter-region" command. For
example, say I have a program that reads input in English and outputs
it in Pig Latin. I want my Python script to call the program, pipe
some input into it and read the output:

english = "hello world"
pig_latin = ext_filter("pig_latin", english)

should set pig_latin to "ellohay orldway".

Note that you can't just call popen2, jam the english through it and
then read the pig latin, because the subprocess can block if you give
it too much input before reading the output, and in general there's no
way to know how much buffering the subprocess is willing to do. So a
proper solution has to use asynchronous i/o and keep polling the
output side, or else separate threads for reading and writing.

This is something that really belongs in the standard library. I've
needed it several times and rather than going to the trouble of coding
and debugging it, I've always ended up using a temp file instead,
which is a kludge.


The time machine lives!

=========================
Add this file: Lib/encodings/pig.py
----------------------------------------
"Pig Latin Codec -- Lib/encodings/pig.py"

import codecs, re

def encode(input, errors='strict'):
output = re.sub( r'\b(th|ch|st|\w)(\w+)\b', r'\2\1ay', input)
return (output, len(input))
def decode(input, errors='strict'):
output = re.sub( r'(\b\w+?)(th|ch|st|\w)ay\b', r'\2\1', input)
return (output, len(input))

def getregentry():
return (encode,decode,codecs.StreamReader,codecs.StreamWr iter)
-------------------------------------------
Now, fire-up Python:
'hello world'.encode('pig') 'ellohay orldway' 'ellohay orldway'.decode('pig')

'hello world'

Raymond Hettinger

Jul 18 '05 #2
"Raymond Hettinger" <vz******@verizon.net> writes:
The time machine lives!

=========================
Add this file: Lib/encodings/pig.py
----------------------------------------
"Pig Latin Codec -- Lib/encodings/pig.py"


Chuckle :). But I had in mind a more general purpose means of running
external processes.
Jul 18 '05 #3
Paul Rubin wrote:
Anyone know if there's code around to filter text through an external
process? Sort of like the Emacs "filter-region" command. For

Check out popen2 -- its the piece you need.

dest, result = os.popen2('cmd')
dest.write('echo Hello world\n')
dest.write('exit\n')
dest.close()
result.read()
So, perhaps you mean:
import os

def filtered(command, source):
dest, result = os.popen2(command)
dest.write(source)
dest.close()
try:
return result.read()
finally:
result.close()
-Scott David Daniels
Sc***********@Acm.Org

Jul 18 '05 #4
Scott David Daniels <Sc***********@Acm.Org> writes:
Anyone know if there's code around to filter text through an external
process? Sort of like the Emacs "filter-region" command. For

Check out popen2 -- its the piece you need.


No, that doesn't do the job. If you popen2 a process and send too
much input without reading the output, the subprocess will block and
your application will hang. That is explained in the docs. Doing it
right is a little bit complicated. You need threads or asynchronous
i/o. That's the functionality that's missing.
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3065
by: Richard | last post by:
Hi, I have a form that take some time to load due to many comboboxes and at least 8 subforms. When I filter or sort the main form I get an error message and then Access shuts down. They ask if...
4
20105
by: My SQL | last post by:
Hi Can I trigger an external program to run when a new record is added to the mysql table? The external program is in C and instead of scanning the table continuously for new insertions, it...
0
1339
by: lcifers | last post by:
I have written an application and tested it as a Windows executable. It works fine. The application uses an external COM application to process some existing files. But I can't seem to start that...
5
3609
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
2
1739
by: holysmokes99 | last post by:
I am developing a component in .Net 1.1, and want to debug it using the "start external program" of the debugger in the IDE. The program I want to start references both 1.1 and 2.0 components. The...
3
4154
by: lookaround | last post by:
Hi everyone, I need some help... I call an external exe (a command-line tool) with Process.Start through this code: try { p = new Process(); p.StartInfo.UseShellExecute = false;...
1
2302
by: =?Utf-8?B?Q3JhaWc=?= | last post by:
Hi Guys, I am trying to caputre the output for an external application. The idea is to use the System.Diagnostics.Process to run the exe in a process and redirect the output to a string. When...
2
5050
by: =?Utf-8?B?SXJmYW4=?= | last post by:
Hello, It may be a repeated question but I don't find the solution to the situation that I encounter in it. My application is monitoring another application that is built in VB6. The...
1
1930
by: webotronics | last post by:
Hi, I need to add a timeout for external programs, as the external program sometimes never dies (it's a ClearQuest multisite call to the shipping server, that sometimes never ends, but simply...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3103
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.