Added some descriptions.

This commit is contained in:
The Wobbler 2024-03-14 18:36:52 +01:00
parent f5e44bf45f
commit 45ad97fc13

View file

@ -1,5 +1,10 @@
#!/usr/bin/python3 #!/usr/bin/python3
"""
With this module, you can store data in files easyly.
"""
import os import os
import json import json
@ -60,6 +65,10 @@ class DictFile:
class DataclassJSONFile: class DataclassJSONFile:
"""
Store a dataclass in a JSON file.
"""
def __init__(self, file_path, dataclass, class_instance=None): def __init__(self, file_path, dataclass, class_instance=None):
self.file_path = file_path self.file_path = file_path
self.dataclass = dataclass self.dataclass = dataclass
@ -101,8 +110,11 @@ class DataclassJSONFile:
return len(self.class_instance.__dict__) return len(self.class_instance.__dict__)
# these 2 functions do the exact same thing as 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):
"""
Loads a dataclass instance from a json file.
"""
if os.path.exists(file_path): 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)
@ -115,6 +127,9 @@ def load_dataclass_json(dataclass, file_path: str):
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.
"""
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()))