Pipe is a Python library that enables infix notation (pipes), offering a cleaner alternative to nested function calls. Here are some of the most useful methods from the Pipe library:
select and where (aliases for map and filter):
Python’s built-in map
and filter
functions are powerful tools for working with iterables, allowing for efficient data transformation and filtering. However, when used together, they can lead to code that’s difficult to read due to nested function calls. For example:
nums = [1, 2, 3, 4, 5, 6]
list(
filter(lambda x: x % 2 == 0,
map(lambda x: x ** 2, nums)
)
)
[4, 16, 36]
Pipe allows for a more intuitive and readable way of chaining operations:
from pipe import select, where
list(
nums
| select(lambda x: x ** 2)
| where(lambda x: x % 2 == 0)
)
[4, 16, 36]
In this version, the operations are read from left to right, mirroring the order in which they’re applied. The select
method corresponds to map
, while where
corresponds to filter
. This syntax not only improves readability but also makes it easier to add, remove, or reorder operations in your data processing pipeline.
traverse:
The traverse
method recursively unfolds nested iterables, which is useful for flattening deeply nested lists:
from pipe import traverse
from pipe import traverse
nested = [[1, 2, [3]], [4, 5]]
flattened = list(nested | traverse)
print(flattened)
[1, 2, 3, 4, 5]
chain:
The chain
method combines multiple iterables:
from pipe import chain
result = list([[1, 2], [3, 4], [5]] | chain)
print(result)
[1, 2, 3, 4, 5]
take and skip:
These methods allow you to select or skip a specific number of elements from an iterable:
from pipe import take, skip
from itertools import count
first_five = list(count() | take(5))
print(first_five)
[0, 1, 2, 3, 4]
skip_first_two = list([1, 2, 3, 4, 5] | skip(2))
print(skip_first_two)
[3, 4, 5]