sh_cpl/src/cpl_query/base/sequence_values.py

50 lines
1.2 KiB
Python
Raw Normal View History

2022-09-13 19:33:26 +02:00
import io
import itertools
from cpl_query.exceptions import IndexOutOfRangeException
class SequenceValues:
def __init__(self, data, _t: type):
if data is None:
data = []
2022-09-14 23:01:52 +02:00
if len(data) > 0:
def type_check(_t: type, _l: list):
return all(isinstance(x, _t) for x in _l)
if not type_check(_t, data):
raise Exception(f'Unexpected type\nExpected type: {_t}')
2022-09-13 19:33:26 +02:00
if not hasattr(data, '__iter__'):
raise TypeError(f'{type(self).__name__} must be instantiated with an iterable object')
2022-09-15 17:00:22 +02:00
self._new_cycle = lambda: itertools.cycle(data)
self._len = lambda: len(data)
2022-09-13 19:33:26 +02:00
self._index = 0
2022-09-15 17:00:22 +02:00
self._cycle = self._new_cycle()
2022-09-13 19:33:26 +02:00
def __len__(self):
2022-09-15 17:00:22 +02:00
return self._len()
2022-09-13 19:33:26 +02:00
def __iter__(self):
i = 0
while i < len(self):
yield next(self._cycle)
i += 1
def __next__(self):
if self._index >= len(self):
raise IndexOutOfRangeException()
self._index += 1
return self.next()
def next(self):
return next(self._cycle)
def reset(self):
self._index = 0
2022-09-15 17:00:22 +02:00
self._cycle = self._new_cycle()