Welcome, guest | Sign In | My Account | Store | Cart

This module is inspired by recipe 276960 and shows how generators can be combined with a pipe-like syntax. A similar approach, using Popen, pipes and Thread, is presented in recipe 576757.

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Pipeable( object ):
	def _other( self, other ):
		self.other = other
	def __iter__( self ):
		for i in self.other:
			yield i
	def __or__( self, right ):
		right._other( self )
		return right

class Filter( Pipeable ):
	def __init__( self, filter ):
		self.filter = filter
	def __iter__( self ):
		for line in self.other:
			yield self.filter( line )

class Cat( Pipeable ):
	def __init__( self, iterable ):
		self.other = iterable

class Reverse( Pipeable ):
	def __iter__( self ):
		for line in self.other:
			yield line[::-1]

if __name__ == '__main__':
	for line in Cat( ('This', 'is', 'just', 'an', 'example') ) | Reverse() | Filter( lambda str : str[::-1] ):
		print line,
	

This is just an hack, but can probably be transformed in an interesting way of combining generators for people used to shell like languages.

Created by Massimo Santini on Fri, 15 May 2009 (MIT)
Python recipes (4591)
Massimo Santini's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks