Add common.search_source_by_keys method

master
Felix Stupp 10 months ago
parent f8d80ed209
commit 61a6257534
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -8,6 +8,9 @@ from ._itertools import (
iter_lookahead,
limit_iter,
)
from ._search import (
search_source_by_keys,
)
from ._setting_handler import (
update_bool_value,
)
@ -26,6 +29,7 @@ __all__ = [
"fix_iter",
"iter_lookahead",
"limit_iter",
"search_source_by_keys",
"to_just_number",
"trim",
"update_bool_value",

@ -0,0 +1,24 @@
from typing import (
Callable,
Optional,
Sequence,
TypeVar,
)
T = TypeVar("T")
K = TypeVar("K")
def search_source_by_keys(
source: Callable[[K], Optional[T]],
all_keys: Sequence[K],
) -> Optional[T]:
"""
tries <all_keys> in <source> and returns first result `!= None`, otherwise `None`
"""
for key in all_keys:
item = source(key)
if item is not None:
return item
return None
Loading…
Cancel
Save