[bip] replacing those shell scripts ... the PipeChain
James Casbon
casbon at gmail.com
Thu May 7 06:46:50 PDT 2009
So I often want to use the unix toolchain to get a quick idea of some
data. You know, grep this, cut that, sort, etc. I often find myself
thinking that I won't use python because writing all those Popen(blah,
stdout=PIPE) is just too verbose. However, this means that the
migration from shell script to python script has an annoying little
little bump.
So, without further ado, I present a little helper called PipeChain.
This allows this kind of code:
chain = PipeChain('grep ^x', 'cut -d" " -f2-3', 'sort', 'uniq -c')
# use on a file
proc = chain(file('input1.txt), file('output1.txt'))
proc.wait()
# or get a handle on the results
proc = chain(file('input1.txt))
proc.stdout.read()
I have attached the file. I need one bit of help though - it would be
nice to be able to pass in an iterable to use as the input_handle.
subprocess either wants a proper handle, or PIPE. How can you stream
an iterable into a subprocess.PIPE without blocking?
cheers,
James
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pipechain.py
Type: application/octet-stream
Size: 2454 bytes
Desc: not available
Url : http://lists.idyll.org/pipermail/biology-in-python/attachments/20090507/25d1185f/attachment.obj
More information about the biology-in-python
mailing list