From 47ed0a705d98239448467c2636a9b95d45e18168 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Thu, 15 Sep 2022 17:00:22 +0200 Subject: [PATCH] Improved query --- src/cpl_query/base/sequence_values.py | 10 ++++++---- src/cpl_query/enumerable/enumerable.py | 6 +++--- src/cpl_query/iterable/iterable.py | 2 +- unittests/unittests_query/performance_test_case.py | 4 +++- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/cpl_query/base/sequence_values.py b/src/cpl_query/base/sequence_values.py index e5406412..9e2a485e 100644 --- a/src/cpl_query/base/sequence_values.py +++ b/src/cpl_query/base/sequence_values.py @@ -19,12 +19,14 @@ class SequenceValues: if not hasattr(data, '__iter__'): raise TypeError(f'{type(self).__name__} must be instantiated with an iterable object') - self._data = data + self._new_cycle = lambda: itertools.cycle(data) + self._len = lambda: len(data) + self._index = 0 - self._cycle = itertools.cycle(self._data) + self._cycle = self._new_cycle() def __len__(self): - return sum(1 for item in self._data) + return self._len() def __iter__(self): i = 0 @@ -44,4 +46,4 @@ class SequenceValues: def reset(self): self._index = 0 - self._cycle = itertools.cycle(self._data) + self._cycle = self._new_cycle() diff --git a/src/cpl_query/enumerable/enumerable.py b/src/cpl_query/enumerable/enumerable.py index 6291cd5d..d095f473 100644 --- a/src/cpl_query/enumerable/enumerable.py +++ b/src/cpl_query/enumerable/enumerable.py @@ -233,9 +233,9 @@ class Enumerable(EnumerableABC): if self is None: raise ArgumentNoneException(ExceptionArgument.list) - if self.count() > 1: + if len(self) > 1: raise IndexError('Found more than one element') - elif self.count() == 0: + elif len(self) == 0: return None return self.element_at(0) @@ -316,4 +316,4 @@ class Enumerable(EnumerableABC): if _func is None: raise ArgumentNoneException(ExceptionArgument.func) - return Enumerable(self.type, [x for x in self if _func(x)]) + return Enumerable(self.type, list(filter(_func, self._values))) diff --git a/src/cpl_query/iterable/iterable.py b/src/cpl_query/iterable/iterable.py index 154630cf..64a0ce31 100644 --- a/src/cpl_query/iterable/iterable.py +++ b/src/cpl_query/iterable/iterable.py @@ -340,4 +340,4 @@ class Iterable(IterableABC): if _func(element): result.append(element) - return result + return result \ No newline at end of file diff --git a/unittests/unittests_query/performance_test_case.py b/unittests/unittests_query/performance_test_case.py index 9a375763..1741f4e2 100644 --- a/unittests/unittests_query/performance_test_case.py +++ b/unittests/unittests_query/performance_test_case.py @@ -5,7 +5,7 @@ import unittest from cpl_query.enumerable import Enumerable from cpl_query.iterable import Iterable -VALUES = 1000 +VALUES = 10000 COUNT = 50 @@ -23,6 +23,7 @@ class PerformanceTestCase(unittest.TestCase): enumerable = timeit.timeit(lambda: Enumerable(int, self.values), number=COUNT) iterable = timeit.timeit(lambda: Iterable(int, self.values), number=COUNT) + print('Range') print(f'd: {default}') print(f'i: {iterable}') print(f'e: {enumerable}') @@ -35,6 +36,7 @@ class PerformanceTestCase(unittest.TestCase): iterable = timeit.timeit(lambda: Iterable(int, self.values).where(lambda x: x == 50).single(), number=COUNT) enumerable = timeit.timeit(lambda: Enumerable(int, self.values).where(lambda x: x == 50).single(), number=COUNT) + print('Where single') print(f'd: {default}') print(f'i: {iterable}') print(f'e: {enumerable}')