Add common.search_source_by_keys method
parent
f8d80ed209
commit
61a6257534
@ -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…
Reference in New Issue