Improved Sequences

This commit is contained in:
2022-09-14 23:01:52 +02:00
parent f0ed0bd2e1
commit 52069b7bb3
6 changed files with 50 additions and 47 deletions

View File

@@ -5,7 +5,7 @@ from typing import Optional, Callable, Union
class QueryableABC(ABC):
@abstractmethod
def all(self, _func: Callable) -> bool:
def all(self, _func: Callable = None) -> bool:
r"""Checks if every element of list equals result found by function
Parameter
@@ -20,7 +20,7 @@ class QueryableABC(ABC):
pass
@abstractmethod
def any(self, _func: Callable) -> bool:
def any(self, _func: Callable = None) -> bool:
r"""Checks if list contains result found by function
Parameter
@@ -35,7 +35,7 @@ class QueryableABC(ABC):
pass
@abstractmethod
def average(self, _func: Callable) -> Union[int, float, complex]:
def average(self, _func: Callable = None) -> Union[int, float, complex]:
r"""Returns average value of list
Parameter
@@ -145,7 +145,7 @@ class QueryableABC(ABC):
pass
@abstractmethod
def for_each(self, _func: Callable):
def for_each(self, _func: Callable = None):
r"""Runs given function for each element of list
Parameter
@@ -174,8 +174,9 @@ class QueryableABC(ABC):
Last element of list: Optional[any]
"""
pass
@abstractmethod
def max(self, _func: Callable) -> Union[int, float, complex]:
def max(self, _func: Callable = None) -> Union[int, float, complex]:
r"""Returns the highest value
Parameter
@@ -199,9 +200,8 @@ class QueryableABC(ABC):
"""
pass
@abstractmethod
def min(self, _func: Callable) -> Union[int, float, complex]:
def min(self, _func: Callable = None) -> Union[int, float, complex]:
r"""Returns the lowest value
Parameter
@@ -216,7 +216,7 @@ class QueryableABC(ABC):
pass
@abstractmethod
def order_by(self, _func: Callable) -> 'QueryableABC':
def order_by(self, _func: Callable = None) -> 'QueryableABC':
r"""Sorts elements by function in ascending order
Parameter
@@ -231,7 +231,7 @@ class QueryableABC(ABC):
pass
@abstractmethod
def order_by_descending(self, _func: Callable) -> 'QueryableABC':
def order_by_descending(self, _func: Callable = None) -> 'QueryableABC':
r"""Sorts elements by function in descending order
Parameter
@@ -331,7 +331,7 @@ class QueryableABC(ABC):
pass
@abstractmethod
def sum(self, _func: Callable) -> Union[int, float, complex]:
def sum(self, _func: Callable = None) -> Union[int, float, complex]:
r"""Sum of all values
Parameter
@@ -376,7 +376,7 @@ class QueryableABC(ABC):
pass
@abstractmethod
def where(self, _func: Callable) -> 'QueryableABC':
def where(self, _func: Callable = None) -> 'QueryableABC':
r"""Select element by function
Parameter

View File

@@ -37,9 +37,6 @@ class SequenceABC(ABC):
def type(self) -> type:
return self._type
def reset(self):
self._values.reset()
def to_list(self) -> list:
r"""Converts :class: `cpl_query.base.sequence_abc.SequenceABC` to :class: `list`

View File

@@ -19,20 +19,22 @@ class SequenceValues:
if data is None:
data = []
if len(data) > 0:
def type_check(_t: type, _l: list):
return all(isinstance(x, _t) for x in _l)
if not type_check(_t, data):
raise Exception(f'Unexpected type\nExpected type: {_t}')
if not hasattr(data, '__iter__'):
raise TypeError(f'{type(self).__name__} must be instantiated with an iterable object')
self._data = []
for element in data:
if _t is not None and type(element) != _t and not isinstance(type(element), _t) and not issubclass(type(element), _t):
raise Exception(f'Unexpected type: {type(element)}\nExpected type: {_t}')
self._data.append(element)
self._data = data
self._index = 0
self._len = sum(1 for item in self._data)
self._cycle = itertools.cycle(self._data)
def __len__(self):
return self._len
return sum(1 for item in self._data)
def __iter__(self):
i = 0