bash-style pipes in python?

Dan Stromberg - Datallegro dstromberg at datallegro.com
Thu Jul 12 02:56:21 CEST 2007


I'm constantly flipping back and forth between bash and python.

Sometimes, I'll start a program in one, and end up recoding in the
other, or including a bunch of python inside my bash scripts, or snippets
of bash in my python.

But what if python had more of the power of bash-style pipes?  I might not
need to flip back and forth so much.  I could code almost entirely in python.

The kind of thing I do over and over in bash looks like:

	#!/usr/bin/env bash

	# exit on errors, like python. Exit on undefind variables, like python.
	set -eu

	# give the true/false value of the last false command in a pipeline
	# not the true/false value of the lat command in the pipeline - like
	# nothing I've seen
	set -o pipefail

	# save output in "output", but only echo it to the screen if the command fails
	if ! output=$(foo | bar 2>&1)
	then
		echo "$0: foo | bar failed" 1>&2
		echo "$output" 1>&2
		exit 1
	fi

Sometimes I use $PIPESTATUS too, but not that much.

I'm aware that python has a variety of pipe handling support in its
standard library.

But is there a similarly-simple way already, in python, of hooking the stdout of
process foo to the stdin of process bar, saving the stdout and errors from both
in a variable, and still having convenient access to process exit values?

Would it be possible to overload | (pipe) in python to have the same behavior as in
bash?

I could deal with slightly more cumbersome syntax, like:

	(stdout, stderrs, exit_status) = proc('foo') | proc('bar')

...if the basic semantics were there.

How about it?  Has someone already done this?




More information about the Python-list mailing list