Skip to content

fp

Bad

Bases: Generic[B, G]

The bad side of the disjoint union.

Source code in m/core/fp.py
class Bad(Generic[B, G]):
    """The bad side of the disjoint union."""

    value: B
    is_bad = True

    def __init__(self, value: B):
        """Initialize a `Bad` instance.

        Args:
            value: The "bad" value to store in the instance.
        """
        self.value = value

    def __iter__(self) -> Iterator[G]:
        """Iterate over values of an instance.

        The intention is to raise a `StopBadIteration` exception to
        be able to break out of for loop comprehensions.

        Raises:
            StopBadIteration: If the instance has a "Bad" value.
        """
        raise StopBadIteration(self)

    def iter(self) -> Iterator[G]:
        """Shortcut to transform to a list.

        Can be used as `list(x.iter())`. It will either contain a value or be
        an empty list.

        Yields:
            The value if the instance is a "Good" value.
        """
        empty = ()
        yield from empty

    def get_or_else(self, default: LazyArg[G]) -> G:
        """Return the value if its Good or the given argument if its a Bad.

        Args:
            default: The default value in case the instance is "Bad".

        Returns:
            Either the value or the default specified by "default".
        """
        return lazy_arg(default)

    def map(self, _fct: Callable[[G], K]) -> 'Bad[B, K]':
        """Apply the function to its value if this is a `Good` instance.

        Args:
            _fct: The function to apply to the "Good" value.

        Returns:
            Itself if its a `Bad` otherwise another instance of `Good`.
        """
        return cast(Bad[B, K], self)

    def flat_map_bad(self, fct: Callable[[B], OneOf[B, G]]) -> OneOf[B, G]:
        """Apply the input function if this is a `Bad` value.

        Args:
            fct: The function to apply to the "Bad" value.

        Returns:
            Itself if its a `Good` otherwise another instance of `Bad`.
        """
        return fct(self.value)

__init__(value)

Initialize a Bad instance.

Parameters:

Name Type Description Default
value B

The "bad" value to store in the instance.

required
Source code in m/core/fp.py
def __init__(self, value: B):
    """Initialize a `Bad` instance.

    Args:
        value: The "bad" value to store in the instance.
    """
    self.value = value

__iter__()

Iterate over values of an instance.

The intention is to raise a StopBadIteration exception to be able to break out of for loop comprehensions.

Raises:

Type Description
StopBadIteration

If the instance has a "Bad" value.

Source code in m/core/fp.py
def __iter__(self) -> Iterator[G]:
    """Iterate over values of an instance.

    The intention is to raise a `StopBadIteration` exception to
    be able to break out of for loop comprehensions.

    Raises:
        StopBadIteration: If the instance has a "Bad" value.
    """
    raise StopBadIteration(self)

flat_map_bad(fct)

Apply the input function if this is a Bad value.

Parameters:

Name Type Description Default
fct Callable[[B], OneOf[B, G]]

The function to apply to the "Bad" value.

required

Returns:

Type Description
OneOf[B, G]

Itself if its a Good otherwise another instance of Bad.

Source code in m/core/fp.py
def flat_map_bad(self, fct: Callable[[B], OneOf[B, G]]) -> OneOf[B, G]:
    """Apply the input function if this is a `Bad` value.

    Args:
        fct: The function to apply to the "Bad" value.

    Returns:
        Itself if its a `Good` otherwise another instance of `Bad`.
    """
    return fct(self.value)

get_or_else(default)

Return the value if its Good or the given argument if its a Bad.

Parameters:

Name Type Description Default
default LazyArg[G]

The default value in case the instance is "Bad".

required

Returns:

Type Description
G

Either the value or the default specified by "default".

Source code in m/core/fp.py
def get_or_else(self, default: LazyArg[G]) -> G:
    """Return the value if its Good or the given argument if its a Bad.

    Args:
        default: The default value in case the instance is "Bad".

    Returns:
        Either the value or the default specified by "default".
    """
    return lazy_arg(default)

iter()

Shortcut to transform to a list.

Can be used as list(x.iter()). It will either contain a value or be an empty list.

Yields:

Type Description
G

The value if the instance is a "Good" value.

Source code in m/core/fp.py
def iter(self) -> Iterator[G]:
    """Shortcut to transform to a list.

    Can be used as `list(x.iter())`. It will either contain a value or be
    an empty list.

    Yields:
        The value if the instance is a "Good" value.
    """
    empty = ()
    yield from empty

map(_fct)

Apply the function to its value if this is a Good instance.

Parameters:

Name Type Description Default
_fct Callable[[G], K]

The function to apply to the "Good" value.

required

Returns:

Type Description
'Bad[B, K]'

Itself if its a Bad otherwise another instance of Good.

Source code in m/core/fp.py
def map(self, _fct: Callable[[G], K]) -> 'Bad[B, K]':
    """Apply the function to its value if this is a `Good` instance.

    Args:
        _fct: The function to apply to the "Good" value.

    Returns:
        Itself if its a `Bad` otherwise another instance of `Good`.
    """
    return cast(Bad[B, K], self)

Good

Bases: Generic[B, G]

The good side of the disjoint union.

Source code in m/core/fp.py
class Good(Generic[B, G]):
    """The good side of the disjoint union."""

    value: G
    is_bad = False

    def __init__(self, value: G):
        """Initialize a `Good` instance.

        Args:
            value: The "good" value to store in the instance.
        """
        self.value = value

    def __iter__(self) -> Iterator[G]:
        """Iterate over the value of the instance.

        Yields:
            The value of the instance.
        """
        yield self.value

    def iter(self) -> Iterator[G]:
        """Shortcut to transform to a list.

        Can be used as `list(x.iter())`. It will either contain a value or be
        an empty list.

        Yields:
            The value if the instance is a "Good" value.
        """
        if not self.is_bad:
            yield self.value

    def get_or_else(self, _default: LazyArg[G]) -> G:
        """Return the value.

        Args:
            _default: The default value in case the instance is "Bad".

        Returns:
            Either the value or the default specified by "default".
        """
        return self.value

    def map(self, fct: Callable[[G], K]) -> 'Good[B, K]':
        """Apply the function to its value if this is a `Good` instance.

        Args:
            fct: The function to apply to the "Good" value.

        Returns:
            Itself if its a `Bad` otherwise another instance of `Good`.
        """
        return Good(fct(self.value))

    def flat_map_bad(self, _fct: Callable[[B], OneOf[B, G]]) -> OneOf[B, G]:
        """Apply the input function if this is a `Bad` value.

        Args:
            _fct: The function to apply to the "Bad" value.

        Returns:
            Itself if its a `Good` otherwise another instance of `Bad`.
        """
        return self

__init__(value)

Initialize a Good instance.

Parameters:

Name Type Description Default
value G

The "good" value to store in the instance.

required
Source code in m/core/fp.py
def __init__(self, value: G):
    """Initialize a `Good` instance.

    Args:
        value: The "good" value to store in the instance.
    """
    self.value = value

__iter__()

Iterate over the value of the instance.

Yields:

Type Description
G

The value of the instance.

Source code in m/core/fp.py
def __iter__(self) -> Iterator[G]:
    """Iterate over the value of the instance.

    Yields:
        The value of the instance.
    """
    yield self.value

flat_map_bad(_fct)

Apply the input function if this is a Bad value.

Parameters:

Name Type Description Default
_fct Callable[[B], OneOf[B, G]]

The function to apply to the "Bad" value.

required

Returns:

Type Description
OneOf[B, G]

Itself if its a Good otherwise another instance of Bad.

Source code in m/core/fp.py
def flat_map_bad(self, _fct: Callable[[B], OneOf[B, G]]) -> OneOf[B, G]:
    """Apply the input function if this is a `Bad` value.

    Args:
        _fct: The function to apply to the "Bad" value.

    Returns:
        Itself if its a `Good` otherwise another instance of `Bad`.
    """
    return self

get_or_else(_default)

Return the value.

Parameters:

Name Type Description Default
_default LazyArg[G]

The default value in case the instance is "Bad".

required

Returns:

Type Description
G

Either the value or the default specified by "default".

Source code in m/core/fp.py
def get_or_else(self, _default: LazyArg[G]) -> G:
    """Return the value.

    Args:
        _default: The default value in case the instance is "Bad".

    Returns:
        Either the value or the default specified by "default".
    """
    return self.value

iter()

Shortcut to transform to a list.

Can be used as list(x.iter()). It will either contain a value or be an empty list.

Yields:

Type Description
G

The value if the instance is a "Good" value.

Source code in m/core/fp.py
def iter(self) -> Iterator[G]:
    """Shortcut to transform to a list.

    Can be used as `list(x.iter())`. It will either contain a value or be
    an empty list.

    Yields:
        The value if the instance is a "Good" value.
    """
    if not self.is_bad:
        yield self.value

map(fct)

Apply the function to its value if this is a Good instance.

Parameters:

Name Type Description Default
fct Callable[[G], K]

The function to apply to the "Good" value.

required

Returns:

Type Description
'Good[B, K]'

Itself if its a Bad otherwise another instance of Good.

Source code in m/core/fp.py
def map(self, fct: Callable[[G], K]) -> 'Good[B, K]':
    """Apply the function to its value if this is a `Good` instance.

    Args:
        fct: The function to apply to the "Good" value.

    Returns:
        Itself if its a `Bad` otherwise another instance of `Good`.
    """
    return Good(fct(self.value))

StopBadIteration

Bases: Exception

Store a Bad instance.

Source code in m/core/fp.py
class StopBadIteration(Exception):  # noqa: N818 - This is for internal use
    """Store a `Bad` instance."""

    def __init__(self, bad: Any):
        """Initialize the Exception.

        Args:
            bad: The `Bad` value stored in a `OneOf`.
        """
        super().__init__()
        self.bad = bad

__init__(bad)

Initialize the Exception.

Parameters:

Name Type Description Default
bad Any

The Bad value stored in a OneOf.

required
Source code in m/core/fp.py
def __init__(self, bad: Any):
    """Initialize the Exception.

    Args:
        bad: The `Bad` value stored in a `OneOf`.
    """
    super().__init__()
    self.bad = bad

is_bad(inst)

Assert that a OneOf instance is a Bad.

Parameters:

Name Type Description Default
inst OneOf[B, G]

The OneOf instance.

required

Returns:

Type Description
TypeGuard[Bad[B, G]]

True if the instance is a Bad.

Source code in m/core/fp.py
@typing_extensions.deprecated(
    'The `is_bad` type guard is deprecated; use `isinstance(inst, Bad)` instead.',
)
def is_bad(inst: OneOf[B, G]) -> TypeGuard[Bad[B, G]]:
    """Assert that a OneOf instance is a `Bad`.

    Args:
        inst: The `OneOf` instance.

    Returns:
        True if the instance is a `Bad`.
    """
    # `m` does not reference this function anymore, excluding from coverage
    return isinstance(inst, Bad)  # pragma: no cover

is_good(inst)

Assert that a OneOf instance is a Good.

Parameters:

Name Type Description Default
inst OneOf[B, G]

The OneOf instance.

required

Returns:

Type Description
TypeGuard[Good[B, G]]

True if the instance is a Good.

Source code in m/core/fp.py
@typing_extensions.deprecated(
    'The `is_good` type guard is deprecated; use `isinstance(inst, Good)` instead.',
)
def is_good(inst: OneOf[B, G]) -> TypeGuard[Good[B, G]]:
    """Assert that a OneOf instance is a `Good`.

    Args:
        inst: The `OneOf` instance.

    Returns:
        True if the instance is a `Good`.
    """
    # `m` does not reference this function anymore, excluding from coverage
    return isinstance(inst, Good)  # pragma: no cover

lazy_arg(z_arg)

Return the output of calling z_arg if it is function.

Otherwise param is returned.

Parameters:

Name Type Description Default
z_arg LazyArg[A]

A function or a value

required

Returns:

Type Description
A

The value.

Source code in m/core/fp.py
def lazy_arg(z_arg: LazyArg[A]) -> A:
    """Return the output of calling `z_arg` if it is function.

    Otherwise param is returned.

    Args:
        z_arg: A function or a value

    Returns:
        The value.
    """
    return z_arg() if callable(z_arg) else z_arg