diff --git a/src/cpl_query/base/sequence_abc.py b/src/cpl_query/base/sequence_abc.py index 07bc142a..15b665d7 100644 --- a/src/cpl_query/base/sequence_abc.py +++ b/src/cpl_query/base/sequence_abc.py @@ -41,7 +41,7 @@ class SequenceABC(ABC): self._values.reset() def to_list(self) -> list: - r"""Converts :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` to :class: `list` + r"""Converts :class: `cpl_query.base.sequence_abc.SequenceABC` to :class: `list` Returns ------- diff --git a/src/cpl_query/enumerable/__init__.py b/src/cpl_query/enumerable/__init__.py index c98bc6d5..f9ea82d0 100644 --- a/src/cpl_query/enumerable/__init__.py +++ b/src/cpl_query/enumerable/__init__.py @@ -21,6 +21,10 @@ from collections import namedtuple # imports: +from .enumerable import Enumerable +from .enumerable_abc import EnumerableABC +from .ordered_enumerable import OrderedEnumerable +from .ordered_enumerable_abc import OrderedEnumerableABC VersionInfo = namedtuple('VersionInfo', 'major minor micro') version_info = VersionInfo(major='2022', minor='10', micro='9') diff --git a/src/cpl_query/enumerable/enumerable.py b/src/cpl_query/enumerable/enumerable.py index 4c826f77..0b252c9c 100644 --- a/src/cpl_query/enumerable/enumerable.py +++ b/src/cpl_query/enumerable/enumerable.py @@ -57,7 +57,7 @@ class Enumerable(EnumerableABC): if _value is None: raise ArgumentNoneException(ExceptionArgument.value) - return _value in self + return self.where(lambda x: x == _value).count() > 0 def count(self, _func: Callable = None) -> int: if self is None: diff --git a/src/cpl_query/enumerable/enumerable_abc.py b/src/cpl_query/enumerable/enumerable_abc.py index 95f12c5d..517540d7 100644 --- a/src/cpl_query/enumerable/enumerable_abc.py +++ b/src/cpl_query/enumerable/enumerable_abc.py @@ -65,3 +65,13 @@ class EnumerableABC(SequenceABC, QueryableABC): # self._values.remove(__object) self._values = SequenceValues([x for x in self.to_list() if x != __object], self._type) + + def to_iterable(self) -> 'IterableABC': + r"""Converts :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` to :class: `cpl_query.iterable.iterable_abc.IterableABC` + + Returns + ------- + :class: `cpl_query.iterable.iterable_abc.IterableABC` + """ + from cpl_query.iterable.iterable import Iterable + return Iterable(self._type, self.to_list()) diff --git a/src/cpl_query/extension/list.py b/src/cpl_query/extension/list.py index d658f1d2..fbc7c086 100644 --- a/src/cpl_query/extension/list.py +++ b/src/cpl_query/extension/list.py @@ -1,9 +1,32 @@ +from typing import Iterable as IterableType + from cpl_query.iterable.iterable import Iterable -class List(Iterable): +class List(Iterable, list): r"""Implementation of :class: `cpl_query.extension.iterable.Iterable` """ - def __init__(self, t: type = None, values: list = None): + def __init__(self, t: type = None, values: IterableType = None): Iterable.__init__(self, t, values) + list.__init__(self) + + def to_enumerable(self) -> 'EnumerableABC': + r"""Converts :class: `cpl_query.iterable.iterable_abc.IterableABC` to :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` + + Returns + ------- + :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` + """ + from cpl_query.enumerable.enumerable import Enumerable + return Enumerable(self._type, self.to_list()) + + def to_iterable(self) -> 'IterableABC': + r"""Converts :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` to :class: `cpl_query.iterable.iterable_abc.IterableABC` + + Returns + ------- + :class: `cpl_query.iterable.iterable_abc.IterableABC` + """ + from cpl_query.iterable.iterable import Iterable + return Iterable(self._type, self.to_list()) diff --git a/src/cpl_query/iterable/iterable.py b/src/cpl_query/iterable/iterable.py index ea2090a0..87f876de 100644 --- a/src/cpl_query/iterable/iterable.py +++ b/src/cpl_query/iterable/iterable.py @@ -54,7 +54,7 @@ class Iterable(IterableABC): if _value is None: raise ArgumentNoneException(ExceptionArgument.value) - return _value in self + return self.where(lambda x: x == _value).count() > 0 def count(self, _func: Callable = None) -> int: if self is None: diff --git a/src/cpl_query/iterable/iterable_abc.py b/src/cpl_query/iterable/iterable_abc.py index aca77ea1..e844ec2a 100644 --- a/src/cpl_query/iterable/iterable_abc.py +++ b/src/cpl_query/iterable/iterable_abc.py @@ -57,3 +57,13 @@ class IterableABC(SequenceABC, QueryableABC): self.append(value) return self + + def to_enumerable(self) -> 'EnumerableABC': + r"""Converts :class: `cpl_query.iterable.iterable_abc.IterableABC` to :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` + + Returns + ------- + :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` + """ + from cpl_query.enumerable.enumerable import Enumerable + return Enumerable(self._type, self.to_list()) diff --git a/unittests/unittests_query/query_test_suite.py b/unittests/unittests_query/query_test_suite.py index d01049f9..e1c307b4 100644 --- a/unittests/unittests_query/query_test_suite.py +++ b/unittests/unittests_query/query_test_suite.py @@ -4,6 +4,7 @@ from unittests_query.enumerable_query_test_case import EnumerableQueryTestCase from unittests_query.enumerable_test_case import EnumerableTestCase from unittests_query.iterable_query_test_case import IterableQueryTestCase from unittests_query.iterable_test_case import IterableTestCase +from unittests_query.sequence_test_case import SequenceTestCase class QueryTestSuite(unittest.TestSuite): @@ -12,6 +13,7 @@ class QueryTestSuite(unittest.TestSuite): unittest.TestSuite.__init__(self) loader = unittest.TestLoader() + self.addTests(loader.loadTestsFromTestCase(SequenceTestCase)) self.addTests(loader.loadTestsFromTestCase(EnumerableTestCase)) self.addTests(loader.loadTestsFromTestCase(EnumerableQueryTestCase)) self.addTests(loader.loadTestsFromTestCase(IterableTestCase)) @@ -19,3 +21,8 @@ class QueryTestSuite(unittest.TestSuite): def run(self, *args): super().run(*args) + + +if __name__ == "__main__": + runner = unittest.TextTestRunner() + runner.run(QueryTestSuite()) \ No newline at end of file diff --git a/unittests/unittests_query/sequence_test_case.py b/unittests/unittests_query/sequence_test_case.py new file mode 100644 index 00000000..2398e2a9 --- /dev/null +++ b/unittests/unittests_query/sequence_test_case.py @@ -0,0 +1,32 @@ +import unittest + +from cpl_query.enumerable import Enumerable +from cpl_query.extension.list import List +from cpl_query.iterable import Iterable + + +class SequenceTestCase(unittest.TestCase): + + def test_to_list(self): + _list = List().extend(range(0, 100)) + enumerable = Enumerable.range(0, 100) + iterable = Iterable(int, list(range(0, 100))) + + self.assertEqual(enumerable.to_list(), _list.to_list()) + self.assertEqual(iterable.to_list(), _list.to_list()) + + def test_to_enumerable(self): + _list = List().extend(range(0, 100)) + enumerable = Enumerable.range(0, 100) + iterable = Iterable(int, list(range(0, 100))) + + self.assertEqual(type(_list.to_enumerable()), type(enumerable)) + self.assertEqual(type(iterable.to_enumerable()), type(enumerable)) + + def test_to_iterable(self): + _list = List().extend(range(0, 100)) + enumerable = Enumerable.range(0, 100) + iterable = Iterable(int, list(range(0, 100))) + + self.assertEqual(type(_list.to_iterable()), type(iterable)) + self.assertEqual(type(enumerable.to_iterable()), type(iterable))