From 30b92195bd4fd1befd1628bb0628211da27e9513 Mon Sep 17 00:00:00 2001 From: freezed <2160318-free_zed@users.noreply.gitlab.com> Date: Wed, 21 Apr 2021 23:46:31 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8A=20Add=20a=20`--debug`=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit relate to forga/process/fr/embarquement#6 --- tuto-pysdur/pgcd.py | 46 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/tuto-pysdur/pgcd.py b/tuto-pysdur/pgcd.py index 1a8556a..7f19f81 100755 --- a/tuto-pysdur/pgcd.py +++ b/tuto-pysdur/pgcd.py @@ -17,9 +17,30 @@ import sys # MESSAGES FUNCTION_CALL = "Function `%s` is called" -SUBSTRACTION_MADE = "Substraction made: %s - %s" +RESULT = "Result : `%s`" +SUBSTRACTION_MADE = "Substraction made : %s - %s" WRONG_INPUT_ORDER = "Revert input order (`%s < %s`)" +LOGGER = logging.getLogger(__file__) + + +def logging_setup(args): + """Set logging up""" + + loglevel = logging.WARNING + if args.debug: + loglevel = logging.DEBUG + + LOGGER.setLevel(loglevel) + + console_hdlr = logging.StreamHandler() + console_hdlr.setFormatter( + logging.Formatter("[%(name)s] %(levelname)s\t%(message)s") + ) + + root = logging.getLogger("") + root.addHandler(console_hdlr) + def pgcd(input_a, input_b): """ @@ -36,14 +57,14 @@ def pgcd(input_a, input_b): >>> pgcd(910, 42) 14 """ - logging.info(FUNCTION_CALL, "pgcd") + LOGGER.info(FUNCTION_CALL, "pgcd()") rest = input_a - input_b check = input_b - rest while check != 0: input_a = max(input_b, rest) input_b = min(input_b, rest) - logging.debug(SUBSTRACTION_MADE, input_a, input_b) + LOGGER.debug(SUBSTRACTION_MADE, input_a, input_b) rest = input_a - input_b check = max(input_b, rest) - min(input_b, rest) @@ -69,21 +90,32 @@ if __name__ == "__main__": PARSER.add_argument( "-v", "--verbose", help="A near mathematics answer", action="store_true" ) - + PARSER.add_argument( + "-d", + "--debug", + default=False, + help="Set logging level to DEBUG", + action="store_true", + ) ARGS = PARSER.parse_args() + logging_setup(ARGS) + # CHECKS INPUTS if ARGS.INPUT_A <= ARGS.INPUT_B: - logging.critical(WRONG_INPUT_ORDER, ARGS.INPUT_A, ARGS.INPUT_B) + LOGGER.critical(WRONG_INPUT_ORDER, ARGS.INPUT_A, ARGS.INPUT_B) sys.exit() # DO THE JOB else: NEW_PGCD = pgcd(ARGS.INPUT_A, ARGS.INPUT_B) - # RESPONSE + # POST COMPUTE if ARGS.verbose: - print(f"PGCD({ARGS.INPUT_A};{ARGS.INPUT_B}) = {NEW_PGCD}") + NEW_PGCD = f"PGCD({ARGS.INPUT_A};{ARGS.INPUT_B}) = {NEW_PGCD}" + + if ARGS.debug: + LOGGER.debug(RESULT, NEW_PGCD) else: print(NEW_PGCD)