Finish restructuring files
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
All checks were successful
Build Formula10 Docker Image / build-docker (push) Successful in 14s
This commit is contained in:
69
formula10/database/validation_util.py
Normal file
69
formula10/database/validation_util.py
Normal file
@ -0,0 +1,69 @@
|
||||
from typing import Any, Callable, Iterable, List, TypeVar
|
||||
|
||||
_T = TypeVar("_T")
|
||||
|
||||
|
||||
def any_is_none(*args: Any) -> bool:
|
||||
for arg in args:
|
||||
if arg is None:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def positions_are_contiguous(positions: List[str]) -> bool:
|
||||
if len(positions) == 0:
|
||||
return True
|
||||
|
||||
positions_unique = set(positions) # Remove duplicates
|
||||
positions_sorted: List[int] = sorted([int(position) for position in positions_unique])
|
||||
|
||||
# [2, 3, 4, 5]: 2 + 3 == 5
|
||||
return positions_sorted[0] + len(positions_sorted) - 1 == positions_sorted[-1]
|
||||
|
||||
|
||||
def find_first_or_none(predicate: Callable[[_T], bool], iterable: Iterable[_T]) -> _T | None:
|
||||
"""
|
||||
Finds the first element in a sequence matching a predicate.
|
||||
Returns None if no element is found.
|
||||
"""
|
||||
return next(filter(predicate, iterable), None)
|
||||
|
||||
|
||||
def find_multiple(predicate: Callable[[_T], bool], iterable: Iterable[_T], count: int = 0) -> List[_T]:
|
||||
"""
|
||||
Finds <count> elements in a sequence matching a predicate (finds all if <count> is 0).
|
||||
Throws exception if more/fewer elements were found than specified.
|
||||
"""
|
||||
filtered = list(filter(predicate, iterable))
|
||||
|
||||
if count != 0 and len(filtered) != count:
|
||||
raise Exception(f"find_multiple found {len(filtered)} matching elements but expected {count}")
|
||||
|
||||
return filtered
|
||||
|
||||
|
||||
def find_single(predicate: Callable[[_T], bool], iterable: Iterable[_T]) -> _T:
|
||||
"""
|
||||
Find a single element in a sequence matching a predicate.
|
||||
Throws exception if more/less than a single element is found.
|
||||
"""
|
||||
filtered = list(filter(predicate, iterable))
|
||||
|
||||
if len(filtered) != 1:
|
||||
raise Exception(f"find_single found {len(filtered)} matching elements but expected 1")
|
||||
|
||||
return filtered[0]
|
||||
|
||||
|
||||
def find_single_or_none(predicate: Callable[[_T], bool], iterable: Iterable[_T]) -> _T | None:
|
||||
"""
|
||||
Find a single element in a sequence matching a predicate if it exists.
|
||||
Only throws exception if more than a single element is found.
|
||||
"""
|
||||
filtered = list(filter(predicate, iterable))
|
||||
|
||||
if len(filtered) > 1:
|
||||
raise Exception(f"find_single_or_none found {len(list(filtered))} matching elements but expected 0 or 1")
|
||||
|
||||
return filtered[0] if len(filtered) == 1 else None
|
Reference in New Issue
Block a user