2021.10.3 #35
@ -13,6 +13,7 @@ from .._query.first_last import first_or_default_query, first_query, last_or_def
|
|||||||
from .._query.for_each import for_each_query
|
from .._query.for_each import for_each_query
|
||||||
from .._query.max_min import max_query, min_query
|
from .._query.max_min import max_query, min_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
|
||||||
|
from .._query.reverse import reverse_query
|
||||||
from .._query.single import single_query, single_or_default_query
|
from .._query.single import single_query, single_or_default_query
|
||||||
from .._query.where import where_query
|
from .._query.where import where_query
|
||||||
from cpl_query.extension.iterable_abc import IterableABC
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
@ -80,6 +81,9 @@ class Iterable(IterableABC):
|
|||||||
res.__class__ = OrderedIterable
|
res.__class__ = OrderedIterable
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def reverse(self) -> IterableABC:
|
||||||
|
return reverse_query(self)
|
||||||
|
|
||||||
def single(self) -> any:
|
def single(self) -> any:
|
||||||
return single_query(self)
|
return single_query(self)
|
||||||
|
|
||||||
@ -90,6 +94,3 @@ class Iterable(IterableABC):
|
|||||||
res = where_query(self, func)
|
res = where_query(self, func)
|
||||||
res.__class__ = Iterable
|
res.__class__ = Iterable
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def to_list(self) -> list:
|
|
||||||
return list(self)
|
|
||||||
|
15
src/cpl_query/_query/reverse.py
Normal file
15
src/cpl_query/_query/reverse.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument
|
||||||
|
from cpl_query.extension.iterable_abc import IterableABC
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_query(_list: IterableABC) -> IterableABC:
|
||||||
|
if _list is None:
|
||||||
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
|
result = IterableABC()
|
||||||
|
_copied_list = _list.to_list()
|
||||||
|
_copied_list.reverse()
|
||||||
|
for element in _copied_list:
|
||||||
|
result.append(element)
|
||||||
|
|
||||||
|
return result
|
@ -80,9 +80,15 @@ class IterableABC(ABC, list):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def order_by_descending(self, func: Callable) -> 'IterableABC': pass
|
def order_by_descending(self, func: Callable) -> 'IterableABC': pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def reverse(self) -> 'IterableABC': pass
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def single(self) -> any: pass
|
def single(self) -> any: pass
|
||||||
|
|
||||||
|
def to_list(self) -> list:
|
||||||
|
return list(self)
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def single_or_default(self) -> Optional[any]: pass
|
def single_or_default(self) -> Optional[any]: pass
|
||||||
|
|
||||||
|
@ -228,6 +228,13 @@ class QueryTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(res, s_res)
|
self.assertEqual(res, s_res)
|
||||||
|
|
||||||
|
def test_reverse(self):
|
||||||
|
res = self._tests.reverse()
|
||||||
|
l_res = self._tests.to_list()
|
||||||
|
l_res.reverse()
|
||||||
|
|
||||||
|
self.assertEqual(l_res, res)
|
||||||
|
|
||||||
def test_single(self):
|
def test_single(self):
|
||||||
res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr)
|
res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr)
|
||||||
s_res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr).single()
|
s_res = self._tests.where(lambda u: u.address.nr == self._t_user.address.nr).single()
|
||||||
|
Loading…
Reference in New Issue
Block a user