Made it not crash if the class contains functions.
This commit is contained in:
parent
246875690f
commit
f5e44bf45f
1 changed files with 9 additions and 3 deletions
12
data_file.py
12
data_file.py
|
@ -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):
|
||||||
file = open(file_path, "r")
|
if os.path.exists(file_path):
|
||||||
class_dict = json.load(file)
|
file = open(file_path, "r")
|
||||||
file.close()
|
class_dict = json.load(file)
|
||||||
|
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)
|
||||||
|
|
Loading…
Reference in a new issue