Open
Description
I see on #109 #92 #88 a cool feature to be able to chain pipes and save the chain for later.
From the last pr #106 I craft this changes witch allow to do those things available:
class Factor:
n = 10
def __init__(self, n):
self.n = n
@Pipe
@classmethod
def mul(cls, iterable):
return (x * cls.n for x in iterable)
pipes = ... | Factor(2).mul | Factor(5).mul | Pipe(print)
...
diff --git a/pipe.py b/pipe.py
index da89ede..cd07f5f 100644
--- a/pipe.py
+++ b/pipe.py
@@ -66,6 +66,8 @@ class Pipe:
provided arguments and keyword arguments.
"""
+ if other is Ellipsis or isinstance(other, Pipe):
+ return LazyPipe(function=self.function, *self.args, **self.kwargs)
return self.function(other, *self.args, **self.kwargs)
def __call__(self, *args, **kwargs):
@@ -92,6 +94,14 @@ class Pipe:
)
+class LazyPipe(Pipe):
+ def __init__(self, function, *args, **kwargs):
+ def chain_from(data, *args, **kwargs):
+ yield from function(data, *args, **kwargs)
+
+ super().__init__(chain_from, *args, **kwargs)
+
+
@Pipe
def take(iterable, qte):
"""Yield qte of elements in the given iterable."""
@JulienPalard let me know if I shoul include in the PR this or wait?
Metadata
Metadata
Assignees
Labels
No labels