2022.10.9 - Enumerable & List (#105) #106
@ -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
|
||||
-------
|
||||
|
@ -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')
|
||||
|
@ -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:
|
||||
|
@ -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())
|
||||
|
@ -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())
|
||||
|
@ -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:
|
||||
|
@ -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())
|
||||
|
@ -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())
|
32
unittests/unittests_query/sequence_test_case.py
Normal file
32
unittests/unittests_query/sequence_test_case.py
Normal 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))
|
Loading…
Reference in New Issue
Block a user