Python Pipe Library
The `pipe` library enables a shell-like infix syntax in Python, allowing users to chain operations on iterables using the pipe (`|`) operator. It provides a functional programming style for data processing, similar to LINQ in C# or shell pipes. The current stable version is 2.2, and it follows a slow but steady release cadence, focusing on stability given its utility nature.
Common errors
-
NameError: name 'select' is not defined
cause Attempting to use a `pipe` function (like `select`, `where`, `take`) without explicitly importing it from the `pipe` module.fixAdd the necessary import statement: `from pipe import select` (or the specific function you are using). -
TypeError: unsupported operand type(s) for |: 'int' and 'function'
cause Trying to apply a pipe operation to a non-iterable object, such as an integer, instead of a list, tuple, or generator.fixEnsure the initial object being piped is an iterable. If you have a single value, wrap it in a list: `[my_int] | select(...)`. -
(No output/empty result when expected)
cause The `pipe` operations return generators, which are lazy. If you don't explicitly consume the generator, you won't see any concrete results or side effects.fixCall `list()`, `tuple()`, `set()`, `next()`, or iterate over the result to force evaluation: `final_data = list(initial_data | op1 | op2)`.
Warnings
- gotcha Pipe operations are lazy and return generators. To get a concrete result (e.g., a list or a single value), you must explicitly consume the generator.
- gotcha The `pipe` library functions (e.g., `select`, `where`) are standalone functions that receive the iterable via the pipe operator, not methods of the iterable itself.
- gotcha Piped operations work on iterables. Passing a non-iterable (like an integer) as the initial input or at any point in the chain where an iterable is expected will raise a TypeError.
Install
-
pip install pipe
Imports
- select
from pipe import select
- where
from pipe import where
- take
from pipe import take
Quickstart
from pipe import select, where, take l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Chain operations: filter even numbers, double them, take the first 3 result = l | where(lambda x: x % 2 == 0) | select(lambda x: x * 2) | take(3) # The result is a generator; convert to list to see concrete values final_list = list(result) print(final_list)