Support pickled datafiles. Not readable by humans, but way more versatile

This commit is contained in:
Stefan Harmuth 2022-05-04 10:39:32 +02:00
parent 1b4ef0276b
commit 8f8201d8c8

View File

@ -1,5 +1,6 @@
import json
import os
import pickle
class DataFile(dict):
@ -40,3 +41,21 @@ class JSONFile(DataFile):
def save(self):
with open(self.filename, "wt") as f:
f.write(json.dumps(self.copy(), indent=4))
class PickleFile(DataFile):
def __init__(self, filename: str, create: bool) -> None:
super().__init__(filename, create)
def load(self) -> None:
with open(self.filename, "rb") as f:
c = f.read()
if len(c) > 0:
pickle_dict = pickle.loads(c)
for k in pickle_dict:
self[k] = pickle_dict[k]
def save(self) -> None:
with open(self.filename, "wb") as f:
pickle.dump(self.copy(), f)