Skip to content

using Ellipsis operator for chaining pipes #110

Open
@AdrianCert

Description

@AdrianCert

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions