Added a class that can save dataclasses to a json file.
This commit is contained in:
parent
6292f0b5e9
commit
65d7dbd043
2 changed files with 101 additions and 51 deletions
101
data_file.py
Normal file
101
data_file.py
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
import json
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
|
class DictFile:
|
||||||
|
"""
|
||||||
|
This class is not safe to use!
|
||||||
|
Any code in the file will be executed!
|
||||||
|
"""
|
||||||
|
def __init__(self, path: str=None):
|
||||||
|
self.path = path
|
||||||
|
|
||||||
|
if path is None:
|
||||||
|
self.settings = {}
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.load_from_file(path)
|
||||||
|
|
||||||
|
def load_from_file(self, path):
|
||||||
|
file = open(path, "r")
|
||||||
|
self.settings = eval(file.read())
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
def save(self, path: str=None):
|
||||||
|
if path is None:
|
||||||
|
path = self.path
|
||||||
|
|
||||||
|
file = open(path, "w")
|
||||||
|
file.write(str(self))
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
new_settings_str = "{\n"
|
||||||
|
for key in self.settings:
|
||||||
|
new_settings_str += "\t" + repr(key) + ": " + repr(self.settings[key]) + ",\n"
|
||||||
|
|
||||||
|
new_settings_str = new_settings_str[:-2] + "\n}"
|
||||||
|
|
||||||
|
return new_settings_str
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return repr(self.settings)
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return self.settings[key]
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
self.settings[key] = value
|
||||||
|
|
||||||
|
def __delitem__(self, key):
|
||||||
|
self.settings.pop(key)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self.settings
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.settings)
|
||||||
|
|
||||||
|
|
||||||
|
class DataclassJSONFile:
|
||||||
|
def __init__(self, file_path, dataclass, class_instance=None):
|
||||||
|
self.file_path = file_path
|
||||||
|
self.dataclass = dataclass
|
||||||
|
self.class_instance = class_instance
|
||||||
|
|
||||||
|
if class_instance is None:
|
||||||
|
self.load_from_file(file_path)
|
||||||
|
|
||||||
|
def load_from_file(self, file_path):
|
||||||
|
file = open(file_path, "r")
|
||||||
|
class_dict = json.load(file)
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
self.class_instance = self.dataclass(**class_dict)
|
||||||
|
|
||||||
|
def save_to_file(self):
|
||||||
|
class_dict = self.class_instance.__dict__
|
||||||
|
|
||||||
|
file = open(self.file_path, "w")
|
||||||
|
json.dump(class_dict, file)
|
||||||
|
file.close()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return repr(self.class_instance)
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
return getattr(self.class_instance, key)
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
setattr(self.class_instance, key, value)
|
||||||
|
|
||||||
|
def __delitem__(self, key):
|
||||||
|
delattr(self.class_instance, key)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self.class_instance.__iter__()
|
||||||
|
|
||||||
|
def __len__(self):
|
||||||
|
return len(self.class_instance.__dict__)
|
51
file_dict.py
51
file_dict.py
|
@ -1,51 +0,0 @@
|
||||||
#!/usr/bin/python3
|
|
||||||
|
|
||||||
class FileDict:
|
|
||||||
def __init__(self, path: str=None):
|
|
||||||
self.path = path
|
|
||||||
|
|
||||||
if path is None:
|
|
||||||
self.settings = {}
|
|
||||||
|
|
||||||
else:
|
|
||||||
self.load_from_file(path)
|
|
||||||
|
|
||||||
def load_from_file(self, path):
|
|
||||||
file = open(path, "r")
|
|
||||||
self.settings = eval(file.read())
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
def save(self, path: str=None):
|
|
||||||
if path is None:
|
|
||||||
path = self.path
|
|
||||||
|
|
||||||
file = open(path, "w")
|
|
||||||
file.write(str(self))
|
|
||||||
file.close()
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
new_settings_str = "{\n"
|
|
||||||
for key in self.settings:
|
|
||||||
new_settings_str += "\t" + repr(key) + ": " + repr(self.settings[key]) + ",\n"
|
|
||||||
|
|
||||||
new_settings_str = new_settings_str[:-2] + "\n}"
|
|
||||||
|
|
||||||
return new_settings_str
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return repr(self.settings)
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
|
||||||
return self.settings[key]
|
|
||||||
|
|
||||||
def __setitem__(self, key, value):
|
|
||||||
self.settings[key] = value
|
|
||||||
|
|
||||||
def __delitem__(self, key):
|
|
||||||
self.settings.pop(key)
|
|
||||||
|
|
||||||
def __iter__(self):
|
|
||||||
return self.settings
|
|
||||||
|
|
||||||
def __len__(self):
|
|
||||||
return len(self.settings)
|
|
Loading…
Reference in a new issue