From 2e0a8bb1ff18265cdc8dff85a4374241f63f1a47 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 4 Dec 2022 00:08:16 +0100 Subject: [PATCH] Minor fixes to cpl query --- src/cpl_discord/cpl-discord.json | 4 +-- src/cpl_query/base/sequence_abc.py | 30 +++++++++++++++++++ src/cpl_query/iterable/iterable_abc.py | 14 +++++++++ .../iterable_query_test_case.py | 2 +- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/cpl_discord/cpl-discord.json b/src/cpl_discord/cpl-discord.json index 08fb8870..a515bca1 100644 --- a/src/cpl_discord/cpl-discord.json +++ b/src/cpl_discord/cpl-discord.json @@ -3,8 +3,8 @@ "Name": "cpl-discord", "Version": { "Major": "2022", - "Minor": "10", - "Micro": "0.post6" + "Minor": "12", + "Micro": "0" }, "Author": "Sven Heidemann", "AuthorEmail": "sven.heidemann@sh-edraft.de", diff --git a/src/cpl_query/base/sequence_abc.py b/src/cpl_query/base/sequence_abc.py index 8e3a01aa..b33a8082 100644 --- a/src/cpl_query/base/sequence_abc.py +++ b/src/cpl_query/base/sequence_abc.py @@ -21,6 +21,10 @@ class SequenceABC(ABC): self._type = t self._set_values(values) + @classmethod + def __class_getitem__(cls, _t: type): + return _t + def __len__(self): return len(self._values) @@ -71,6 +75,15 @@ class SequenceABC(ABC): """ return [x for x in self] + def copy(self) -> 'SequenceABC': + r"""Creates a copy of sequence + + Returns + ------- + SequenceABC + """ + return type(self)(self._type, self.to_list()) + @classmethod def empty(cls) -> 'SequenceABC': r"""Returns an empty sequence @@ -81,6 +94,23 @@ class SequenceABC(ABC): """ return cls() + def index(self, _object: object) -> int: + r"""Returns the index of given element + + Returns + ------- + Index of object + + Raises + ------- + IndexError if object not in sequence + """ + for i, o in enumerate(self): + if o == _object: + return i + + raise IndexError + @classmethod def range(cls, start: int, length: int) -> 'SequenceABC': return cls(int, list(range(start, length))) diff --git a/src/cpl_query/iterable/iterable_abc.py b/src/cpl_query/iterable/iterable_abc.py index dcae50b3..5758b03c 100644 --- a/src/cpl_query/iterable/iterable_abc.py +++ b/src/cpl_query/iterable/iterable_abc.py @@ -56,6 +56,20 @@ class IterableABC(QueryableABC): return self + def remove(self, _object: object): + r"""Removes element from list + Parameter + --------- + _object: :class:`object` + value + """ + if _object not in self: + raise ValueError + + values = [*self._values] + values.remove(_object) + self._set_values(values) + def to_enumerable(self) -> 'EnumerableABC': r"""Converts :class: `cpl_query.iterable.iterable_abc.IterableABC` to :class: `cpl_query.enumerable.enumerable_abc.EnumerableABC` diff --git a/unittests/unittests_query/iterable_query_test_case.py b/unittests/unittests_query/iterable_query_test_case.py index 8ea3a0b8..6dfa36f0 100644 --- a/unittests/unittests_query/iterable_query_test_case.py +++ b/unittests/unittests_query/iterable_query_test_case.py @@ -342,7 +342,7 @@ class IterableQueryTestCase(unittest.TestCase): self.assertEqual(skipped.to_list(), self._tests[:-5]) self.assertEqual(skipped.last(), self._tests[:-5][len(self._tests[:-5]) - 1]) - def test_sum(self): + def test_sum(self) -> List['int']: res = self._tests.sum(lambda u: u.address.nr) s_res = 0