Skip to content

Commit

Permalink
APPLY CHANGES FROM: pull JulienPalard#88
Browse files Browse the repository at this point in the history
* ADDED: Pipe.__or__() function
* USE: self.__class__ instead of Pipe class
  REASON: Simlify to derive from Pipe class and extend functionality
* Add test for pipeline recipe based on or-pipes.

SEE ALSO:

* JulienPalard#88
  • Loading branch information
jenisys committed May 29, 2023
1 parent f9b513e commit 4d8f6da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,19 @@ def __init__(self, function):
def __ror__(self, other):
return self.function(other)

# SEE: https://github.com/JulienPalard/Pipe/pull/88
def __or__(self, other):
cls = self.__class__
if isinstance(other, Pipe):
return cls(lambda iterable, *args2, **kwargs2: (
other.function(self.function(iterable, *args2, **kwargs2))))
# -- CASE: other is pipeable only
return cls(lambda iterable, *args2, **kwargs2: (
self.function(iterable, *args2, **kwargs2) | other))

def __call__(self, *args, **kwargs):
return Pipe(
cls = self.__class__
return cls(
lambda iterable, *args2, **kwargs2: self.function(
iterable, *args, *args2, **kwargs, **kwargs2
)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_pipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,15 @@ def test_enumerate():
data = [4, "abc", {"key": "value"}]
expected = [(5, 4), (6, "abc"), (7, {"key": "value"})]
assert list(data | pipe.enumerate(start=5)) == expected


def test_concatenate_pipes():
data = range(10)
is_even = pipe.where(lambda x: x % 2 == 0)
higher_than_4 = pipe.where(lambda x: x > 4)
expected = [6,8]
# standard behavior
assert list(data | is_even | higher_than_4) == expected
# concatenated pipes
is_even_and_higher_than_4 = is_even | higher_than_4
assert list(data | is_even_and_higher_than_4) == expected

0 comments on commit 4d8f6da

Please sign in to comment.