Shell like syntax for subprocess.Popen # overloading >, <, |

Serge Orlov Serge.Orlov at gmail.com
Wed Apr 19 09:21:22 EDT 2006


jelle wrote:
> Hi,
>
> I use python quite a bit to couple different programs together.
> Doing so has been a _lot_ easier since subprocess came around, but
> would really like to be able to use the succinct shell syntax; >, <, |
>
> That really shouldn't be too hard to wrap in a class, but so far I
> didn't succeed to do so this well, since I'm facing some trouble with
> operator precedence that I do not know how to overcome.
>
> Consider the following:
>
> A = 'inputString'
> B = Process('process.exe')
> C = cStringIO.StringIO() # output bucket
>
> A > B > C
>
> A is being piped to B and processed, but the output of B is not being
> piped to C
> executing A > B; B > C works as expected however.
> Which is disappointing, since what I'm trying to achieve is a sugar
> syntax for Popen processes, where directly sees the chain of
> commands...
>
> Any suggestions to overcome this issue are greatly appreciated!

How about good old function call?

A = Lines('inputString')
B = Process('process.exe')
C = Process('proc2.exe')
result = pipeline(A, B, C, Lines()) # Text result
result = pipeline(A, B, C, Bytes()) # Binary result
pipeline(A, B, C, file("somefile","wb")) # dump to binary file




More information about the Python-list mailing list