def addFileHandler(cls, file_name: str, file_dir=None) -> logging.FileHandler: """ 直接添加文件的handler :param file_name: str :param file_dir: None or str :return: """ if file_dir is None: file_dir = cls._instance.log_sub_dir # 断言保证 assertor.type_assert(file_name, str) # 判别并创建文件夹 judge_mkdir(file_dir) # 创建file handler formatter = logging.Formatter(cls._instance.log_config.log_format) log_file_path = yy.io.join(file_dir, file_name) file_handler = logging.FileHandler(log_file_path) file_handler.setFormatter(formatter) # 设置file handler cls.addHandler(file_handler) return file_handler
def save_list_to_list_file(input_list: list, save_path: str): assertor.type_assert(input_list, list) assertor.file_exist_assert(file_path) with open(file_path, 'w') as f: f.write(str(input_list)) return rst_list
def fatal(cls, msg: str) -> None: """ DEGUG信息 :param msg: str :return: """ # 断言保证 assertor.type_assert(msg, str) cls._instance.logger.fatal(msg)
def log_dir(self, value: str): """ 判别传入的value文件是否存在,若不存在则创建 :param value: str :return: """ # 断言保证 assertor.type_assert(value, str) judge_mkdir(value) self._log_dir = value
def mkdir(path: str) -> None: """ 创建文件夹 :param path: str :return: None """ # 断言保证 assertor.type_assert(path, str) assertor.not_equal_assert(path, '') # 去除首空格 path = path.strip() os.makedirs(path)
def judge_mkdir(path: str) -> None: """ 判别位置,如果不存在则创建文件夹 :param path: str :return: None """ # 断言保证 assertor.type_assert(path, str) assertor.not_equal_assert(path, '') # 去除首空格 path = path.strip() # 判别是否存在路径,如果不存在则创建 if not os.path.exists(path): mkdir(path)
def __new__(cls, *config_path: tuple): """ 接受config path来new一个单件configer, 不应该直接实例化configer而应该使用init_configer方法 :param config_path: tuple :return Configer """ # 断言保证 assertor.type_assert(config_path, tuple) # 如果类维护的实例为None就创建一个新的,否则直接返回 if Configer._instance is None: Configer._instance = object.__new__(cls) Configer._instance.config = Configer._instance._get_config_parser(*config_path) return Configer._instance
def del_file(file_dir: str, file_name: str) -> None: """ 删除目标文件 :param file_dir: str :param file_name: str :return: None """ # 断言保证 assertor.type_assert(file_dir, str) assertor.type_assert(file_name, str) file_path = join(file_dir, file_name) # 断言保证 assertor.file_exist_assert(file_path) os.remove(file_path)
def __new__(cls, *config_path: tuple): """ Train的Configer的初始化 :param config_path: tuple :return: TrainConfiger """ # 断言保证 assertor.type_assert(config_path, tuple) # 调基类的new方法 instance = Configer.__new__(cls, *config_path) instance._data = DataConfiger(instance.config) instance._log = LogConfiger(instance.config) instance._model = ModelConfiger(instance.config) instance._version = instance.config.get('basic', 'version') return instance
def _check_label_exist(cls, rule: str, filename: str) -> int: """ 通过正则表达式来判断文件名和rule一致 :param rule: str,正则的规则 :param filename: str,文件的名字 :return: int, 表达是否正确, 方便下一步计算匹配个数 """ # 断言保证输入 assertor.type_assert(rule, str) assertor.type_assert(filename, str) # 进行正则匹配 res = re.match(rule, filename, re.IGNORECASE) if res: print('----Find %s' % res.group()) return 1 else: return 0
def check_labels_exists(cls, dir_path: str, rules: list, label_dirname='1') -> None: """ 检查labels是否存在且是否齐全 :param dir_path: str,文件夹的路径 :param rules: list,匹配的规则 :param label_dirname: str,label文件夹的名字 :return: None """ # 断言保证 assertor.path_exist_assert(dir_path) assertor.list_type_assert(rules, str) assertor.type_assert(label_dirname, str) dir_end = dir_path.split(os.sep)[-1] for (root, dirs, files) in os.walk(dir_path): if label_dirname in dirs: path_list = root.split(os.sep) for i in range(len(path_list)): if dir_end == path_list[i]: print('Dir %s' % path_list[i + 1]) break sub_path = os.path.join(root, label_dirname) print(os.path.join(root, sub_path).split('/')[-2]) match_num = cls._get_match_num(sub_path, rules) if match_num != len(rules): print( '---------------------------------------------------------------------' ) print( '---------------------------------------------------------------------' ) print('Warning: dir %s Not have all of the mask' % str(dir_path)) print( '---------------------------------------------------------------------' ) print( '---------------------------------------------------------------------' )