Skip to content

bagofstuff.pipe

Provides a tool for creating a 'pipe' of functions.

Pipe

Pipe(*functions: Callable[[Any], Any])

A class that provides a simple function pipeline.

Pipe is a little like Python's own partial except that:

  1. It only allows for one positional parameter.
  2. It allows for a whole chain of functions.
Example
from bagoftools.pipe import Pipe

shortest_len = Pipe[str, int](str.trim, len)

print(shortest_len("      this is padded     "))

As well as passing a list of functions when creating the pipe, they can also be added afterwards:

from bagoftools.pipe import Pipe

shortest_len = Pipe[str, int]()
shortest_len |= str.trim
shortest_len |= len

print(shortest_len("      this is padded     "))

Parameters:

Name Type Description Default

functions

Callable[[Any], Any]

The initial set of functions.

()

__call__

__call__(initial: TInitial) -> TResult

Execute the pipeline.

Given an initial value, it is passed to the first function in the pipeline, the result is then passed as the argument to the next function in the pipeline, and so on. The result is the result of the call to the last function in the pipeline.

Parameters:

Name Type Description Default

initial

TInitial

The initial value.

required

Returns:

Type Description
TResult

The result of the pipeline.

__or__

__or__(
    function: Callable[[Any], Any],
) -> Pipe[TInitial, TResult]

Add another function to the pipeline.

Example

Assuming a pipeline called tidy, a call to strip could be added like this:

tidy |= str.strip