mypkgs.utils#

Utility functions for configuration and environment management.

These functions are strictly decoupled from the project’s folder structure to ensure maximum reusability and testability.

Functions#

load_json_config(config_path)

Reads and parses a JSON configuration file.

prepare_output_dir(output_path)

Validates and prepares the designated output directory.

Module Contents#

mypkgs.utils.load_json_config(config_path)[source]#

Reads and parses a JSON configuration file.

The specified file is accessed in read mode, and its contents are decoded from JSON format into a Python dictionary. It is required that the root element of the targeted JSON file constitutes a valid JSON object (key-value pairs).

Parameters:

config_path (pathlib.Path or str) – The file path to the targeted JSON configuration file.

Returns:

A dictionary containing the parsed configuration parameters.

Return type:

dict[str, Any]

Raises:

Examples

>>> config = load_json_config("./configs/model_params.json")
>>> type(config)
<class 'dict'>
mypkgs.utils.prepare_output_dir(output_path)[source]#

Validates and prepares the designated output directory.

This function ensures that the specified output path exists. If the directory does not exist, it safely creates it along with any necessar parent directories.

Parameters:

output_path (pathlib.Path or str) – The target directory path where output files will be saved.

Returns:

The absolute path of the prepared output directory as a string.

Return type:

str

Raises:
  • PermissionError – If the program lacks the necessary permissions to create the directory.

  • OSError – If the path is invalid or creation fails for system-level reasons.

Examples

>>> prepared_path = prepare_output_dir("./results/experiment_1")
>>> print(prepared_path)
'/absolute/path/to/results/experiment_1'