Made it not crash if the class contains functions.

This commit is contained in:
The Wobbler 2024-02-25 16:51:10 +01:00
parent 246875690f
commit f5e44bf45f

View file

@ -1,5 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os
import json import json
@ -102,15 +103,20 @@ class DataclassJSONFile:
# these 2 functions do the exact same thing as in the class above # these 2 functions do the exact same thing as in the class above
def load_dataclass_json(dataclass, file_path: str): def load_dataclass_json(dataclass, file_path: str):
if os.path.exists(file_path):
file = open(file_path, "r") file = open(file_path, "r")
class_dict = json.load(file) class_dict = json.load(file)
file.close() file.close()
else:
class_dict = {}
return dataclass(**class_dict) return dataclass(**class_dict)
def save_dataclass_json(class_instance, file_path: str): def save_dataclass_json(class_instance, file_path: str):
class_dict = class_instance.__dict__ class_dict = class_instance.__dict__
class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items()))
file = open(file_path, "w") file = open(file_path, "w")
json.dump(class_dict, file) json.dump(class_dict, file)