Changed structure of cpl-query
This commit is contained in:
@@ -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) -> 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) -> 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 = None) -> Union[int, float, complex]:
|
||||
def average(self, _func: Callable) -> Union[int, float, complex]:
|
||||
r"""Returns average value of list
|
||||
|
||||
Parameter
|
||||
@@ -65,7 +65,7 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def count(self, func: Callable = None) -> int:
|
||||
def count(self, _func: Callable = None) -> int:
|
||||
r"""Returns length of list or count of found elements
|
||||
|
||||
Parameter
|
||||
@@ -80,7 +80,7 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def distinct(self, func: Callable = None) -> 'QueryableABC':
|
||||
def distinct(self, _func: Callable = None) -> 'QueryableABC':
|
||||
r"""Returns list without redundancies
|
||||
|
||||
Parameter
|
||||
@@ -124,26 +124,6 @@ class QueryableABC(ABC):
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def last(self) -> any:
|
||||
r"""Returns last element
|
||||
|
||||
Returns
|
||||
-------
|
||||
Last element of list: any
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def last_or_default(self) -> any:
|
||||
r"""Returns last element or None
|
||||
|
||||
Returns
|
||||
-------
|
||||
Last element of list: Optional[any]
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def first(self) -> any:
|
||||
r"""Returns first element
|
||||
@@ -165,7 +145,7 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def for_each(self, func: Callable):
|
||||
def for_each(self, _func: Callable):
|
||||
r"""Runs given function for each element of list
|
||||
|
||||
Parameter
|
||||
@@ -176,8 +156,27 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def max(self, func: Callable = None) -> Union[int, float, complex]:
|
||||
r"""Returns highest value
|
||||
def last(self) -> any:
|
||||
r"""Returns last element
|
||||
|
||||
Returns
|
||||
-------
|
||||
Last element of list: any
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def last_or_default(self) -> any:
|
||||
r"""Returns last element or None
|
||||
|
||||
Returns
|
||||
-------
|
||||
Last element of list: Optional[any]
|
||||
"""
|
||||
pass
|
||||
@abstractmethod
|
||||
def max(self, _func: Callable) -> Union[int, float, complex]:
|
||||
r"""Returns the highest value
|
||||
|
||||
Parameter
|
||||
---------
|
||||
@@ -191,8 +190,19 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def min(self, func: Callable = None) -> Union[int, float, complex]:
|
||||
r"""Returns highest value
|
||||
def median(self) -> Union[int, float]:
|
||||
r"""Return the median value of data elements
|
||||
|
||||
Returns
|
||||
-------
|
||||
Union[int, float]
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
@abstractmethod
|
||||
def min(self, _func: Callable) -> Union[int, float, complex]:
|
||||
r"""Returns the lowest value
|
||||
|
||||
Parameter
|
||||
---------
|
||||
@@ -206,7 +216,7 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def order_by(self, func: Callable) -> 'QueryableABC':
|
||||
def order_by(self, _func: Callable) -> 'QueryableABC':
|
||||
r"""Sorts elements by function in ascending order
|
||||
|
||||
Parameter
|
||||
@@ -221,7 +231,7 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def order_by_descending(self, func: Callable) -> 'QueryableABC':
|
||||
def order_by_descending(self, _func: Callable) -> 'QueryableABC':
|
||||
r"""Sorts elements by function in descending order
|
||||
|
||||
Parameter
|
||||
@@ -321,7 +331,7 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def sum(self, func: Callable = None) -> Union[int, float, complex]:
|
||||
def sum(self, _func: Callable) -> Union[int, float, complex]:
|
||||
r"""Sum of all values
|
||||
|
||||
Parameter
|
||||
@@ -366,7 +376,7 @@ class QueryableABC(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def where(self, func: Callable) -> 'QueryableABC':
|
||||
def where(self, _func: Callable) -> 'QueryableABC':
|
||||
r"""Select element by function
|
||||
|
||||
Parameter
|
||||
|
||||
50
src/cpl_query/base/sequence_abc.py
Normal file
50
src/cpl_query/base/sequence_abc.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from abc import abstractmethod, ABC
|
||||
from typing import Union
|
||||
|
||||
from cpl_query.base.sequence_values import SequenceValues
|
||||
|
||||
|
||||
class SequenceABC(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self, t: type = None, values: Union[list, iter] = None):
|
||||
ABC.__init__(self)
|
||||
|
||||
if t == any:
|
||||
t = None
|
||||
elif t is None and values is not None:
|
||||
t = type(values[0])
|
||||
|
||||
self._type = t
|
||||
self._values = SequenceValues(values, t)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._values)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._values)
|
||||
|
||||
def next(self):
|
||||
return next(self._values)
|
||||
|
||||
def __next__(self):
|
||||
return self.next()
|
||||
|
||||
def __repr__(self):
|
||||
return f'<{type(self).__name__} {list(self).__repr__()}>'
|
||||
|
||||
@property
|
||||
def type(self) -> type:
|
||||
return self._type
|
||||
|
||||
def reset(self):
|
||||
self._values.reset()
|
||||
|
||||
def to_list(self) -> list:
|
||||
r"""Converts :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` to :class: `list`
|
||||
|
||||
Returns
|
||||
-------
|
||||
:class: `list`
|
||||
"""
|
||||
return [x for x in self]
|
||||
55
src/cpl_query/base/sequence_values.py
Normal file
55
src/cpl_query/base/sequence_values.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import io
|
||||
import itertools
|
||||
|
||||
from cpl_query.exceptions import IndexOutOfRangeException
|
||||
|
||||
|
||||
class SequenceEnd:
|
||||
|
||||
def __init__(self):
|
||||
self.is_ended = False
|
||||
|
||||
def set_end(self, value: bool) -> 'SequenceEnd':
|
||||
self.is_ended = value
|
||||
return self
|
||||
|
||||
|
||||
class SequenceValues:
|
||||
def __init__(self, data, _t: type):
|
||||
if data is None:
|
||||
data = []
|
||||
|
||||
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._index = 0
|
||||
self._len = sum(1 for item in self._data)
|
||||
self._cycle = itertools.cycle(self._data)
|
||||
|
||||
def __len__(self):
|
||||
return self._len
|
||||
|
||||
def __iter__(self):
|
||||
i = 0
|
||||
while i < len(self):
|
||||
yield next(self._cycle)
|
||||
i += 1
|
||||
|
||||
def __next__(self):
|
||||
if self._index >= len(self):
|
||||
raise IndexOutOfRangeException()
|
||||
self._index += 1
|
||||
|
||||
return self.next()
|
||||
|
||||
def next(self):
|
||||
return next(self._cycle)
|
||||
|
||||
def reset(self):
|
||||
self._index = 0
|
||||
self._cycle = itertools.cycle(self._data)
|
||||
Reference in New Issue
Block a user