classPipe: """ Represent a Pipeable Element : Described as : first = Pipe(lambda iterable: next(iter(iterable))) and used as : print [1, 2, 3] | first printing 1 Or represent a Pipeable Function : It's a function returning a Pipe Described as : select = Pipe(lambda iterable, pred: (pred(x) for x in iterable)) and used as : print [1, 2, 3] | select(lambda x: x * 2) # 2, 4, 6 """
def__init__(self, function): self.function = function functools.update_wrapper(self, function)
@Pipe defdedup(iterable, key=lambda x: x): """Only yield unique items. Use a set to keep track of duplicate data.""" seen = set() for item in iterable: dupkey = key(item) if dupkey notin seen: seen.add(dupkey) yield item