From 0f85d4b9bce9019abe2897b646243c1ad512b33e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 27 Jul 2021 09:15:19 +0200 Subject: [PATCH] Refactoring & added all query --- src/cpl_query/_extension/iterable.py | 4 ++++ src/cpl_query/_query/all_query.py | 9 +++++++++ src/cpl_query/_query/where_query.py | 5 ----- src/cpl_query/extension/iterable_abc.py | 3 +++ src/cpl_query/tests/query_test.py | 12 ++++++++++++ 5 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/cpl_query/_query/all_query.py diff --git a/src/cpl_query/_extension/iterable.py b/src/cpl_query/_extension/iterable.py index 690a8cd3..2db2c507 100644 --- a/src/cpl_query/_extension/iterable.py +++ b/src/cpl_query/_extension/iterable.py @@ -2,6 +2,7 @@ from typing import Optional, Callable from cpl_query._extension.ordered_iterable import OrderedIterable from cpl_query.extension.ordered_iterable_abc import OrderedIterableABC +from .._query.all_query import all_query from .._query.any_query import any_query from .._query.first_query import first_or_default_query, first_query from .._query.for_each_query import for_each_query @@ -19,6 +20,9 @@ class Iterable(IterableABC): def any(self, func: Callable) -> bool: return any_query(self, func) + def all(self, func: Callable) -> bool: + return all_query(self, func) + def first(self) -> any: return first_query(self) diff --git a/src/cpl_query/_query/all_query.py b/src/cpl_query/_query/all_query.py new file mode 100644 index 00000000..33c9e4c6 --- /dev/null +++ b/src/cpl_query/_query/all_query.py @@ -0,0 +1,9 @@ +from collections import Callable + +from cpl_query._query.where_query import where_query +from cpl_query.extension.iterable_abc import IterableABC + + +def all_query(_list: IterableABC, _func: Callable) -> bool: + result = where_query(_list, _func) + return len(result) == len(_list) diff --git a/src/cpl_query/_query/where_query.py b/src/cpl_query/_query/where_query.py index 3c73b5b2..92593fdd 100644 --- a/src/cpl_query/_query/where_query.py +++ b/src/cpl_query/_query/where_query.py @@ -6,12 +6,7 @@ from cpl_query.extension.iterable_abc import IterableABC def where_query(_list: IterableABC, _func: Callable) -> IterableABC: result = IterableABC() for element in _list: - element_type = type(element).__name__ if _func(element): result.append(element) - # if element_type in _func: - # func = _func.replace(element_type, 'element') - # if eval(func): - # result.append(element) return result diff --git a/src/cpl_query/extension/iterable_abc.py b/src/cpl_query/extension/iterable_abc.py index 63d5d97b..b4ce047d 100644 --- a/src/cpl_query/extension/iterable_abc.py +++ b/src/cpl_query/extension/iterable_abc.py @@ -11,6 +11,9 @@ class IterableABC(ABC, list): @abstractmethod def any(self, func: Callable) -> bool: pass + @abstractmethod + def all(self, func: Callable) -> bool: pass + @abstractmethod def first(self) -> any: pass diff --git a/src/cpl_query/tests/query_test.py b/src/cpl_query/tests/query_test.py index 9135c434..5020183f 100644 --- a/src/cpl_query/tests/query_test.py +++ b/src/cpl_query/tests/query_test.py @@ -47,6 +47,18 @@ class QueryTest(unittest.TestCase): self.assertTrue(res) self.assertFalse(n_res) + def test_all(self): + results = [] + for user in self._tests: + if user.address.nr == 10: + results.append(user) + + res = self._tests.all(lambda u: u.address is not None) + n_res = self._tests.all(lambda u: u.address.nr == 100) + + self.assertTrue(res) + self.assertFalse(n_res) + def test_first(self): results = [] for user in self._tests: