File: partition.py

package info (click to toggle)
python-returns 0.26.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,652 kB
  • sloc: python: 11,000; makefile: 18
file content (36 lines) | stat: -rw-r--r-- 1,109 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
from collections.abc import Iterable
from typing import TypeVar

from returns.interfaces.unwrappable import Unwrappable
from returns.primitives.exceptions import UnwrapFailedError

_ValueType_co = TypeVar('_ValueType_co', covariant=True)
_ErrorType_co = TypeVar('_ErrorType_co', covariant=True)


def partition(
    containers: Iterable[Unwrappable[_ValueType_co, _ErrorType_co],],
) -> tuple[list[_ValueType_co], list[_ErrorType_co]]:
    """
    Partition a list of unwrappables into successful and failed values.

    Preserves order.

    .. code:: python

        >>> from returns.result import Failure, Success
        >>> from returns.methods import partition

        >>> results = [Success(1), Failure(2), Success(3), Failure(4)]
        >>> partition(results)
        ([1, 3], [2, 4])

    """
    successes: list[_ValueType_co] = []
    failures: list[_ErrorType_co] = []
    for container in containers:
        try:
            successes.append(container.unwrap())
        except UnwrapFailedError:  # noqa: PERF203
            failures.append(container.failure())
    return successes, failures