2021.10.3 #35

Merged
edraft merged 31 commits from 2021.10.3 into 2021.10 2021-08-02 14:36:46 +02:00
5 changed files with 48 additions and 0 deletions
Showing only changes of commit a3a0a150a8 - Show all commits

View File

@ -8,6 +8,7 @@ from .._query.avg import avg_query
from .._query.contains import contains_query
from .._query.count import count_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.for_each import for_each_query
from .._query.order_by import order_by_query, order_by_descending_query
@ -41,6 +42,12 @@ class Iterable(IterableABC):
res.__class__ = Iterable
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:
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'
type = 'type'
value = 'value'
index = 'index'
# exceptions

View File

@ -26,6 +26,12 @@ class IterableABC(ABC, list):
@abstractmethod
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
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)
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):
results = []
for user in self._tests: