A helper module for managing paths and environments for the ValUtils project.
- Easy file system access
- Comprehensible API
- Default settings from dataclass_json
The preferred method of installation is through pip but if you know better use the package manager that you want.
pip install ValStorageValStorage contains the following methods:
list_dirto list a directorysave_to_driveto save a string to the filesystemread_from_driveto read a string to the filesystemjson_writeto write data as json to the filesystemjson_readto read data as json to the filesystemutils_pathto get the path forValUtils
And the special get_settings method explained down below.
from ValStorage import json_read, json_write, utils_path
data = json_read(utils_path / "data.json")
data["time"] = 100
json_write(data, utils_path / "data.json")In this use case we export all the functionality as a local module and also our local settingsPath referencing the module directory.
from ValStorage import *
def set_path():
global settingsPath
utilsPath = utils_path()
settingsPath = utilsPath / "test"
create_path(settingsPath)
set_path()The get_settings method takes a dataclass_json and a path as parameters and it retrieves or the settings in said path or the default settings.
For that our dataclass needs to have default values, here's a quick example:
from ValStorage import get_settings
from dataclass_json import DataClassJsonMixin
from dataclass import dataclass
@dataclass
class Data(DataClassJsonMixin):
time: float = 0
argument: string = "default"
get_settings(Data, "data.json")If data.json doesn't exist it gets filled with the default data provided by our dataclass.