Fixed weird code duplication

This commit is contained in:
Maximilien Rigaut 2022-06-12 14:13:21 +02:00
parent 62666e0ab4
commit c8082c44a3
No known key found for this signature in database
GPG Key ID: 4F14B23709ADA5B1
2 changed files with 12 additions and 37 deletions

View File

@ -86,41 +86,6 @@ def log_exception(exception, level="ERROR"):
return 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): class BootstraparseError(Exception):
""" """
Base class for all Bootstraparse errors Base class for all Bootstraparse errors

View File

@ -15,11 +15,14 @@ import re
############################################################################### ###############################################################################
# Dictionary checks # Dictionary checks
######################################## ########################################
def dict_check(dic, *args): 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 dic: The dictionary of dictionaries to check
:param args: Any number of keys to check the dictionaries for :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. :return: A table of Booleans for every key checked.
""" """
output = [False for _ in args] output = [False for _ in args]
@ -34,8 +37,15 @@ def dict_check(dic, *args):
def str_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 '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) out = dict_check(dic, *args)
return "\n".join([f"{args[i]}: {out[i]}" for i in range(len(out))]) return "\n".join([f"{args[i]}: {out[i]}" for i in range(len(out))])