Skip to main content

A utility for calling functions in pipelines.

Project description

Pipeline Functions

Last release Python version Test status Test coverage Last commit

Pipeline Functions is a simple utility for calling functions in "pipelines", i.e. where the output from one function is automatically the input for the next. Here's an example showing how this works:

>>> from pipeline_func import f, X
>>> a = [3, 4, 2, 1, 0]
>>> b = [2, 1, 4, 0, 3]
>>> a | f(zip, b) | f(map, min, X) | f(filter, bool, X) | f(sorted)
[1, 2, 2]

To briefly explain, we start with two random lists of integers. We then find the smaller value in each position, discard any zeros, and sort whatever remains. Contrast the easily-readable pipeline syntax to what would be necessary without it:

>>> sorted(filter(bool, map(min, zip(a, b))))
[1, 2, 2]

Documentation

This library provides a single function, named f(). The purpose of f() is to wrap another function such that it can be called as part of a pipeline. Below is the call signature for f():

f(func: Callable[..., Any], /, *args: Any, **kwargs: Any)

The first argument to f() must be a function. The remaining arguments can be arbitrary positional and/or keyword arguments. f() produces a wrapped version of the given function which can subsequently be called using the | operator as shown above. By default, the value from the left-hand side of the operator will be the first argument to the function. Any extra arguments provided to f will be added after.

If it's not convenient for the incoming value to be the first argument to the wrapped function, you can use the X object to put the arguments in any order you want. Think of X as a placeholder that will be replaced by the value from the previous step in the pipeline. X can be either a positional or a keyword argument, and can be used for multiple arguments at the same time. You can also use attribute access syntax (e.g. X.my_attr), item lookup syntax (e.g. X[key]), function invocation syntax (e.g. X(arg1, arg2)), or any combination of the above (e.g. X.my_method()[0]) to easily extract information from the input value.

To make this more concrete, here are some examples showing how f() can be used to call some common functions:

Pipeline Equivalent to Result
[1, 2] | f(min) min([1, 2]) 1
[1, 2] | f(zip, [3, 4]) zip([1, 2], [3, 4]) [[1, 3], [2, 4]]
[1, 2] | f(sorted, reverse=True) sorted([1, 2], reverse=True) [2, 1]
[1, 2] | f(map, str, X) map([1, 2], str) ['1', '2']

This library only works if each pipeline function has exactly one input and one output. It also requires that the initial input object does not implement the left | operator for pipeline_func.PipelineFunc objects. This should be true for any well-behaved object, though.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pipeline_func-1.0.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

pipeline_func-1.0.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file pipeline_func-1.0.0.tar.gz.

File metadata

  • Download URL: pipeline_func-1.0.0.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pipeline_func-1.0.0.tar.gz
Algorithm Hash digest
SHA256 73d19ea19434c23233eed744bca7215f9c8562da9afa9f82dad71696a13e799a
MD5 949d56678cabbcd6518e4661cd18beb5
BLAKE2b-256 bdc3025d7414ca32ed423ac2b96f37c3047f9343b23280f34a474577280e74d5

See more details on using hashes here.

File details

Details for the file pipeline_func-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pipeline_func-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 09bf80c63e52734d349d2241f14d0432ce47d14ca03b76f924cacd0ba5a16198
MD5 1ccda4826ebca92f9455eca2d616fdb0
BLAKE2b-256 0687d1e43c14588d5f87b9d4870daedbe45e3e3f1ced9737b3add15b0c38a5b3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page