-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
91 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import json | ||
import pathlib | ||
import platform | ||
|
||
import jsmin | ||
|
||
import lussac | ||
|
||
|
||
class LussacParams: | ||
|
||
@staticmethod | ||
def load_from_string(params: str, params_folder: pathlib.Path | str | None = None): | ||
""" | ||
Loads the parameters from a string and returns them as a dict. | ||
@param params: str | ||
Lussac's parameters. | ||
@param params_folder: str | ||
Path to replace the "$PARAMS_FOLDER". | ||
""" | ||
|
||
if params_folder is not None: | ||
params_folder = str(pathlib.Path(params_folder).absolute()) | ||
params = params.replace("$PARAMS_FOLDER", params_folder) | ||
if platform.system() == "Windows": # pragma: no cover (OS specific). | ||
params = params.replace("\\", "\\\\") | ||
|
||
return json.loads(params) | ||
|
||
@staticmethod | ||
def load_from_json_file(filename: str, params_folder: pathlib.Path | str | None = None) -> dict: | ||
""" | ||
Loads the JSON parameters file and returns its content as a dict. | ||
@param filename: str | ||
Path to the file containing Lussac's parameters. | ||
@param params_folder: Path | str | None | ||
Path to replace the "$PARAMS_FOLDER". | ||
If None (default), will use the parent folder of the filename. | ||
@return params: dict | ||
Lussac's parameters. | ||
""" | ||
|
||
if params_folder is None: | ||
params_folder = str(pathlib.Path(filename).parent.absolute()) | ||
else: | ||
params_folder = str(pathlib.Path(params_folder).absolute()) | ||
|
||
with open(filename) as json_file: | ||
minified = jsmin.jsmin(json_file.read()) # Parses out comments. | ||
return LussacParams.load_from_string(minified, params_folder) | ||
|
||
@staticmethod | ||
def load_default_params(name: str, folder: pathlib.Path | str) -> dict: | ||
""" | ||
Loads the default parameters from the "params_example" folder. | ||
@param name: str | ||
The name of the default params file to load. | ||
@param folder: str | ||
Path to the folder where to create the "lussac" folder. | ||
@return params: dict | ||
The default parameters. | ||
""" | ||
|
||
if not name.startswith("params_"): | ||
name = f"params_{name}" | ||
if not name.endswith(".json"): | ||
name = f"{name}.json" | ||
|
||
params_folder = pathlib.Path(lussac.__file__).parent.parent.parent / "params_examples" | ||
file = params_folder / name | ||
|
||
return LussacParams.load_from_json_file(str(file), folder) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from lussac.core import LussacParams | ||
|
||
|
||
def test_load_default_params() -> None: | ||
params = LussacParams.load_default_params('params_synthetic', '/aze/') | ||
print(params) | ||
assert params['lussac']['tmp_folder'] == "/aze/lussac/tmp" |