Improved query

This commit is contained in:
Sven Heidemann 2022-09-15 17:00:22 +02:00
parent ae3192b63c
commit 47ed0a705d
4 changed files with 13 additions and 9 deletions

View File

@ -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()

View File

@ -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)))

View File

@ -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}')