Created
April 22, 2012 19:34
-
-
Save bukzor/2466345 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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