Skip to main content

piping between functions in Python

Project description

https://img.shields.io/circleci/project/github/brettbeatty/lead_pipe.svg https://img.shields.io/github/license/brettbeatty/lead_pipe.svg https://img.shields.io/codecov/c/github/brettbeatty/lead_pipe.svg https://img.shields.io/pypi/v/lead_pipe.svg

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(3)
>>> Pipe('a')
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)
Pipe(5)

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)
11
>>> Pipe('foo').value
'foo'
>>> ~Pipe(2)(pow, 3)(str)
'8'

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]
4
>>> ~p(lambda x: x[1][1])
3

Apply

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'

Reflect

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')
1

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

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

lead_pipe-0.2.1.tar.gz (3.3 kB view hashes)

Uploaded Source

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