File: pipeline.rst

package info (click to toggle)
python-returns 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (284 lines) | stat: -rw-r--r-- 8,270 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
.. _pipelines:

Pipelines
=========

The main idea behind functional programming is functional composition.

We provide several tools to make
composition easy, readable, pythonic, and useful.

.. note::

  Make sure you are familiar with our :ref:`pointfree` tools,
  because pipelines and pointfree functions are best friends!


flow
----

``flow`` allows to easily compose multiple functions together into a pipeline.
It is useful when you already have an instance to compose functions with.

.. note::

  ``flow`` is the recommended way to write your code with ``returns``!

Let's see an example:

.. code:: python

  >>> from returns.pipeline import flow
  >>> assert flow(
  ...     [1, 2, 3],
  ...     lambda collection: max(collection),
  ...     lambda max_number: -max_number,
  ... ) == -3

This allows you to write declarative steps
that should be performed on an existing value.

.. note::

  Technical note: ``flow`` has the best type inference mechanism
  among all other tools we provide here.
  This happens due to our :ref:`mypy plugins <mypy-plugins>`.

You can also use ``flow`` with pointfree functions and containers:

.. code:: python

  >>> from returns.result import Result, Success, Failure
  >>> from returns.pointfree import bind
  >>> from returns.pipeline import flow

  >>> def regular_function(arg: int) -> float:
  ...     return float(arg)

  >>> def returns_container(arg: float) -> Result[str, ValueError]:
  ...     if arg != 0:
  ...         return Success(str(arg))
  ...     return Failure(ValueError('Wrong arg'))

  >>> def also_returns_container(arg: str) -> Result[str, ValueError]:
  ...     return Success(arg + '!')

  >>> assert flow(
  ...     1,  # initial value
  ...     regular_function,  # composes easily
  ...     returns_container,  # also composes easily, but returns a container
  ...     # So we need to `bind` the next function to allow it to consume
  ...     # the container from the previous step.
  ...     bind(also_returns_container),
  ... ) == Success('1.0!')

  >>> # And this will fail:
  >>> assert flow(
  ...     0,  # initial value
  ...     regular_function,  # composes easily
  ...     returns_container,  # also composes easily, but returns a container
  ...     # So we need to `bind` the next function to allow it to consume
  ...     # the container from the previous step.
  ...     bind(also_returns_container),
  ... ).failure().args == ('Wrong arg', )

And now let's get to know ``pipe``, it is very similar,
but has different usage pattern.


.. _pipe:

pipe
----

``pipe`` is an easy way to compose functions together.
It is useful when you don't have an instance to compose functions with yet.

.. note::

  ``pipe`` requires to use our :ref:`mypy plugins <mypy-plugins>`.

Let's see an example.

.. code:: python

  >>> from returns.pipeline import pipe

  >>> pipeline = pipe(str, lambda x: x + 'b', str.upper)
  >>> assert pipeline(1) == '1B'

It might be later used with multiple values:

.. code:: python

  >>> assert pipeline(2) == '2B'

It also might be useful to compose containers together:

.. code:: python

  >>> from returns.pipeline import pipe
  >>> from returns.result import Result, Success, Failure
  >>> from returns.pointfree import bind

  >>> def regular_function(arg: int) -> float:
  ...     return float(arg)

  >>> def returns_container(arg: float) -> Result[str, ValueError]:
  ...     if arg != 0:
  ...         return Success(str(arg))
  ...     return Failure(ValueError('Wrong arg'))

  >>> def also_returns_container(arg: str) -> Result[str, ValueError]:
  ...     return Success(arg + '!')

  >>> transaction = pipe(
  ...     regular_function,  # composes easily
  ...     returns_container,  # also composes easily, but returns a container
  ...     # So we need to `bind` the next function to allow it to consume
  ...     # the container from the previous step.
  ...     bind(also_returns_container),
  ... )
  >>> result = transaction(1)  # running the pipeline
  >>> assert result == Success('1.0!')

You might consider ``pipe()`` as :func:`returns.functions.compose` on steroids.
The main difference is that ``compose`` takes strictly two arguments
(or you might say that it has an arity of two),
while ``pipe`` has infinite possible arguments.


managed
-------

A really common task is to work with something stateful,
like database connections or files.

First, you need to acquire some resource,
then use it and do your thing,
and clear things up and release the acquired resource.

There are several rules here:

1. If the acquiring failed,
   then do nothing: do not try to use the resource or release it
2. If the resource is acquired, then try to use it
   and then release it despite of the usage result

In other words, if you cannot open a file, then do nothing.
If you opened it, then try to read it. And then always close it.

Let's say you have to read a file's contents:

.. code:: python

  >>> from typing import TextIO
  >>> from returns.pipeline import managed, is_successful
  >>> from returns.result import ResultE
  >>> from returns.io import IOResultE, impure_safe

  >>> def read_file(file_obj: TextIO) -> IOResultE[str]:
  ...     return impure_safe(file_obj.read)()  # this will be the final result

  >>> def close_file(
  ...     file_obj: TextIO,
  ...     file_contents: ResultE[str],
  ... ) -> IOResultE[None]:  # sometimes might require to use `untap`
  ...     return impure_safe(file_obj.close)()  # this value will be dropped

  >>> managed_read = managed(read_file, close_file)

  >>> read_result = managed_read(
  ...     impure_safe(lambda filename: open(filename, 'r'))('pyproject.toml'),
  ... )
  >>> assert is_successful(read_result)  # file content is inside `IOSuccess`

And here's how we recommend to combine
``managed`` with other pipeline functions:

.. code:: python

  >>> import tomlkit
  >>> from returns.pipeline import flow
  >>> from returns.pointfree import bind_result
  >>> from returns.result import safe
  >>> from returns.io import IOSuccess

  >>> @safe
  ... def parse_toml(file_contents: str) -> dict:
  ...     return tomlkit.parse(file_contents)

  >>> @safe
  ... def get_project_name(parsed: dict) -> str:
  ...     return parsed['tool']['poetry']['name']

  >>> pipeline_result = flow(
  ...     'pyproject.toml',  # filename we work with
  ...     impure_safe(lambda filename: open(filename, 'r')),
  ...     managed_read,
  ...     bind_result(parse_toml),
  ...     bind_result(get_project_name),
  ... )
  >>> assert pipeline_result == IOSuccess('returns')

Notice a few tricks here:

1. We use ``managed`` with and without ``flow`` here,
   both are fine!
2. We have created a ``managed_read`` managed function,
   so we don't need to specify it every time we want
   to read a file in a functional way
3. We are using impure and pure operations inside the pipeline:
   this helps us to understand how our app works.
   Which parts do access the file system and which just work

However, you can still use the imperative approach
with ``with:`` or ``try/finally`` wrapped into ``@impure_safe`` decorator,
your choice! We don't recommend to mix these two.
Stick to one you like the most.

``managed`` can be used with:

- ``IOResult``
- ``FutureResult``
- ``RequiresContextIOResult``
- ``RequiresContextFutureResult``


is_successful
-------------

:func:`is_successful <returns.functions.is_successful>` is used to
tell whether or not your result is a success.
We treat only three types that do not throw as successful ones,
basically: :func:`Success <returns.result.Success>`,
:func:`IOSuccess <returns.io.IOSuccess>`,
and :func:`Some <returns.maybe.Some>`

.. code:: python

  >>> from returns.result import Success, Failure
  >>> from returns.pipeline import is_successful

  >>> assert is_successful(Success(1)) is True
  >>> assert is_successful(Failure('text')) is False


Further reading
---------------

- `fp-ts pipeable <https://github.com/gcanti/fp-ts/blob/master/src/pipeable.ts>`_
- `ZIO Managed <https://zio.dev/docs/datatypes/datatypes_managed>`_


API Reference
-------------

.. autofunction:: returns.pipeline.flow

.. autofunction:: returns.pipeline.pipe

.. autofunction:: returns.pipeline.managed

.. automodule:: returns.pipeline
   :members: