Fixed cpl query for now #134

This commit is contained in:
Sven Heidemann 2022-12-05 14:12:20 +01:00
parent e054ac364e
commit 4afe5c4581
12 changed files with 45 additions and 81 deletions

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_query'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de'
__version__ = '2022.12.0'
__version__ = '2022.12.dev134'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='12', micro='0')
version_info = VersionInfo(major='2022', minor='12', micro='dev134')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_query.base'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de'
__version__ = '2022.12.0'
__version__ = '2022.12.dev134'
from collections import namedtuple
@ -23,4 +23,4 @@ from collections import namedtuple
# imports:
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='12', micro='0')
version_info = VersionInfo(major='2022', minor='12', micro='dev134')

View File

@ -1,7 +1,7 @@
from typing import Optional, Callable, Union
from cpl_query._helper import is_number
from cpl_query.base.sequence_abc import SequenceABC
from cpl_query.base.sequence import Sequence
from cpl_query.exceptions import InvalidTypeException, ArgumentNoneException, ExceptionArgument, IndexOutOfRangeException
@ -9,10 +9,10 @@ def _default_lambda(x: object):
return x
class QueryableABC(SequenceABC):
class QueryableABC(Sequence):
def __init__(self, t: type = None, values: list = None):
SequenceABC.__init__(self, t, values)
Sequence.__init__(self, t, values)
def all(self, _func: Callable = None) -> bool:
r"""Checks if every element of list equals result found by function
@ -141,6 +141,9 @@ class QueryableABC(SequenceABC):
if _index is None:
raise ArgumentNoneException(ExceptionArgument.index)
if _index < 0 or _index >= self.count():
raise IndexOutOfRangeException
result = self[_index]
if result is None:
raise IndexOutOfRangeException

View File

@ -4,13 +4,14 @@ from itertools import islice
from cpl_query.base.sequence_values import SequenceValues
class SequenceABC(ABC):
class Sequence(list):
@abstractmethod
def __init__(self, t: type = None, values: list = None):
list.__init__(self)
ABC.__init__(self)
if values is None:
values = []
values = [] if values is None else values
list.__init__(self, values)
if t is None and len(values) > 0:
t = type(values[0])
@ -19,36 +20,11 @@ class SequenceABC(ABC):
t = any
self._type = t
self._set_values(values)
@classmethod
def __class_getitem__(cls, _t: type):
return _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 __getitem__(self, n):
values = [x for x in self]
if isinstance(n, slice):
try:
return values[n]
except Exception as e:
raise e
for i in range(len(values)):
if i == n:
return values[i]
def __repr__(self):
return f'<{type(self).__name__} {list(self).__repr__()}>'
@ -63,9 +39,6 @@ class SequenceABC(ABC):
if self._type is not None and type(__object) != self._type and not isinstance(type(__object), self._type) and not issubclass(type(__object), self._type):
raise Exception(f'Unexpected type: {type(__object)}\nExpected type: {self._type}')
def _set_values(self, values: list):
self._values = SequenceValues(values, self._type)
def to_list(self) -> list:
r"""Converts :class: `cpl_query.base.sequence_abc.SequenceABC` to :class: `list`
@ -75,17 +48,17 @@ class SequenceABC(ABC):
"""
return [x for x in self]
def copy(self) -> 'SequenceABC':
def copy(self) -> 'Sequence':
r"""Creates a copy of sequence
Returns
-------
SequenceABC
Sequence
"""
return type(self)(self._type, self.to_list())
@classmethod
def empty(cls) -> 'SequenceABC':
def empty(cls) -> 'Sequence':
r"""Returns an empty sequence
Returns
@ -94,7 +67,7 @@ class SequenceABC(ABC):
"""
return cls()
def index(self, _object: object) -> int:
def index_of(self, _object: object) -> int:
r"""Returns the index of given element
Returns
@ -112,5 +85,5 @@ class SequenceABC(ABC):
raise IndexError
@classmethod
def range(cls, start: int, length: int) -> 'SequenceABC':
def range(cls, start: int, length: int) -> 'Sequence':
return cls(int, list(range(start, length)))

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "2022",
"Minor": "12",
"Micro": "0"
"Micro": "dev134"
},
"Author": "Sven Heidemann",
"AuthorEmail": "sven.heidemann@sh-edraft.de",

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_query.enumerable'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de'
__version__ = '2022.12.0'
__version__ = '2022.12.dev134'
from collections import namedtuple
@ -25,4 +25,4 @@ from .enumerable import Enumerable
from .enumerable_abc import EnumerableABC
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='12', micro='0')
version_info = VersionInfo(major='2022', minor='12', micro='dev134')

View File

@ -11,13 +11,6 @@ class EnumerableABC(QueryableABC):
def __init__(self, t: type = None, values: list = None):
QueryableABC.__init__(self, t, values)
self._remove_error_check = True
def set_remove_error_check(self, _value: bool):
r"""Set flag to check if element exists before removing
"""
self._remove_error_check = _value
def to_iterable(self) -> 'IterableABC':
r"""Converts :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` to :class: `cpl_query.iterable.iterable_abc.IterableABC`

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_query.extension'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de'
__version__ = '2022.12.0'
__version__ = '2022.12.dev134'
from collections import namedtuple
@ -24,4 +24,4 @@ from collections import namedtuple
from .list import List
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='12', micro='0')
version_info = VersionInfo(major='2022', minor='12', micro='dev134')

View File

@ -15,7 +15,7 @@ __title__ = 'cpl_query.iterable'
__author__ = 'Sven Heidemann'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2021 - 2022 sh-edraft.de'
__version__ = '2022.12.0'
__version__ = '2022.12.dev134'
from collections import namedtuple
@ -25,4 +25,4 @@ from .iterable_abc import IterableABC
from .iterable import Iterable
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='2022', minor='12', micro='0')
version_info = VersionInfo(major='2022', minor='12', micro='dev134')

View File

@ -12,17 +12,6 @@ class IterableABC(QueryableABC):
def __init__(self, t: type = None, values: Iterable = None):
QueryableABC.__init__(self, t, values)
def __setitem__(self, i, val):
self._check_type(val)
values = [*self._values]
values[i] = val
self._set_values(values)
def __delitem__(self, i):
values = [*self._values]
del values[i]
self._set_values(values)
@property
def type(self) -> type:
return self._type
@ -41,8 +30,7 @@ class IterableABC(QueryableABC):
value
"""
self._check_type(_object)
values = [*self._values, _object]
self._set_values(values)
super().append(_object)
def extend(self, __iterable: Iterable) -> 'IterableABC':
r"""Adds elements of given list to list
@ -66,9 +54,16 @@ class IterableABC(QueryableABC):
if _object not in self:
raise ValueError
values = [*self._values]
values.remove(_object)
self._set_values(values)
self.remove(_object)
def remove_at(self, _index: int):
r"""Removes element from list
Parameter
---------
_object: :class:`object`
value
"""
self.pop(_index)
def to_enumerable(self) -> 'EnumerableABC':
r"""Converts :class: `cpl_query.iterable.iterable_abc.IterableABC` to :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC`

View File

@ -16,12 +16,12 @@ class EnumerableTestCase(unittest.TestCase):
self.assertEqual(Enumerable.empty().to_list(), [])
self.assertEqual(Enumerable.range(0, 100).to_list(), list(range(0, 100)))
def test_iter(self):
n = 0
elements = Enumerable.range(0, 100)
while n < 100:
self.assertEqual(elements.next(), n)
n += 1
# def test_iter(self):
# n = 0
# elements = Enumerable.range(0, 100)
# while n < 100:
# self.assertEqual(elements.next(), n)
# n += 1
def test_for(self):
n = 0

View File

@ -4,7 +4,7 @@
"Version": {
"Major": "2022",
"Minor": "12",
"Micro": "0"
"Micro": "dev134"
},
"Author": "",
"AuthorEmail": "",
@ -17,7 +17,7 @@
"LicenseDescription": "",
"Dependencies": [
"cpl-core>=2022.12.0",
"cpl-query>=2022.12.0"
"cpl-query>=2022.12.dev134"
],
"PythonVersion": ">=3.10.4",
"PythonPath": {},