-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiterator.py
More file actions
137 lines (95 loc) · 4.53 KB
/
iterator.py
File metadata and controls
137 lines (95 loc) · 4.53 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import csv
import datetime
import os
class DateIterator:
def __init__(self, path):
self.counter = 0
self.file_name = path
def __next__(self) -> tuple:
if os.path.exists(self.file_name):
with open(self.file_name, 'r', encoding='utf-8') as csvfile:
reader_object = list(csv.reader(csvfile, delimiter=","))
if self.counter == len(reader_object):
raise StopIteration
elif self.counter < len(reader_object):
self.counter += 1
output = (reader_object[self.counter - 1][0], reader_object[self.counter - 1][1], reader_object[self.counter - 1][2],
reader_object[self.counter - 1][3], reader_object[self.counter - 1][4], reader_object[self.counter - 1][5], reader_object[self.counter - 1][6])
return output
else:
raise FileNotFoundError
class DateIteratorFromXY:
def __init__(self, path_to_x: str , path_to_y: str):
self.counter = 0
self.X = path_to_x
self.Y = path_to_y
def __next__(self) -> tuple:
if os.path.exists(self.X) and os.path.exists(self.Y):
with open(self.X, 'r', encoding='utf-8') as csvfile:
reader_object = list(csv.reader(csvfile, delimiter=","))
if self.counter == len(reader_object):
raise StopIteration
elif self.counter < len(reader_object):
self.counter += 1
with open(self.X, 'r', encoding='utf-8') as csvfilex:
reader_object_X = list(
csv.reader(csvfilex, delimiter=","))
date = reader_object_X[self.counter][0]
with open(self.Y, 'r', encoding='utf-8') as csvfiley:
reader_object_Y = list(
csv.reader(csvfiley, delimiter=","))
output = (date, reader_object_Y[self.counter - 1][0], reader_object_Y[self.counter - 1][1], reader_object_Y[self.counter - 1]
[2], reader_object_Y[self.counter - 1][3], reader_object_Y[self.counter - 1][4], reader_object_Y[self.counter - 1][5])
return output
else:
raise FileNotFoundError
class DateIteratorFromWeeks:
def __init__(self, path: str):
self.file_name = path
self.counter = 0
self.data = []
if os.path.exists(self.file_name):
for root, dirs, files in os.walk(self.file_name):
for file in files:
with open(os.path.join(self.file_name, file), 'r', encoding='utf-8') as csvfile:
dates = list(csv.reader(csvfile, delimiter=","))
for i in range(len(dates)):
self.data.append(dates[i])
else:
raise FileNotFoundError
def __next__(self) -> tuple:
if self.counter == len(self.data):
raise StopIteration
elif self.counter < len(self.data):
self.counter += 1
output = (self.data[self.counter - 1][0], self.data[self.counter - 1][1], self.data[self.counter - 1][2],
self.data[self.counter - 1][3], self.data[self.counter - 1][4], self.data[self.counter - 1][5], self.data[self.counter - 1][6])
return output
class DateIteratorFromYears:
def __init__(self, path: str):
self.file_name = path
self.counter = 0
self.data = []
if os.path.exists(self.file_name):
for root, dirs, files in os.walk(self.file_name):
for file in files:
with open(os.path.join(self.file_name, file), 'r', encoding='utf-8') as csvfile:
dates = list(csv.reader(csvfile, delimiter=","))
for i in range(len(dates)):
self.data.append(dates[i])
else:
raise FileNotFoundError
def __next__(self) -> tuple:
if self.counter == len(self.data):
raise StopIteration
elif self.counter < len(self.data):
self.counter += 1
output = (
self.data[self.counter - 1][0],
self.data[self.counter - 1][1],
self.data[self.counter - 1][2],
self.data[self.counter - 1][3],
self.data[self.counter - 1][4],
self.data[self.counter - 1][5],
self.data[self.counter - 1][6])
return output