Added sequence converters

This commit is contained in:
Sven Heidemann 2022-09-14 13:23:23 +02:00
parent e868a120f0
commit f0ed0bd2e1
9 changed files with 91 additions and 5 deletions

View File

@ -41,7 +41,7 @@ class SequenceABC(ABC):
self._values.reset() self._values.reset()
def to_list(self) -> list: 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 Returns
------- -------

View File

@ -21,6 +21,10 @@ from collections import namedtuple
# imports: # 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') VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='10', micro='9') version_info = VersionInfo(major='2022', minor='10', micro='9')

View File

@ -57,7 +57,7 @@ class Enumerable(EnumerableABC):
if _value is None: if _value is None:
raise ArgumentNoneException(ExceptionArgument.value) raise ArgumentNoneException(ExceptionArgument.value)
return _value in self return self.where(lambda x: x == _value).count() > 0
def count(self, _func: Callable = None) -> int: def count(self, _func: Callable = None) -> int:
if self is None: if self is None:

View File

@ -65,3 +65,13 @@ class EnumerableABC(SequenceABC, QueryableABC):
# self._values.remove(__object) # self._values.remove(__object)
self._values = SequenceValues([x for x in self.to_list() if x != __object], self._type) 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())

View File

@ -1,9 +1,32 @@
from typing import Iterable as IterableType
from cpl_query.iterable.iterable import Iterable from cpl_query.iterable.iterable import Iterable
class List(Iterable): class List(Iterable, list):
r"""Implementation of :class: `cpl_query.extension.iterable.Iterable` 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) 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())

View File

@ -54,7 +54,7 @@ class Iterable(IterableABC):
if _value is None: if _value is None:
raise ArgumentNoneException(ExceptionArgument.value) raise ArgumentNoneException(ExceptionArgument.value)
return _value in self return self.where(lambda x: x == _value).count() > 0
def count(self, _func: Callable = None) -> int: def count(self, _func: Callable = None) -> int:
if self is None: if self is None:

View File

@ -57,3 +57,13 @@ class IterableABC(SequenceABC, QueryableABC):
self.append(value) self.append(value)
return self 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())

View File

@ -4,6 +4,7 @@ from unittests_query.enumerable_query_test_case import EnumerableQueryTestCase
from unittests_query.enumerable_test_case import EnumerableTestCase from unittests_query.enumerable_test_case import EnumerableTestCase
from unittests_query.iterable_query_test_case import IterableQueryTestCase from unittests_query.iterable_query_test_case import IterableQueryTestCase
from unittests_query.iterable_test_case import IterableTestCase from unittests_query.iterable_test_case import IterableTestCase
from unittests_query.sequence_test_case import SequenceTestCase
class QueryTestSuite(unittest.TestSuite): class QueryTestSuite(unittest.TestSuite):
@ -12,6 +13,7 @@ class QueryTestSuite(unittest.TestSuite):
unittest.TestSuite.__init__(self) unittest.TestSuite.__init__(self)
loader = unittest.TestLoader() loader = unittest.TestLoader()
self.addTests(loader.loadTestsFromTestCase(SequenceTestCase))
self.addTests(loader.loadTestsFromTestCase(EnumerableTestCase)) self.addTests(loader.loadTestsFromTestCase(EnumerableTestCase))
self.addTests(loader.loadTestsFromTestCase(EnumerableQueryTestCase)) self.addTests(loader.loadTestsFromTestCase(EnumerableQueryTestCase))
self.addTests(loader.loadTestsFromTestCase(IterableTestCase)) self.addTests(loader.loadTestsFromTestCase(IterableTestCase))
@ -19,3 +21,8 @@ class QueryTestSuite(unittest.TestSuite):
def run(self, *args): def run(self, *args):
super().run(*args) super().run(*args)
if __name__ == "__main__":
runner = unittest.TextTestRunner()
runner.run(QueryTestSuite())

View File

@ -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))