From 2c6367a408426c39b55870e254069a0312453e7d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 4 Apr 2023 21:05:53 +0200 Subject: [PATCH] Improved cpl query --- src/cpl_query/base/ordered_queryable.py | 5 +- src/cpl_query/base/ordered_queryable_abc.py | 8 +-- src/cpl_query/base/queryable_abc.py | 54 ++++++++++----------- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/src/cpl_query/base/ordered_queryable.py b/src/cpl_query/base/ordered_queryable.py index 2bc18bf4..d865979f 100644 --- a/src/cpl_query/base/ordered_queryable.py +++ b/src/cpl_query/base/ordered_queryable.py @@ -1,5 +1,4 @@ from collections.abc import Callable -from typing import Self from cpl_query.base.ordered_queryable_abc import OrderedQueryableABC from cpl_query.exceptions import ArgumentNoneException, ExceptionArgument @@ -12,7 +11,7 @@ class OrderedQueryable(OrderedQueryableABC): def __init__(self, _t: type, _values: Iterable = None, _func: Callable = None): OrderedQueryableABC.__init__(self, _t, _values, _func) - def then_by(self, _func: Callable) -> Self: + def then_by(self, _func: Callable) -> OrderedQueryableABC: if self is None: raise ArgumentNoneException(ExceptionArgument.list) @@ -23,7 +22,7 @@ class OrderedQueryable(OrderedQueryableABC): return OrderedQueryable(self.type, sorted(self, key=lambda *args: [f(*args) for f in self._funcs]), _func) - def then_by_descending(self, _func: Callable) -> Self: + def then_by_descending(self, _func: Callable) -> OrderedQueryableABC: if self is None: raise ArgumentNoneException(ExceptionArgument.list) diff --git a/src/cpl_query/base/ordered_queryable_abc.py b/src/cpl_query/base/ordered_queryable_abc.py index 4a00d10a..8034bba4 100644 --- a/src/cpl_query/base/ordered_queryable_abc.py +++ b/src/cpl_query/base/ordered_queryable_abc.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from abc import abstractmethod from collections.abc import Callable -from typing import Iterable, Self +from typing import Iterable from cpl_query.base.queryable_abc import QueryableABC @@ -14,7 +16,7 @@ class OrderedQueryableABC(QueryableABC): self._funcs.append(_func) @abstractmethod - def then_by(self, func: Callable) -> Self: + def then_by(self, func: Callable) -> OrderedQueryableABC: r"""Sorts OrderedList in ascending order by function Parameter: @@ -26,7 +28,7 @@ class OrderedQueryableABC(QueryableABC): pass @abstractmethod - def then_by_descending(self, func: Callable) -> Self: + def then_by_descending(self, func: Callable) -> OrderedQueryableABC: r"""Sorts OrderedList in descending order by function Parameter: diff --git a/src/cpl_query/base/queryable_abc.py b/src/cpl_query/base/queryable_abc.py index 95cadadd..9a123a03 100644 --- a/src/cpl_query/base/queryable_abc.py +++ b/src/cpl_query/base/queryable_abc.py @@ -1,9 +1,9 @@ from __future__ import annotations -from typing import Optional, Callable, Union, Iterable, Self, Any -from typing import TYPE_CHECKING +from typing import Optional, Callable, Union, Iterable, Any, TYPE_CHECKING from cpl_query._helper import is_number +from cpl_query.base import default_lambda if TYPE_CHECKING: from cpl_query.base.ordered_queryable_abc import OrderedQueryableABC @@ -16,10 +16,6 @@ from cpl_query.exceptions import ( ) -def _default_lambda(x: object) -> object: - return x - - class QueryableABC(Sequence): def __init__(self, t: type, values: Iterable = None): Sequence.__init__(self, t, values) @@ -37,7 +33,7 @@ class QueryableABC(Sequence): bool """ if _func is None: - _func = _default_lambda + _func = default_lambda return self.count(_func) == self.count() @@ -54,7 +50,7 @@ class QueryableABC(Sequence): bool """ if _func is None: - _func = _default_lambda + _func = default_lambda return self.where(_func).count() > 0 @@ -109,7 +105,7 @@ class QueryableABC(Sequence): return self.where(_func).count() - def distinct(self, _func: Callable = None) -> Self: + def distinct(self, _func: Callable = None) -> QueryableABC: r"""Returns list without redundancies Parameter @@ -122,7 +118,7 @@ class QueryableABC(Sequence): :class: `cpl_query.base.queryable_abc.QueryableABC` """ if _func is None: - _func = _default_lambda + _func = default_lambda result = [] known_values = [] @@ -218,7 +214,7 @@ class QueryableABC(Sequence): return self - def group_by(self, _func: Callable = None) -> Self: + def group_by(self, _func: Callable = None) -> QueryableABC: r"""Groups by func Returns @@ -226,7 +222,7 @@ class QueryableABC(Sequence): Grouped list[list[any]]: any """ if _func is None: - _func = _default_lambda + _func = default_lambda groups = {} for v in self: @@ -282,7 +278,7 @@ class QueryableABC(Sequence): raise InvalidTypeException() if _func is None: - _func = _default_lambda + _func = default_lambda return _func(max(self, key=_func)) @@ -294,7 +290,7 @@ class QueryableABC(Sequence): Union[int, float] """ if _func is None: - _func = _default_lambda + _func = default_lambda result = self.order_by(_func).select(_func).to_list() length = len(result) @@ -317,7 +313,7 @@ class QueryableABC(Sequence): raise InvalidTypeException() if _func is None: - _func = _default_lambda + _func = default_lambda return _func(min(self, key=_func)) @@ -334,7 +330,7 @@ class QueryableABC(Sequence): :class: `cpl_query.base.ordered_queryable_abc.OrderedQueryableABC` """ if _func is None: - _func = _default_lambda + _func = default_lambda from cpl_query.base.ordered_queryable import OrderedQueryable @@ -353,13 +349,13 @@ class QueryableABC(Sequence): :class: `cpl_query.base.ordered_queryable_abc.OrderedQueryableABC` """ if _func is None: - _func = _default_lambda + _func = default_lambda from cpl_query.base.ordered_queryable import OrderedQueryable return OrderedQueryable(self.type, sorted(self, key=_func, reverse=True), _func) - def reverse(self) -> Self: + def reverse(self) -> QueryableABC: r"""Reverses list Returns @@ -368,7 +364,7 @@ class QueryableABC(Sequence): """ return type(self)(self._type, reversed(self._values)) - def select(self, _func: Callable) -> Self: + def select(self, _func: Callable) -> QueryableABC: r"""Formats each element of list to a given format Returns @@ -376,14 +372,14 @@ class QueryableABC(Sequence): :class: `cpl_query.base.queryable_abc.QueryableABC` """ if _func is None: - _func = _default_lambda + _func = default_lambda _l = [_func(_o) for _o in self] _t = type(_l[0]) if len(_l) > 0 else Any return type(self)(_t, _l) - def select_many(self, _func: Callable) -> Self: + def select_many(self, _func: Callable) -> QueryableABC: r"""Flattens resulting lists to one Returns @@ -427,7 +423,7 @@ class QueryableABC(Sequence): return self._values[0] - def skip(self, _index: int) -> Self: + def skip(self, _index: int) -> QueryableABC: r"""Skips all elements from index Parameter @@ -444,7 +440,7 @@ class QueryableABC(Sequence): return type(self)(self.type, self._values[_index:]) - def skip_last(self, _index: int) -> Self: + def skip_last(self, _index: int) -> QueryableABC: r"""Skips all elements after index Parameter @@ -478,7 +474,7 @@ class QueryableABC(Sequence): raise InvalidTypeException() if _func is None: - _func = _default_lambda + _func = default_lambda result = 0 for x in self: @@ -486,7 +482,7 @@ class QueryableABC(Sequence): return result - def split(self, _func: Callable) -> Self: + def split(self, _func: Callable) -> QueryableABC: r"""Splits the list by given function @@ -519,7 +515,7 @@ class QueryableABC(Sequence): return type(self)(self._type, query_groups) - def take(self, _index: int) -> Self: + def take(self, _index: int) -> QueryableABC: r"""Takes all elements from index Parameter @@ -536,7 +532,7 @@ class QueryableABC(Sequence): return type(self)(self._type, self._values[:_index]) - def take_last(self, _index: int) -> Self: + def take_last(self, _index: int) -> QueryableABC: r"""Takes all elements after index Parameter @@ -555,7 +551,7 @@ class QueryableABC(Sequence): return type(self)(self._type, self._values[index:]) - def where(self, _func: Callable = None) -> Self: + def where(self, _func: Callable = None) -> QueryableABC: r"""Select element by function Parameter @@ -571,6 +567,6 @@ class QueryableABC(Sequence): raise ArgumentNoneException(ExceptionArgument.func) if _func is None: - _func = _default_lambda + _func = default_lambda return type(self)(self.type, filter(_func, self))