File Handler#

(hivenas.utils.file_handler)

HiveNAS file-handling methods.

class FileHandler[source]#

Bases: object

Wrapper for file-handling methods

static create_dir(path)[source]#

Recursively creates new directory if it does not exist

Parameters

path (str) – directory path to be created

static export_yaml(config_dict, path, filename, file_version_comment='', force_dir=True)[source]#

Exports a given dictionary to a yaml file

Parameters
  • config_dict (dict) – dictionary to be saved as yaml

  • path (str) – save directory path

  • filename (str) – output filename

  • file_version_comment (str, optional) – optional string to be prepended at the top of the yaml file as a comment (typically used to highlight the configuration version)

  • force_dir (bool, optional) – whether or not to force create the directory if it does not exist

Returns

save operation status

Return type

bool

static load_df(path, default_df=None)[source]#

Loads a pandas.DataFrame

Parameters
  • path (str) – path to the dataframe

  • default_df (None, optional) – default dictionary to return if the dataframe does not exist (defaults to empty dataframe)

Returns

loaded dataframe or default

Return type

pandas.DataFrame

static load_pickle(path, default_dict={})[source]#

Loads pickle and returns decoded dictionary

Parameters
  • path (str) – path to pickle file (includes filename)

  • default_dict (dict, optional) – default dictionary to return if the pickle does not exist (defaults to { })

Returns

loaded data

Return type

dict

static load_yaml(path, loader, default_dict={})[source]#

Loads a yaml config file and returns it as dict

Parameters
  • path (str) – path to the yaml file (includes filename)

  • loader (yaml.SafeLoader) – yaml custom loader defined in export_yaml

  • default_dict (dict, optional) – default dictionary to return if the yaml file does not exist (defaults to { })

Returns

loaded yaml data

Return type

dict

static path_must_exist(path)[source]#

Checks if file exists and raises error if it is not. Used when the logic of the algorithm depends on the loaded file

Parameters

path (str) – path to file

Raises

FileNotFoundError – file does not exist

static save_df(df, path, filename, force_dir=True)[source]#

Saves a Pandas DataFrame to the given path

Parameters
  • df (pandas.DataFrame) – dataframe to be saved

  • path (str) – save directory path

  • filename (str) – output filename

  • force_dir (bool, optional) – whether or not to force create the directory if it does not exist

Returns

save operation status

Return type

bool

static save_pickle(p_dict, path, filename, force_dir=True)[source]#

Saves the given dictionary as a pickle

Parameters
  • p_dict (dict) – data to be saved

  • path (str) – path to save directory

  • filename (str) – output filename

  • force_dir (bool, optional) – whether or not to force create the directory if it does not exist

Returns

save operation status

Return type

bool

static validate_path(path)[source]#

Ensures that a given directory path is universaly valid (Windows/Linux/MacOS/POSIX) and creates it.

Prompts user for overwriting (using PromptHandler) if it already exists

Parameters

path (str) – path to validated

Returns

validity of the given path

Return type

bool