sh_cpl/src/cpl_query/_query/single.py

29 lines
746 B
Python
Raw Normal View History

2021-07-25 19:15:02 +02:00
from typing import Optional
2021-07-27 09:41:51 +02:00
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument
2021-07-25 19:15:02 +02:00
from cpl_query.extension.iterable_abc import IterableABC
def single_query(_list: IterableABC) -> any:
2021-07-27 09:41:51 +02:00
if _list is None:
raise ArgumentNoneException(ExceptionArgument.list)
2021-07-25 19:15:02 +02:00
if len(_list) > 1:
raise Exception('Found more than one element')
elif len(_list) == 0:
raise Exception('Found no element')
return _list[0]
def single_or_default_query(_list: IterableABC) -> Optional[any]:
2021-07-27 09:41:51 +02:00
if _list is None:
raise ArgumentNoneException(ExceptionArgument.list)
2021-07-25 19:15:02 +02:00
if len(_list) > 1:
raise Exception('Index out of range')
elif len(_list) == 0:
return None
return _list[0]