Added element at query

This commit is contained in:
Sven Heidemann 2021-07-27 11:29:52 +02:00
parent 6951a19670
commit a3a0a150a8
5 changed files with 48 additions and 0 deletions

View File

@ -8,6 +8,7 @@ from .._query.avg import avg_query
from .._query.contains import contains_query from .._query.contains import contains_query
from .._query.count import count_query from .._query.count import count_query
from .._query.distinct import distinct_query from .._query.distinct import distinct_query
from .._query.element_at import element_at_query, element_at_or_default_query
from .._query.first import first_or_default_query, first_query from .._query.first import first_or_default_query, first_query
from .._query.for_each import for_each_query from .._query.for_each import for_each_query
from .._query.order_by import order_by_query, order_by_descending_query from .._query.order_by import order_by_query, order_by_descending_query
@ -41,6 +42,12 @@ class Iterable(IterableABC):
res.__class__ = Iterable res.__class__ = Iterable
return res return res
def element_at(self, index: int) -> any:
return element_at_query(self, index)
def element_at_or_default(self, index: int) -> Optional[any]:
return element_at_or_default_query(self, index)
def first(self) -> any: def first(self) -> any:
return first_query(self) return first_query(self)

View File

@ -0,0 +1,25 @@
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument
from cpl_query.extension.iterable_abc import IterableABC
def element_at_query(_list: IterableABC, _index: int) -> any:
if _list is None:
raise ArgumentNoneException(ExceptionArgument.list)
if _index is None:
raise ArgumentNoneException(ExceptionArgument.index)
return _list[_index]
def element_at_or_default_query(_list: IterableABC, _index: int) -> any:
if _list is None:
raise ArgumentNoneException(ExceptionArgument.list)
if _index is None:
raise ArgumentNoneException(ExceptionArgument.index)
try:
return _list[_index]
except IndexError:
return None

View File

@ -7,6 +7,7 @@ class ExceptionArgument(Enum):
func = 'func' func = 'func'
type = 'type' type = 'type'
value = 'value' value = 'value'
index = 'index'
# exceptions # exceptions

View File

@ -26,6 +26,12 @@ class IterableABC(ABC, list):
@abstractmethod @abstractmethod
def distinct(self, func: Callable) -> 'IterableABC': pass def distinct(self, func: Callable) -> 'IterableABC': pass
@abstractmethod
def element_at(self, index: int) -> any: pass
@abstractmethod
def element_at_or_default(self, index: int) -> Optional[any]: pass
@abstractmethod @abstractmethod
def first(self) -> any: pass def first(self) -> any: pass

View File

@ -91,6 +91,15 @@ class QueryTest(unittest.TestCase):
res = self._tests.distinct(lambda u: u.address.nr).where(lambda u: u.address.nr == 5) res = self._tests.distinct(lambda u: u.address.nr).where(lambda u: u.address.nr == 5)
self.assertEqual(1, len(res)) self.assertEqual(1, len(res))
def test_element_at(self):
index = randint(0, len(self._tests) - 1)
self.assertEqual(self._tests[index], self._tests.element_at(index))
def test_element_at_or_default(self):
index = randint(0, len(self._tests) - 1)
self.assertEqual(self._tests[index], self._tests.element_at_or_default(index))
self.assertIsNone(self._tests.element_at_or_default(len(self._tests)))
def test_first(self): def test_first(self):
results = [] results = []
for user in self._tests: for user in self._tests: