Skip to main content
Help us improve PyPI by participating in user testing. All experience levels needed!

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(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


Release history Release notifications

This version
History Node

0.2.1

History Node

0.2

History Node

0.1

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
lead_pipe-0.2.1.tar.gz (3.3 kB) Copy SHA256 hash SHA256 Source None Dec 18, 2017

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page