-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiter.py
More file actions
59 lines (47 loc) · 1.56 KB
/
iter.py
File metadata and controls
59 lines (47 loc) · 1.56 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import csv
class Iterator:
def __init__(self, directory: str, name: str):
"""Сonstructor of the class object, return NONE.
Args:
directory (str): full path to the folder.
name (str): object class.
"""
self.directory = directory
self.name = name
self.count = -1
self.read_list = []
with open(directory, "r", encoding = "utf-8") as f:
r = csv.DictReader(f, fieldnames=["Absolut_path", "Relative_patch", "Class"], delimiter="|")
for i in r:
if i["Class"] == name:
self.read_list.append(i["Absolut_path"])
def __iter__(self):
"""Return iterator object.
Returns:
self: iterstor object.
"""
return self
def __next__(self):
"""Return the next element in the sequence.
Raises:
StopIteration: stopping the iterator.
Returns:
str: patch to the file.
"""
if self.count < len(self.read_list):
self.count += 1
return self.read_list[self.count]
elif self.count == len(self.read_list):
raise StopIteration
def main():
"""Separates code blocks."""
s = Iterator("D:\Lab Python\Lab_2\copy_patch.csv", "rose")
print(next(s))
print(next(s))
print(next(s))
t = Iterator("D:\Lab Python\Lab_2\copy_patch.csv", "tulip")
print(next(t))
print(next(t))
print(next(t))
if __name__ == "__main__":
main()