-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_from_folder.py
More file actions
60 lines (47 loc) · 1.56 KB
/
Copy pathload_from_folder.py
File metadata and controls
60 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
60
from os import listdir, mkdir, remove, rmdir
from os.path import isfile, join, isdir
import json
class LoadAndSaveData:
def __init__(self, dir):
self.dir = dir
if not isdir(dir):
mkdir(dir)
def __read_json(self, file_path):
with open(file_path, 'r') as target:
return json.load(target)
def __save_json(self, file_path, data):
with open(file_path, 'w') as target:
return json.dump(data, target)
def __get_dir_files(self):
only_files = [join(self.dir, f) for f in listdir(self.dir)
if isfile(join(self.dir, f)) and f[-5:].lower() == '.json']
return only_files
def load_data_set(self):
result = []
for item in self.__get_dir_files():
try:
result.append(self.__read_json(item))
except:
pass
return result
def save_data_set(self, data_set):
for i in range(len(data_set)):
self.__save_json(join(self.dir, str(i) + '.json'), data_set[i])
def clear_data_set(self):
try:
for path in self.__get_dir_files():
remove(path)
rmdir(self.dir)
except:
pass
if __name__ == "__main__":
test_data_set = [
{'inputs': [1, 1], 'result': 1},
{'inputs': [1, 0], 'result': -1},
{'inputs': [0, 1], 'result': -1},
{'inputs': [0, 0], 'result': -1},
]
x = LoadAndSaveData('./test')
x.save_data_set(test_data_set)
print(x.load_data_set())
x.clear_data_set()