From f5e44bf45f24203d512136032c4b3790a89b8725 Mon Sep 17 00:00:00 2001 From: EKNr1 Date: Sun, 25 Feb 2024 16:51:10 +0100 Subject: [PATCH] Made it not crash if the class contains functions. --- data_file.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/data_file.py b/data_file.py index e3eb0ff..a8091e5 100644 --- a/data_file.py +++ b/data_file.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 +import os import json @@ -102,15 +103,20 @@ class DataclassJSONFile: # these 2 functions do the exact same thing as in the class above def load_dataclass_json(dataclass, file_path: str): - file = open(file_path, "r") - class_dict = json.load(file) - file.close() + if os.path.exists(file_path): + file = open(file_path, "r") + class_dict = json.load(file) + file.close() + + else: + class_dict = {} return dataclass(**class_dict) def save_dataclass_json(class_instance, file_path: str): class_dict = class_instance.__dict__ + class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items())) file = open(file_path, "w") json.dump(class_dict, file)