betty.functools module - Betty 0.4.0a3 documentation

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# betty.functools module

Provide functional programming utilities.

class betty.functools.Do

  Bases: `Generic`[`_DoFP`, `_DoFReturnT`]

  A functional implementation of do-while functionality, with retries and timeouts.

  Parameters:

    - do (`typing.Callable`[[`typing.ParamSpec`(`_DoFP`, bound= `None`)], `typing.Union`[`typing.TypeVar`(`_DoFReturnT`), `typing.Awaitable`[`typing.TypeVar`(`_DoFReturnT`)]]])

    - do_args (`typing.ParamSpecArgs`)

    - do_kwargs (`typing.ParamSpecKwargs`)

  __init__(do: typing.Callable[[~_DoFP], betty.functools._DoFReturnT | typing.Awaitable[betty.functools._DoFReturnT]], *do_args: typing.~_DoFP, **do_kwargs: typing.~_DoFP)
    Parameters:

      - do (`typing.Callable`[[`typing.ParamSpec`(`_DoFP`, bound= `None`)], `typing.Union`[`typing.TypeVar`(`_DoFReturnT`), `typing.Awaitable`[`typing.TypeVar`(`_DoFReturnT`)]]])

      - do_args (`typing.ParamSpecArgs`)

      - do_kwargs (`typing.ParamSpecKwargs`)

  async until(*conditions: Callable[[betty.functools._DoFReturnT], None | bool | Awaitable[None | bool]], retries: int = 5, timeout: int = 300, interval: int | float = 0.1) → betty.functools._DoFReturnT

    Perform the ‘do’ until it succeeds or as long as the given arguments allow.

    Parameters:

      - timeout (`int`) – The timeout in seconds.

      - interval (`int` | `float`) – The interval between ‘loops’ in seconds.

      - conditions (`typing.Callable`[[`typing.TypeVar`(`_DoFReturnT`)], `typing.Union`[`None`, `bool`, `typing.Awaitable`[`None` | `bool`]]])

      - retries (`int`)

    Return type:

      `typing.TypeVar`(`_DoFReturnT`)

class betty.functools.Uniquifier

  Bases: `Generic`[`_T`]

  Yield the first occurrences of values in a sequence.

  For the purpose of filtering duplicate values from an iterable, this works similar to `set`, except that this class supports non-hashable values. It is therefore slightly slower than `set`.

  Parameters:

    values (`typing.Iterable`[`typing.TypeVar`(`_T`)])

  __init__(*values: Iterable[betty.functools._T])
    Parameters:

      values (`typing.Iterable`[`typing.TypeVar`(`_T`)])

betty.functools.filter_suppress(raising_filter: Callable[[betty.functools._T], Any], exception_type: type[BaseException], items: Iterable[betty.functools._T]) → Iterator[betty.functools._T]

  Filter values, skipping those for which the application of raising_filter raises errors.

  Parameters:

    - raising_filter (`typing.Callable`[[`typing.TypeVar`(`_T`)], `typing.Any`])

    - exception_type (`type`[`BaseException`])

    - items (`typing.Iterable`[`typing.TypeVar`(`_T`)])

  Return type:

    `typing.Iterator`[`typing.TypeVar`(`_T`)]

betty.functools.slice_to_range(indices: slice, iterable: Sized) → Iterable[int]

  Apply a slice to an iterable, and return the corresponding range.

  Parameters:

    - indices (`slice`)

    - iterable (`typing.Sized`)

  Return type:

    `typing.Iterable`[`int`]