2022.10.2 #101

Merged
edraft merged 5 commits from 2022.10.2 into 2022.10 2022-09-12 22:29:23 +02:00
2 changed files with 52 additions and 3 deletions
Showing only changes of commit a528511732 - Show all commits

View File

@ -158,7 +158,7 @@ class IterableABC(ABC, list):
""" """
pass pass
def extend(self, __iterable: Iterable) -> None: def extend(self, __iterable: Iterable) -> 'IterableABC':
r"""Adds elements of given list to list r"""Adds elements of given list to list
Parameter Parameter
@ -169,6 +169,8 @@ class IterableABC(ABC, list):
for value in __iterable: for value in __iterable:
self.append(value) self.append(value)
return self
@abstractmethod @abstractmethod
def last(self) -> any: def last(self) -> any:
r"""Returns last element r"""Returns last element
@ -290,6 +292,24 @@ class IterableABC(ABC, list):
""" """
pass pass
def select(self, _f: Callable) -> 'IterableABC':
r"""Formats each element of list to a given format
Returns
-------
:class: `cpl_query.extension.iterable_abc.IterableABC`
"""
pass
def select_many(self, _f: Callable) -> 'IterableABC':
r"""Flattens resulting lists to one
Returns
-------
:class: `cpl_query.extension.iterable_abc.IterableABC`
"""
pass
@abstractmethod @abstractmethod
def single(self) -> any: def single(self) -> any:
r"""Returns one single element of list r"""Returns one single element of list

View File

@ -161,9 +161,9 @@ class QueryTestCase(unittest.TestCase):
def test_for_each(self): def test_for_each(self):
users = [] users = []
self._tests.for_each(lambda user: ( self._tests.for_each(lambda user: (
users.append(user) users.append(user)
)
) )
)
self.assertEqual(len(users), len(self._tests)) self.assertEqual(len(users), len(self._tests))
@ -239,6 +239,35 @@ class QueryTestCase(unittest.TestCase):
self.assertEqual(l_res, res) self.assertEqual(l_res, res)
def test_select(self):
range_list = List(int, range(0, 100))
selected_range = range_list.select(lambda x: x + 1)
modulo_range = []
for x in range(0, 100):
if x % 2 == 0:
modulo_range.append(x)
self.assertEqual(selected_range.to_list(), list(range(1, 101)))
self.assertEqual(range_list.where(lambda x: x % 2 == 0).to_list(), modulo_range)
def test_select_many(self):
range_list = List(int, range(0, 100))
selected_range = range_list.select(lambda x: [x, x])
self.assertEqual(selected_range, [[x, x] for x in range(0, 100)])
self.assertEqual(selected_range.select_many(lambda x: x).to_list(), [_x for _l in [2 * [x] for x in range(0, 100)] for _x in _l])
class TestClass:
def __init__(self, i, is_sub=False):
self.i = i
if is_sub:
return
self.elements = [TestClass(x, True) for x in range(0, 10)]
elements = List(TestClass, [TestClass(i) for i in range(0, 100)])
selected_elements = elements.select_many(lambda x: x.elements).select(lambda x: x.i)
self.assertEqual(selected_elements.where(lambda x: x == 0).count(), 100)
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()