-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_saver.py
78 lines (57 loc) · 1.94 KB
/
data_saver.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
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
import os
class data_saver(object):
"""
data_saver encapsulates a cache of data to be saved as well as the underlying code to save the data
"""
def __init__(self, save_directory, state):
"""
Construct a new 'data_saver' object.
:param save_directory: The name of the directory within the /tests/ folder to be saved to
:return: returns nothing
"""
self.data_cache = []
self.save_dir = os.getcwd() + "/tests/" + save_directory
self.state = state
def add_data(self, line):
"""
Add a csv or other line to the cache of data to be saved
:param line: the csv or other line
:return: returns nothing
"""
self.data_cache.append(line)
def clear(self):
"""
Clear the data cache
:return: returns nothing
"""
self.data_cache.clear()
def save_data(self, mode):
"""
Command that creates and writes a new file based on the cache
:param mode: tell the object the mode, which informs the directory to
be saved in
:return: returns nothing
"""
path = f"{self.save_dir}/{mode}/"
try:
os.makedirs(path)
except OSError:
# print ("Creation of the directory %s failed" % path)
pass
else:
print ("Successfully created the directory %s" % path)
i = 0
while os.path.exists(f"{path}{mode}_data{i}.csv"):
i += 1
with open(f"{path}{mode}_data{i}.csv", "w") as file:
for line in self.data_cache:
file.write(line+"\n")
print(f"Successfully wrote to file {path}{mode}_data{i}.csv")
return
if __name__ == '__main__':
save = data_saver("test_test")
save.add_data("1,2,3,4")
save.add_data("2,5,1,5")
save.add_data("3,7,7,7")
save.add_data("4,8,8,8")
save.save_data("MVT_L")