-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses_of_iterator.py
More file actions
41 lines (34 loc) · 1.03 KB
/
classes_of_iterator.py
File metadata and controls
41 lines (34 loc) · 1.03 KB
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
import os
class Iterator:
def __init__(self, class_name, dataset_name):
self.dataset_name = dataset_name
self.class_name = class_name
class_dir = os.path.join(dataset_name, class_name)
if not os.path.exists(class_dir):
raise FileNotFoundError(f"Directory '{class_dir}' does not exist.")
self.paths = [
os.path.join(class_dir, filename)
for filename in os.listdir(class_dir)
]
self.index = 0
def __iter__(self):
self.index = 0
return self
def __next__(self):
if self.index < len(self.paths):
path = self.paths[self.index]
self.index += 1
return path
else:
raise StopIteration
if __name__ == "__main__":
cats = Iterator('cats', 'dataset')
dogs = Iterator('dogs', 'dataset')
print(next(cats))
print(next(dogs))
print(next(cats))
print(next(dogs))
print(next(cats))
print(next(dogs))
print(next(cats))
print(next(dogs))