Added some descriptions.
This commit is contained in:
parent
f5e44bf45f
commit
45ad97fc13
1 changed files with 16 additions and 1 deletions
17
data_file.py
17
data_file.py
|
@ -1,5 +1,10 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
"""
|
||||
With this module, you can store data in files easyly.
|
||||
"""
|
||||
|
||||
|
||||
import os
|
||||
import json
|
||||
|
||||
|
@ -60,6 +65,10 @@ class DictFile:
|
|||
|
||||
|
||||
class DataclassJSONFile:
|
||||
"""
|
||||
Store a dataclass in a JSON file.
|
||||
"""
|
||||
|
||||
def __init__(self, file_path, dataclass, class_instance=None):
|
||||
self.file_path = file_path
|
||||
self.dataclass = dataclass
|
||||
|
@ -101,8 +110,11 @@ class DataclassJSONFile:
|
|||
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):
|
||||
"""
|
||||
Loads a dataclass instance from a json file.
|
||||
"""
|
||||
if os.path.exists(file_path):
|
||||
file = open(file_path, "r")
|
||||
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):
|
||||
"""
|
||||
Saves a dataclass instance to a json file.
|
||||
"""
|
||||
class_dict = class_instance.__dict__
|
||||
class_dict = dict(filter(lambda pair: not callable(pair[1]), class_dict.items()))
|
||||
|
||||
|
|
Loading…
Reference in a new issue