import os import pickle import numpy class DataPockets: def __init__(self, filename='default', directory_path='.', load_if_exists=True) -> None: self.memory = {} self.filename = filename if filename.endswith('.pickle') else filename+'.pickle' self.directory_path = directory_path if directory_path.endswith('/') else directory_path+'/' if not os.path.exists(self.directory_path): os.makedirs(self.directory_path) if load_if_exists and os.path.exists(self.directory_path+self.filename): self.load() def append(self, pocket_name, value, erase_first=False): if pocket_name not in self.memory or erase_first: self.memory[pocket_name] = [] if isinstance(value, float) or isinstance(value, int): self.memory[pocket_name].append(value) elif isinstance(value, list) or isinstance(value, numpy.ndarray): for v in value: self.memory[pocket_name].append(v) def append_from_file(self, pocket_name, source_filename, erase_first=False): if pocket_name not in self.memory or erase_first: self.memory[pocket_name] = [] with open(file=source_filename, mode="r") as f: for value in f.readlines(): formatted_value = value.strip() if formatted_value != '': self.memory[pocket_name].append(float(formatted_value)) def append_as_it(self, pocket_name, what_to_append, erase_first=False): if pocket_name not in self.memory or erase_first: self.memory[pocket_name] = [] self.memory[pocket_name].append(what_to_append) def get(self, pocket_name): assert pocket_name in self.memory return self.memory[pocket_name] def save(self): with open(file=self.directory_path+self.filename, mode="wb") as f: pickle.dump(self.memory, f) def load(self): with open(file=self.directory_path+self.filename, mode="rb") as f: self.memory = pickle.load(f) def print(self, pocket_name=None): if pocket_name is not None: assert pocket_name in self.memory print(pocket_name, self.memory[pocket_name]) else: if len(self.memory)==0: print("Empty !!!") else: for name in self.memory: print(name, self.memory[name]) def print_names(self): for name in self.memory: print(name) def clear_pockets(self): del self.memory self.memory = {} def clear_pocket(self, pocket_name): assert pocket_name in self.memory del self.memory[pocket_name] def help(self): print("LIST OF AVAILABLE METHODS AND THEIR EXPLANATIONS") print("================================================") print("append : it appends a new value to (an existing) pocket identified by its name") print("prototype => append(pocket_name, value, erase_first=False)") print("================================================") print("append_from_file : reads a file and appends its values (one value per line) pocket identified by pocket_name") print("prototype => append_from_file(pocket_name, filename, erase_first=False") print("================================================") print("get : it returns an existing pocket identified by its name") print("prototype => get(pocket_name)") print("================================================") print("save : it saves the current DataPocket into a local file named by the value of 'filename' gived at instantiation") print("prototype => save()") print("================================================") print("load : it loads a previouly saved DataPocket from local file") print("prototype => load()") print("================================================") print("print : shows the contents of all pockets (pocket_name unspecified) or a specific pocket in the current DataPocket") print("prototype => print(pocket_name=None)") print("================================================") print("print_names : shows only names of all pockets available in current DataPocket") print("prototype => print_names()") print("================================================") print("clear_pockets : deletes all pockets from current DataPocket (won't affect the local file until one calls 'save' function)") print("prototype => clear_pockets()") print("================================================") print("clear_pocket : delete a specific pocket from current DataPocket (won't affect the local file until one calls 'save' function)") print("prototype => clear_pocket()") print("================================================") if __name__=='__main__': dtpckts = DataPockets(filename='test', directory_path="mydir") #dtpckts.print() #dtpckts.append("t", [1,2,3,4,5]) #dtpckts.print() #dtpckts.save() dtpckts.print()