🔊 Add a `--debug` option

relate to forga/process/fr/embarquement#6
This commit is contained in:
freezed 2021-04-21 23:46:31 +02:00
parent bd949c2075
commit 30b92195bd
1 changed files with 39 additions and 7 deletions

View File

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