-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterators.py
46 lines (38 loc) · 1.3 KB
/
iterators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class PopIterator:
def __init__(self, popable):
"""
batch_size=1
batch_size is not implemented
"""
self.popable = popable
self.rest = self.total = len(popable)
def __iter__(self):
return self
def __next__(self):
if len(self.popable) > 0:
ret = self.popable.pop(0)
self.rest -= 1
return ret
else:
raise StopIteration
class PandasIterator:
def __init__(self, pandas_obj, batch_size=1, start_index=0):
self.pandas_obj = pandas_obj
self.batch_size = batch_size
self.start_index = start_index
self.current_position = self.start_index
assert start_index < len(self), f"start_index should be less than {len(self)}"
def __len__(self):
return ceil(len(self.pandas_obj) / self.batch_size)
def __iter__(self):
return self
def __next__(self):
if len(self) > self.current_position:
start = self.current_position * self.batch_size
end = (self.current_position + 1) * self.batch_size
ret = self.pandas_obj.iloc[start:end]
self.current_position += 1
return ret
else:
self.current_position = self.start_index
raise StopIteration