betty.functools module¶
Provide functional programming utilities.
- class betty.functools.Do[source]¶
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)[source]¶
- 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 [source]¶
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[source]¶
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 thanset
.- Parameters:
values (
typing.Iterable
[typing.TypeVar
(T
)])
- __init__(*values: Iterable[betty.functools.T])[source]¶
- 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] [source]¶
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:
- betty.functools.slice_to_range(indices: slice, iterable: Sized) Iterable[int] [source]¶
Apply a slice to an iterable, and return the corresponding range.
- Parameters:
indices (
slice
)iterable (
typing.Sized
)
- Return type: