Skip to main content

function-pipes

PyPI License Copy and Paste

Fast, type-hinted python equivalent for R pipes.

This decorator only relies on the standard library, so can just be copied into a project as a single file.

Why is this needed?

Various languages have versions of a 'pipe' syntax, where a value can be passed through a succession of different functions before returning the final value.

This means you can avoid syntax like the below, where the sequence is hard to read (especially if extra arguments are introduced).

a = c(b(a(value)))

In Python, there is not a good built-in way of doing this, and other attempts at a pipe do not play nice with type hinting.

This library has a very simple API, and does the fiddly bits behind the scenes to keep the pipe fast.

The pipe

There is a pipe, function which expects a value and then a list of callables.

from function_pipes import pipe

value = pipe(5, lambda x: x + 2, str)
value == "7"

No special form for extra arguments, small special case for functions that don't return a value

There is no bespoke syntax for passing in extra arguments or moving where the pipe's current value is placed - just use a lambda. This is a well understood approach, that is compatible with type hinting. In the above, value will be recognised as a string, but the x is understood as an int.

There is a small bit of bespoke syntax for when you want to pass something through a function, but that function doesn't return the result to the next function. Here the pipe_bridge function will wrap another function, pass the function into it, and continue onwards. The following will print 7, before passing the value on.

from function_pipes import pipe, pipe_bridge

value = pipe(5, lambda x: x + 2, pipe_bridge(print), str)
value == "7"

Merging functions to use later

There is also a pipeline, which given a set of functions will return a function which a value can be passed into. Where possible based on other hints, this will hint the input and output variable types.

from function_pipes import pipeline

func = pipeline(lambda x: x + 2, str)
func(5) == "7"

Optimising use of pipes

There's work behind the scenes to minimise the overhead of using the pipe, but it is still adding a function call. If you want the readability of the pipe and the speed of the native ugly approach you can use the @fast_pipes decorator. This rewrites the function it is called on to expand out the pipe and any lambdas into the fastest native equivalent.

e.g. These two functions should have equivalent AST trees:

@fast_pipes
def function_that_has_a_pipe(v: int) -> str:
    value = pipe(v, a, lambda x: b(x, foo="other_input"), c)
    return pipe
def function_that_has_a_pipe(v: int) -> str:
    value = c(b(a(v),foo="other_input"))
    return pipe

This version of the function is solving three versions of the same puzzle at the same time:

  • The type hinting is unpacking the structure when it is being written.
  • The pipe function solves the problem in standard python.
  • The fast_pipes decorator is rewriting the AST tree to get the same outcome faster.

But to the user, it all looks the same - pipes!

There is a limit of 20 functions that can be passed to a pipe or pipeline. If you really want to do more, you could chain multiple pipelines together.

Install

You can install from pip: python -m pip install function-pipes

Or you can copy the module directly into your projects.

Development

This project comes with a Dockerfile and devcontainer that should get a good environment set up.

The actual code is generated from src/function_pipes/pipes.jinja-py using jinja to generate the code and the seperate versions with and without use of paramspec.

Use make to regenerate the files. The number of allowed arguments is specified in Makefile.

There is a test suite that does checks for equivalence between this syntax and the raw syntax, as well as checking that fast_pipes and other optimisations are faster.

This can be run with script/test.

Download files

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

Source Distribution

function-pipes-0.1.1.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

function_pipes-0.1.1-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

Details for the file function-pipes-0.1.1.tar.gz.

File metadata

  • Download URL: function-pipes-0.1.1.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.2 Linux/5.13.0-1031-azure

File hashes

Hashes for function-pipes-0.1.1.tar.gz
Algorithm Hash digest
SHA256 96c4a894693f7baf93104a527362265953bf21eb6bc95099ac93fc730d5e7587
MD5 4ef396dcb180df77456b92bec6769630
BLAKE2b-256 2d27cf2dc9fd57af68eebbf445f0d185bcec2ffe521fc96a5786fd13281fcced

See more details on using hashes here.

File details

Details for the file function_pipes-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: function_pipes-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.10.2 Linux/5.13.0-1031-azure

File hashes

Hashes for function_pipes-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 61d7bce7ea3c90056e310156e5834a58ec5b8c092933c662ee91abb0e1115fac
MD5 e6052f31362ce22e4c4e952c657abebe
BLAKE2b-256 439edc30832b9a247e3ec204e5c796b9da8c7077724e7ada51bffd5f6a24c799

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page