2022.10.9 - Enumerable & List (#105) #106
@ -19,12 +19,14 @@ class SequenceValues:
|
|||||||
if not hasattr(data, '__iter__'):
|
if not hasattr(data, '__iter__'):
|
||||||
raise TypeError(f'{type(self).__name__} must be instantiated with an iterable object')
|
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._index = 0
|
||||||
self._cycle = itertools.cycle(self._data)
|
self._cycle = self._new_cycle()
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return sum(1 for item in self._data)
|
return self._len()
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
i = 0
|
i = 0
|
||||||
@ -44,4 +46,4 @@ class SequenceValues:
|
|||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self._index = 0
|
self._index = 0
|
||||||
self._cycle = itertools.cycle(self._data)
|
self._cycle = self._new_cycle()
|
||||||
|
@ -233,9 +233,9 @@ class Enumerable(EnumerableABC):
|
|||||||
if self is None:
|
if self is None:
|
||||||
raise ArgumentNoneException(ExceptionArgument.list)
|
raise ArgumentNoneException(ExceptionArgument.list)
|
||||||
|
|
||||||
if self.count() > 1:
|
if len(self) > 1:
|
||||||
raise IndexError('Found more than one element')
|
raise IndexError('Found more than one element')
|
||||||
elif self.count() == 0:
|
elif len(self) == 0:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return self.element_at(0)
|
return self.element_at(0)
|
||||||
@ -316,4 +316,4 @@ class Enumerable(EnumerableABC):
|
|||||||
if _func is None:
|
if _func is None:
|
||||||
raise ArgumentNoneException(ExceptionArgument.func)
|
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)))
|
||||||
|
@ -340,4 +340,4 @@ class Iterable(IterableABC):
|
|||||||
if _func(element):
|
if _func(element):
|
||||||
result.append(element)
|
result.append(element)
|
||||||
|
|
||||||
return result
|
return result
|
@ -5,7 +5,7 @@ import unittest
|
|||||||
from cpl_query.enumerable import Enumerable
|
from cpl_query.enumerable import Enumerable
|
||||||
from cpl_query.iterable import Iterable
|
from cpl_query.iterable import Iterable
|
||||||
|
|
||||||
VALUES = 1000
|
VALUES = 10000
|
||||||
COUNT = 50
|
COUNT = 50
|
||||||
|
|
||||||
|
|
||||||
@ -23,6 +23,7 @@ class PerformanceTestCase(unittest.TestCase):
|
|||||||
enumerable = timeit.timeit(lambda: Enumerable(int, self.values), number=COUNT)
|
enumerable = timeit.timeit(lambda: Enumerable(int, self.values), number=COUNT)
|
||||||
iterable = timeit.timeit(lambda: Iterable(int, self.values), number=COUNT)
|
iterable = timeit.timeit(lambda: Iterable(int, self.values), number=COUNT)
|
||||||
|
|
||||||
|
print('Range')
|
||||||
print(f'd: {default}')
|
print(f'd: {default}')
|
||||||
print(f'i: {iterable}')
|
print(f'i: {iterable}')
|
||||||
print(f'e: {enumerable}')
|
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)
|
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)
|
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'd: {default}')
|
||||||
print(f'i: {iterable}')
|
print(f'i: {iterable}')
|
||||||
print(f'e: {enumerable}')
|
print(f'e: {enumerable}')
|
||||||
|
Loading…
Reference in New Issue
Block a user