From 36e56afa4c21889904e031cf19414f556e8710ca Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Sun, 12 Jun 2022 14:13:21 +0200 Subject: [PATCH] Fixed weird code duplication --- src/bootstraparse/modules/error_mngr.py | 35 ------------------------- src/bootstraparse/modules/tools.py | 14 ++++++++-- 2 files changed, 12 insertions(+), 37 deletions(-) diff --git a/src/bootstraparse/modules/error_mngr.py b/src/bootstraparse/modules/error_mngr.py index 837b99b..c460ee4 100644 --- a/src/bootstraparse/modules/error_mngr.py +++ b/src/bootstraparse/modules/error_mngr.py @@ -86,41 +86,6 @@ def log_exception(exception, level="ERROR"): return -def dict_check(dic, *args): - """ - Helper function to test existence of a list of succesive keys within a nested dictionary. - :param dic: The dictionary of dictionaries to check - :param args: Any number of keys to check the dictionaries for - :type dic: dict | config.ConfigLoader - :type args: str - :return: A table of Booleans for every key checked. - """ - output = [False for _ in args] - for i in range(len(args)): - if args[i] in dic: - output[i] = True - dic = dic[args[i]] - else: - break - return output - - -def str_dict_check(dic, *args): - """ - Helper function to test existence within a dictionary and return a string of the values of the keys checked - and wether they exist. - 'arg[0]' : True or False - :param dic: The nested dictionary to check - :param args: Any number of keys to check successively - :type dic: dict - :type args: str - :return: A string of the values of the keys checked and wether they exist. - :rtype: str - """ - out = dict_check(dic, *args) - return "\n".join([f"{args[i]}: {out[i]}" for i in range(len(out))]) - - class BootstraparseError(Exception): """ Base class for all Bootstraparse errors diff --git a/src/bootstraparse/modules/tools.py b/src/bootstraparse/modules/tools.py index 4dffc12..e6b68b5 100644 --- a/src/bootstraparse/modules/tools.py +++ b/src/bootstraparse/modules/tools.py @@ -15,11 +15,14 @@ import re ############################################################################### # Dictionary checks ######################################## + def dict_check(dic, *args): """ - Helper function to test existence within a dictionary. + Helper function to test existence of a list of succesive keys within a nested dictionary. :param dic: The dictionary of dictionaries to check :param args: Any number of keys to check the dictionaries for + :type dic: dict | config.ConfigLoader + :type args: str :return: A table of Booleans for every key checked. """ output = [False for _ in args] @@ -34,8 +37,15 @@ def dict_check(dic, *args): def str_dict_check(dic, *args): """ - Helper function to test existence within a dictionary and return a string of the values. + Helper function to test existence within a dictionary and return a string of the values of the keys checked + and wether they exist. 'arg[0]' : True or False + :param dic: The nested dictionary to check + :param args: Any number of keys to check successively + :type dic: dict + :type args: str + :return: A string of the values of the keys checked and wether they exist. + :rtype: str """ out = dict_check(dic, *args) return "\n".join([f"{args[i]}: {out[i]}" for i in range(len(out))])