load_dataclass_json() now automatically adds a save function to the dataclass.
This commit is contained in:
parent
742199873c
commit
5ed48e4d7c
2 changed files with 17 additions and 3 deletions
19
data_file.py
19
data_file.py
|
@ -113,9 +113,14 @@ class DataclassJSONFile:
|
||||||
|
|
||||||
|
|
||||||
# these 2 functions do the exact same thing as the functions in the class above
|
# these 2 functions do the exact same thing as the functions in the class above
|
||||||
def load_dataclass_json(dataclass, file_path: str):
|
def load_dataclass_json(dataclass, file_path: str, builtin_save: bool=True):
|
||||||
"""
|
"""
|
||||||
Loads a dataclass instance from a json file.
|
Loads a dataclass instance from a json file.
|
||||||
|
|
||||||
|
:param dataclass: The dataclass from which the instance will be created.
|
||||||
|
:param str file_path: The path to the json file from which the data gets loaded.
|
||||||
|
:param bool builtin_save: If True, you can simply call instance.save(file_path) to store the data.
|
||||||
|
:return: An instance of the dataclass containing the data from the json file.
|
||||||
"""
|
"""
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
file = open(file_path, "r")
|
file = open(file_path, "r")
|
||||||
|
@ -125,15 +130,23 @@ def load_dataclass_json(dataclass, file_path: str):
|
||||||
else:
|
else:
|
||||||
class_dict = {}
|
class_dict = {}
|
||||||
|
|
||||||
return dataclass(**class_dict)
|
instance = dataclass(**class_dict)
|
||||||
|
|
||||||
|
if builtin_save:
|
||||||
|
dataclass.save = save_dataclass_json
|
||||||
|
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
def save_dataclass_json(class_instance, file_path: str):
|
def save_dataclass_json(class_instance, file_path: str):
|
||||||
"""
|
"""
|
||||||
Saves a dataclass instance to a json file.
|
Saves a dataclass instance to a json file.
|
||||||
|
:param class_instance: The instance of the dataclass to be saved.
|
||||||
|
:param str file_path: The path to the file in which the data gets written.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class_dict = class_instance.__dict__
|
class_dict = class_instance.__dict__
|
||||||
class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items()))
|
class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items())) # filter out functions
|
||||||
|
|
||||||
file = open(file_path, "w")
|
file = open(file_path, "w")
|
||||||
json.dump(class_dict, file)
|
json.dump(class_dict, file)
|
||||||
|
|
1
text.py
1
text.py
|
@ -114,6 +114,7 @@ def rsap(old: str, replace: str, position: int):
|
||||||
def find_nth_occurrence(string: str, substring: str, n: int):
|
def find_nth_occurrence(string: str, substring: str, n: int):
|
||||||
"""
|
"""
|
||||||
Find the nth occurrence of a substring in a string.
|
Find the nth occurrence of a substring in a string.
|
||||||
|
|
||||||
:param int n: 0 is the first occurrence
|
:param int n: 0 is the first occurrence
|
||||||
:return: -1 when the substring was not found, 0 is the first character.
|
:return: -1 when the substring was not found, 0 is the first character.
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue