Skip to content

Instantly share code, notes, and snippets.

@bukzor
Created April 22, 2012 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bukzor/2466345 to your computer and use it in GitHub Desktop.
Save bukzor/2466345 to your computer and use it in GitHub Desktop.
class ProcessingPipeline(object):
def __init__(self, *functions, **kwargs):
self.functions = functions
self.data = kwargs.get('data')
def __call__(self, data):
return ProcessingPipeline(*self.functions, data=data)
def __iter__(self):
print "iter"
data = self.data
for func in self.functions:
data = func(data)
return data
class Multiplier(object):
def __init__(self, by):
self.by = by
def __call__(self, data):
print "call"
for x in data:
yield x * self.by
def add(data, y):
for x in data:
yield x + y
from functools import partial
by2 = Multiplier(by=2)
sub1 = partial(add, y=-1)
square = lambda data: ( x*x for x in data )
# first operation is square
pp = ProcessingPipeline(square, sub1, by2,)
print list(pp(range(10)))
print list(pp(range(-3, 4)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment