Fixed weird code duplication

This commit is contained in:
Maximilien Rigaut 2022-06-12 14:13:21 +02:00
parent 611617a8fd
commit 36e56afa4c
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
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

View File

@ -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))])