-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterator_class.py
More file actions
52 lines (43 loc) · 1.54 KB
/
Iterator_class.py
File metadata and controls
52 lines (43 loc) · 1.54 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
import csv
import typing
from typing import Optional
def read_file(path: str) -> list[list[str]]:
'''Читает файл path и возвращает матрицу имён файлов
Parameters
----------
path(str) : Файл path
Имя класса
Returns
-------
list[list[str]]
Матрица имён файлов'''
files: list[list[str]] = []
csvfile: typing.TextIO = open(path, "r")
reader: csv.DictReader = csv.DictReader(csvfile, delimiter=",")
for row in reader:
files.append([row["full_path"], row["class"]])
return files
class FileIterator:
'''Класс-итератор для получения полного пути к файлу'''
def __init__(self, files_class: str, path: str) -> None:
self.file_class: str = files_class
csv_f: list[list[str]] = read_file(path)
self.annotation: list[list[str]] = []
for i in csv_f:
if self.file_class == i[1]:
self.annotation.append(i)
def __next__(self) -> Optional[str]:
if self.annotation:
item_0: str = self.annotation[0][0]
self.annotation: list[list[str]] = self.annotation[1:]
return item_0
else:
return None
if __name__ == "__main__":
path_to_annotation: str = "annotation.csv"
fileIterator: FileIterator = FileIterator("cat", path_to_annotation)
while True:
next_val: Optional[str] = next(fileIterator)
print(next_val)
if next_val == None:
break