use save path relative to script file
This commit is contained in:
parent
9b0fe5b174
commit
1ef7e8beea
1 changed files with 15 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
import os
|
||||
import pickle
|
||||
from typing import List
|
||||
|
||||
|
@ -20,24 +21,30 @@ class SimulationList:
|
|||
def append(self, value: Simulation):
|
||||
self.simlist.append(value)
|
||||
|
||||
def pickle_save(self, filebasename="save"):
|
||||
with open(f'{filebasename}.pickle', 'wb') as file:
|
||||
def save_path(self, extension):
|
||||
script_dir = os.path.dirname(__file__)
|
||||
rel_path = "save" + extension
|
||||
return os.path.join(script_dir, rel_path)
|
||||
|
||||
def pickle_save(self):
|
||||
with open(self.save_path(".pickle"), 'wb') as file:
|
||||
pickle.dump(self.simlist, file)
|
||||
|
||||
@classmethod
|
||||
def pickle_load(cls, filebasename="save"):
|
||||
with open(f'{filebasename}.pickle', 'rb') as file:
|
||||
def pickle_load(cls):
|
||||
tmp = cls()
|
||||
with open(cls.save_path(tmp, ".pickle"), 'rb') as file:
|
||||
return cls(pickle.load(file))
|
||||
|
||||
def jsonlines_save(self, filebasename="save"):
|
||||
with open(f'{filebasename}.jsonl', 'w') as file:
|
||||
def jsonlines_save(self):
|
||||
with open(self.save_path(".jsonl"), 'w') as file:
|
||||
for sim in self.simlist:
|
||||
file.write(json.dumps(vars(sim)) + "\n")
|
||||
|
||||
@classmethod
|
||||
def jsonlines_load(cls, filebasename="save"):
|
||||
def jsonlines_load(cls):
|
||||
simlist = cls()
|
||||
with open(f'{filebasename}.jsonl', 'r') as file:
|
||||
with open(cls.save_path(simlist, ".jsonl"), 'r') as file:
|
||||
for line in file:
|
||||
sim = Simulation.from_dict(json.loads(line))
|
||||
simlist.append(sim)
|
||||
|
|
Reference in a new issue