|
@@ -0,0 +1,107 @@
|
|
1
|
+import os
|
|
2
|
+import pickle
|
|
3
|
+import numpy
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+class DataPockets:
|
|
7
|
+ def __init__(self, filename='default', directory_path='.', load_if_exists=True) -> None:
|
|
8
|
+ self.memory = {}
|
|
9
|
+ self.filename = filename if filename.endswith('.pickle') else filename+'.pickle'
|
|
10
|
+ self.directory_path = directory_path if directory_path.endswith('/') else directory_path+'/'
|
|
11
|
+ if not os.path.exists(self.directory_path): os.makedirs(self.directory_path)
|
|
12
|
+ if load_if_exists and os.path.exists(self.directory_path+self.filename): self.load()
|
|
13
|
+
|
|
14
|
+ def append(self, pocket_name, value, erase_first=False):
|
|
15
|
+ if pocket_name not in self.memory or erase_first:
|
|
16
|
+ self.memory[pocket_name] = []
|
|
17
|
+ if isinstance(value, float) or isinstance(value, int):
|
|
18
|
+ self.memory[pocket_name].append(value)
|
|
19
|
+ elif isinstance(value, list) or isinstance(value, numpy.ndarray):
|
|
20
|
+ for v in value: self.memory[pocket_name].append(v)
|
|
21
|
+
|
|
22
|
+ def append_from_file(self, pocket_name, source_filename, erase_first=False):
|
|
23
|
+ if pocket_name not in self.memory or erase_first:
|
|
24
|
+ self.memory[pocket_name] = []
|
|
25
|
+ with open(file=source_filename, mode="r") as f:
|
|
26
|
+ for value in f.readlines():
|
|
27
|
+ formatted_value = value.strip()
|
|
28
|
+ if formatted_value != '':
|
|
29
|
+ self.memory[pocket_name].append(float(formatted_value))
|
|
30
|
+
|
|
31
|
+ def append_as_it(self, pocket_name, what_to_append, erase_first=False):
|
|
32
|
+ if pocket_name not in self.memory or erase_first:
|
|
33
|
+ self.memory[pocket_name] = []
|
|
34
|
+ self.memory[pocket_name].append(what_to_append)
|
|
35
|
+
|
|
36
|
+ def get(self, pocket_name):
|
|
37
|
+ assert pocket_name in self.memory
|
|
38
|
+ return self.memory[pocket_name]
|
|
39
|
+
|
|
40
|
+ def save(self):
|
|
41
|
+ with open(file=self.directory_path+self.filename, mode="wb") as f:
|
|
42
|
+ pickle.dump(self.memory, f)
|
|
43
|
+
|
|
44
|
+ def load(self):
|
|
45
|
+ with open(file=self.directory_path+self.filename, mode="rb") as f:
|
|
46
|
+ self.memory = pickle.load(f)
|
|
47
|
+
|
|
48
|
+ def print(self, pocket_name=None):
|
|
49
|
+ if pocket_name is not None:
|
|
50
|
+ assert pocket_name in self.memory
|
|
51
|
+ print(pocket_name, self.memory[pocket_name])
|
|
52
|
+ else:
|
|
53
|
+ if len(self.memory)==0:
|
|
54
|
+ print("Empty !!!")
|
|
55
|
+ else:
|
|
56
|
+ for name in self.memory: print(name, self.memory[name])
|
|
57
|
+
|
|
58
|
+ def print_names(self):
|
|
59
|
+ for name in self.memory: print(name)
|
|
60
|
+
|
|
61
|
+ def clear_pockets(self):
|
|
62
|
+ del self.memory
|
|
63
|
+ self.memory = {}
|
|
64
|
+
|
|
65
|
+ def clear_pocket(self, pocket_name):
|
|
66
|
+ assert pocket_name in self.memory
|
|
67
|
+ del self.memory[pocket_name]
|
|
68
|
+
|
|
69
|
+ def help(self):
|
|
70
|
+ print("LIST OF AVAILABLE METHODS AND THEIR EXPLANATIONS")
|
|
71
|
+ print("================================================")
|
|
72
|
+ print("append : it appends a new value to (an existing) pocket identified by its name")
|
|
73
|
+ print("prototype => append(pocket_name, value, erase_first=False)")
|
|
74
|
+ print("================================================")
|
|
75
|
+ print("append_from_file : reads a file and appends its values (one value per line) pocket identified by pocket_name")
|
|
76
|
+ print("prototype => append_from_file(pocket_name, filename, erase_first=False")
|
|
77
|
+ print("================================================")
|
|
78
|
+ print("get : it returns an existing pocket identified by its name")
|
|
79
|
+ print("prototype => get(pocket_name)")
|
|
80
|
+ print("================================================")
|
|
81
|
+ print("save : it saves the current DataPocket into a local file named by the value of 'filename' gived at instantiation")
|
|
82
|
+ print("prototype => save()")
|
|
83
|
+ print("================================================")
|
|
84
|
+ print("load : it loads a previouly saved DataPocket from local file")
|
|
85
|
+ print("prototype => load()")
|
|
86
|
+ print("================================================")
|
|
87
|
+ print("print : shows the contents of all pockets (pocket_name unspecified) or a specific pocket in the current DataPocket")
|
|
88
|
+ print("prototype => print(pocket_name=None)")
|
|
89
|
+ print("================================================")
|
|
90
|
+ print("print_names : shows only names of all pockets available in current DataPocket")
|
|
91
|
+ print("prototype => print_names()")
|
|
92
|
+ print("================================================")
|
|
93
|
+ print("clear_pockets : deletes all pockets from current DataPocket (won't affect the local file until one calls 'save' function)")
|
|
94
|
+ print("prototype => clear_pockets()")
|
|
95
|
+ print("================================================")
|
|
96
|
+ print("clear_pocket : delete a specific pocket from current DataPocket (won't affect the local file until one calls 'save' function)")
|
|
97
|
+ print("prototype => clear_pocket()")
|
|
98
|
+ print("================================================")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+if __name__=='__main__':
|
|
102
|
+ dtpckts = DataPockets(filename='test', directory_path="mydir")
|
|
103
|
+ #dtpckts.print()
|
|
104
|
+ #dtpckts.append("t", [1,2,3,4,5])
|
|
105
|
+ #dtpckts.print()
|
|
106
|
+ #dtpckts.save()
|
|
107
|
+ dtpckts.print()
|