Improved query

This commit is contained in:
2022-09-15 00:30:44 +02:00
parent 52069b7bb3
commit bb461f5fba
6 changed files with 59 additions and 14 deletions

View File

@@ -206,21 +206,17 @@ class Enumerable(EnumerableABC):
if _func is None:
_func = _default_lambda
result = Enumerable()
result.extend(_func(_o) for _o in self)
return result
_l = [_func(_o) for _o in self]
return Enumerable(self._type if len(_l) < 1 else type(_l[0]), _l)
def select_many(self, _func: Callable = None) -> EnumerableABC:
if _func is None:
_func = _default_lambda
result = Enumerable()
# The line below is pain. I don't understand anything of it...
# The line below is pain. I don't understand anything of the list comprehension...
# written on 09.11.2022 by Sven Heidemann
elements = [_a for _o in self for _a in _func(_o)]
result.extend(elements)
return result
_l = [_a for _o in self for _a in _func(_o)]
return Enumerable(self._type if len(_l) < 1 else type(_l[0]), _l)
def single(self: EnumerableABC) -> any:
if self is None:
@@ -237,9 +233,9 @@ class Enumerable(EnumerableABC):
if self is None:
raise ArgumentNoneException(ExceptionArgument.list)
if len(self) > 1:
if self.count() > 1:
raise IndexError('Found more than one element')
elif len(self) == 0:
elif self.count() == 0:
return None
return self.element_at(0)

View File

@@ -1,4 +1,4 @@
from typing import Callable, Optional, Union
from typing import Callable, Optional, Union, Iterable as IterableType
from cpl_query._helper import is_number
from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument, InvalidTypeException, IndexOutOfRangeException
@@ -12,7 +12,7 @@ def _default_lambda(x: object):
class Iterable(IterableABC):
def __init__(self, t: type = None, values: list = None):
def __init__(self, t: type = None, values: IterableType = None):
IterableABC.__init__(self, t, values)
def all(self, _func: Callable = None) -> bool:

View File

@@ -11,7 +11,7 @@ class IterableABC(SequenceABC, QueryableABC):
"""
@abstractmethod
def __init__(self, t: type = None, values: list = None):
def __init__(self, t: type = None, values: Iterable = None):
SequenceABC.__init__(self, t, values)
def __getitem__(self, n) -> object: