From 9739566e1b2a5bbdd704b3aba4f8fd89a0eaf113 Mon Sep 17 00:00:00 2001 From: klemek Date: Mon, 27 Apr 2020 00:36:16 +0200 Subject: [PATCH] utils: docstrings and unit tests --- discord_bot/__main__.py | 2 +- meme_otron/utils.py | 33 ++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/discord_bot/__main__.py b/discord_bot/__main__.py index 96f1a7a..f777586 100644 --- a/discord_bot/__main__.py +++ b/discord_bot/__main__.py @@ -62,7 +62,7 @@ async def on_ready(): async def delete(message): """ - TODO + Delete a discord message :param (discord.Message) message: :rtype: bool diff --git a/meme_otron/utils.py b/meme_otron/utils.py index 66340c4..bdddab4 100644 --- a/meme_otron/utils.py +++ b/meme_otron/utils.py @@ -5,7 +5,7 @@ from Levenshtein import distance def relative_path(file, *args): """ - TODO + Get the full path from a starting file and a relative path :param (str) file: :param (str) args: @@ -17,7 +17,8 @@ def relative_path(file, *args): def read_key_safe(d, k, default=None, *, types=None, is_list=False, is_list_size=None): """ - TODO + Read a value from a dict or return the default value if not found. + Can also check the type of the value. :param (dict) d: source dict :param (str) k: key to read @@ -36,7 +37,8 @@ def read_key_safe(d, k, default=None, *, types=None, is_list=False, is_list_size def read_key(d, k, default=None, *, types=None, is_list=False, is_list_size=None): """ - TODO + Read a value from a dict or return the default value or throw an error if the default is None. + Can also check the type of the value. :param (dict) d: source dict :param (str) k: key to read @@ -64,7 +66,8 @@ def read_key(d, k, default=None, *, types=None, is_list=False, is_list_size=None def check_type(obj, types, is_list=False, is_list_size=None): """ - TODO + Check the type from a list of possibilities. + Can check the types of all elements of a list. :param obj: :param (list of type|None) types: types to check @@ -75,7 +78,7 @@ def check_type(obj, types, is_list=False, is_list_size=None): """ if is_list: if not is_list_of(obj, types, is_list_size): - if is_list_size is None: + if is_list_size is not None: raise TypeError(f"not a list of {is_list_size} {types[0].__name__}") else: raise TypeError(f"not a list of {types[0].__name__}") @@ -86,7 +89,7 @@ def check_type(obj, types, is_list=False, is_list_size=None): def is_list_of(obj, types, length=None): """ - TODO + Check the types of all elements of a list. :param obj: :param (list of type) types: @@ -114,25 +117,25 @@ args_regex = re.compile('"([^"]*)"|\'([^\']*)\'|([^ ]+)') def parse_arguments(s): """ - TODO + Split a string into separates arguments :param (str) s: :rtype: list of str :return: """ + + def get_found_match(m): + f = [g for g in m if len(g) > 0] + if len(f) > 0: + return f[0] + return "" + return [get_found_match(m) for m in args_regex.findall(s)] -def get_found_match(m): - f = [g for g in m if len(g) > 0] - if len(f) > 0: - return f[0] - return "" - - def find_nearest(word, wlist, threshold=5): """ - TODO + Find the nearest word in a list :param (str) word: :param (list of str) wlist: