"""Utilities for checking things."""# Created by Wenjie Du <wenjay.du@gmail.com># License: BSD-3-Clauseimportosfrom.loggingimportlogger
[docs]defextract_parent_dir(path:str)->str:"""Extract the given path's parent directory. Parameters ---------- path : The path for extracting. Returns ------- parent_dir : The path to the parent dir of the given path. """parent_dir=os.path.abspath(os.path.join(path,".."))returnparent_dir
[docs]defcreate_dir_if_not_exist(path:str,is_dir:bool=True)->None:"""Create the given directory if it doesn't exist. Parameters ---------- path : The path for check. is_dir : Whether the given path is to a directory. If `is_dir` is False, the given path is to a file or an object, then this file's parent directory will be checked. """path=extract_parent_dir(path)ifnotis_direlsepathifnotos.path.exists(path):os.makedirs(path,exist_ok=True)logger.info(f"Successfully created the given path {path}")
[docs]defget_class_full_path(cls)->str:"""Get the full path of the given class. Parameters ---------- cls: The class to get the full path. Returns ------- path : The full path of the given class. """module=cls.__module__path=cls.__qualname__ifmoduleisnotNoneandmodule!="__builtin__":path=module+"."+pathreturnpath