You can construct your pipes using the Pipe class like:
from pipe import Pipe
square = Pipe(lambda iterable: (x ** 2 for x in iterable))
map = Pipe(lambda iterable, fct: builtins.map(fct, iterable)
>>>
As you can see it's often very short to write, and with a bit of luck
the function you're wrapping already takes an iterable as the first
argument, making the wrapping straight forward:
>>> from collections import deque
>>> from pipe import Pipe
>>> end = Pipe(deque)
>>>
and that's it itrable | end(3) is deque(iterable, 3):
>>> list(range(100) | end(3))
[97, 98, 99]
>>>
In case it gets more complicated one can use Pipe as a decorator to
a function taking an iterable as the first argument, and any other
optional arguments after:
>>> import re
>>> @Pipe
... def grep(iterable, pattern, flags=0):
... for line in iterable:
... if re.match(pattern, line, flags=flags):
... yield line
...
>>> lines = ["Hello", "hello", "World", "world"]
>>> for line in lines | grep("H"):
... print(line)
Hello
>>>
Now let's reuse it in two ways, first with a pattern:
>>> lowercase_only = grep("[a-z]+$")
>>> for line in lines | lowercase_only:
... print(line)
hello
world
>>>
Or now with a flag:
>>> igrep = grep(flags=re.IGNORECASE)
>>> for line in lines | igrep("hello"):
... print(line)
...
Hello
hello
>>>
Lazy evaluation
Pipe uses generators all the way down, so it is naturally lazy.
In the following examples we'll use
itertools.count:
an infinite generator of integers.
We'll make use of the tee pipe too, which prints every values that
passe through it.
The following example does nothing, nothing is printed by tee so no
value passed through it. It's nice because generating an infinite
sequence of squares is "slow".
>>> result = count() | tee | select(lambda x: x ** 2)
>>>
Chaining more pipes still won't make previous ones start generating
values, in the following example not a single value is pulled out of
count:
>>> result = count() | tee | select(lambda x: x ** 2)
>>> first_results = result | take(10)
>>> only_odd_ones = first_results | where(lambda x: x % 2)
>>>
Same without variables:
>>> result = (count() | tee
... | select(lambda x: x ** 2)
... | take(10)
... | where(lambda x: x % 2))
>>>
Only when values are actually needed, the generators starts to work.
In the following example only two values will be extracted out of count:
0 which is squared (to 0), passes the take(10) eaily,
but is dropped by where
1 which is squared (to 1), also easily passes the take(10),
passes the where, and passes the take(1).
At this point take(1) is satisfied so no other computations need to
be done. Notice tee printing 0 and 1 passing through it:
>>> result = (count() | tee
... | select(lambda x: x ** 2)
... | take(10)
... | where(lambda x: x % 2))
>>> print(list(result | take(1)))
0
1
[1]
>>>
Deprecations
In pipe 1.x a lot of functions were returning iterables and a lot
other functions were returning non-iterables, causing confusion. The
one returning non-iterables could only be used as the last function of
a pipe expression, so they are in fact useless:
range(100) | where(lambda x: x % 2 == 0) | add
can be rewritten with no less readability as:
sum(range(100) | where(lambda x: x % 2 == 0))
so all pipes returning non-iterables were deprecated (raising
warnings), and finally removed in pipe 2.0.
What should I do?
Oh, you just upgraded pipe, got an exception, and landed here? You
have three solutions:
Stop using closing-pipes, replace ...|...|...|...|as_list to
list(...|...|...|), that's it, it's even shorter.
If "closing pipes" are not an issue for you, and you really like
them, just reimplement the few you really need, it often take a very
few lines of code, or copy them from
here.
If you still rely on a lot of them and are in a hurry, just pip install pipe<2.
And start testing your project using the Python Development
Mode so you catch
those warnings before they bite you.
An @Pipe is often easily implemented in a 1 to 3 lines of code
function, and the pipe module does not aim at giving all
possibilities, it aims at giving the Pipe decorator.
So if you need more pipes, closing pipes, weird pipes, you-name-it,
feel free to implement them on your project, and consider the
already-implemented ones as examples on how to do it.
See the Constructing your own paragraph below.
Alternatives
Use instead of pipe: tools that do the same job, closest first.