Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

piping between functions in Python

Project Description

Lead Pipe allows values to be piped from one function to the next without nesting the calls. For example, the following blocks of code are equivalent.

a = foo(bar(baz(8, 2), a=3), 4)
from lead_pipe import Pipe
a = ~Pipe(8)(baz, 2)(bar, a=3)(foo, 4)

Basic Use

Creating a Pipe

Pipelines begin with a base value passed to the Pipe constructor.

>>> from lead_pipe import Pipe
>>> Pipe(3)
>>> Pipe('a')

Piping Results

Each instance of Pipe is callable and takes any number of arguments (at least one). The first argument is a function that gets called with the pipeline’s value followed by the additional arguments and keyword arguments, if any.

>>> Pipe(3)(lambda x: x + 0.5)(lambda x: x ** 2)(int)('{} - {x} + {}'.format, 1, x=8)(eval)

Obtaining Result

Once your pipeline is finished, you can retrieve the result with the tilde (~) operator or through the pipe’s ‘value’ attribute.

>>> ~Pipe(11)
>>> Pipe('foo').value
>>> ~Pipe(2)(pow, 3)(str)

Advanced Features

Intermediate Pipes

Since each step along the pipeline is its own instance of Pipe, an intermediate pipe can be saved to pipe to multiple functions.

>>> p = Pipe(4)(range)(zip, range(2, 6))
>>> p
Pipe([(0, 2), (1, 3), (2, 4), (3, 5)])
>>> p(dict).value[2]
>>> ~p(lambda x: x[1][1])


Sometimes a function may return another function rather than a value to be piped to another function. Apply is a helper function that continues the pipeline with the function returned.

>>> from lead_pipe import apply
>>> ~Pipe('{} foo{a} {}'.format)(apply, 'bar', 'baz', a=3)
'bar foo3 baz'


Sometimes one may want to call a member function of a value in the pipeline. One way would be to pipe the value to getattr then to apply, but the reflect function is the combination of the two.

>>> from lead_pipe import reflect
>>> ~Pipe({'a': 1, 'b': 2})(reflect, 'get', 'a')

In this specific example, one could pipe the dictionary to dict.get, but reflect is more general.

Release History

This version
History Node


History Node


History Node


Download Files

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

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(3.3 kB) Copy SHA256 Hash SHA256
Source None Dec 18, 2017

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Kabu Creative Kabu Creative UX & Design Google Google Cloud Servers Fastly Fastly CDN StatusPage StatusPage Statuspage DigiCert DigiCert EV Certificate