easier subprocess piping and redirection

Dear python enthousiasts, While replacing some of my bash scripts by python scripts, I found the following useful. I wrote a small utility module that allows piping input and output in a very similar way to how you do it in bash. The major advantage over just using shell=True is that this does not expose us to the latter's security risks and quoting hazards. The major advantage of using a pipe instead of just manually feeding the output of one process to the other is that with a pipe, it doesn't all have to fit into memory at once. However, the Python way of doing that is rather cumbersome: >>> from subprocess import Popen, PIPE >>> p1 = Popen(['echo', 'a\nb'], stdout=PIPE) >>> p2 = Popen([head], '-n1'], stdin=p1.stdout, stdout=PIPE) >>> p2.communicate() ('a\n', None) I've been able to replace this by: >>> from pipeutils import call >>> (call('echo', 'a\nb') | call('head', '-n1')).output() 'a\n' And similarly, I can do direct file redirects like this: >>> (call('echo', 'Hello world!') > 'test.txt').do() 0 >>> (call('cat') < 'test.txt').output() 'Hello world!\n' I think that this is a lot more concise and readable. As far as I'm concerned, this was the only reason I would ever use bash scripts instead of python scripts. I would love to hear opinions on this. How would people like this as a library? The source is online at https://bitbucket.org/tkluck/pipeutils Best, Timo

Hi Timo, On 27.02.13 12:27, Timo Kluck wrote:
Dear python enthousiasts,
While replacing some of my bash scripts by python scripts, I found the following useful. I wrote a small utility module that allows piping input and output in a very similar way to how you do it in bash. The major advantage over just using shell=True is that this does not expose us to the latter's security risks and quoting hazards.
The major advantage of using a pipe instead of just manually feeding the output of one process to the other is that with a pipe, it doesn't all have to fit into memory at once. However, the Python way of doing that is rather cumbersome:
>>> from subprocess import Popen, PIPE >>> p1 = Popen(['echo', 'a\nb'], stdout=PIPE) >>> p2 = Popen([head], '-n1'], stdin=p1.stdout, stdout=PIPE) >>> p2.communicate() ('a\n', None)
I've been able to replace this by:
>>> from pipeutils import call >>> (call('echo', 'a\nb') | call('head', '-n1')).output() 'a\n'
thanks a lot for sharing this. How does this relate to plumbum :-? >>> from plumbum.cmd import echo, head >>> chain = echo['a\nb'] | head['-n1'] >>> chain() u'a\n' ($> pip install plumbum) ... http://plumbum.readthedocs.org/en/latest/
And similarly, I can do direct file redirects like this:
>>> (call('echo', 'Hello world!') > 'test.txt').do() 0 >>> (call('cat') < 'test.txt').output() 'Hello world!\n'
I think that this is a lot more concise and readable. As far as I'm concerned, this was the only reason I would ever use bash scripts instead of python scripts.
I would love to hear opinions on this. How would people like this as a library?
The source is online at https://bitbucket.org/tkluck/pipeutils ...
All the best, Stefan.

2013/2/27 Stefan Drees <stefan@drees.name>:
thanks a lot for sharing this. How does this relate to plumbum :-?
>>> from plumbum.cmd import echo, head >>> chain = echo['a\nb'] | head['-n1'] >>> chain() u'a\n'
Fantastic! I kind of guessed that it would have existed already, but didn't know where to find it. Thanks for pointing this out! Timo

On Wed, Feb 27, 2013 at 01:20:17PM +0100, Timo Kluck <tkluck@infty.nl> wrote:
2013/2/27 Stefan Drees <stefan@drees.name>:
thanks a lot for sharing this. How does this relate to plumbum :-?
>>> from plumbum.cmd import echo, head >>> chain = echo['a\nb'] | head['-n1'] >>> chain() u'a\n'
Fantastic! I kind of guessed that it would have existed already, but didn't know where to find it. Thanks for pointing this out!
Very easy -- search for packages at Python Package Index, PyPI: https://pypi.python.org/pypi https://pypi.python.org/pypi?%3Aaction=search&term=pipe&submit=search Add your package to the Index so other people can find it. Oleg. -- Oleg Broytman http://phdru.name/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.
participants (3)
-
Oleg Broytman
-
Stefan Drees
-
Timo Kluck