From 32b7b2c14b4756c2e2b536e89cecd4b8280fe5dc Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Wed, 8 Nov 2023 10:33:30 +0100 Subject: [PATCH] Logging debug. --- fib.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fib.py b/fib.py index 001286a..5c27f3e 100644 --- a/fib.py +++ b/fib.py @@ -1,18 +1,26 @@ """A demo module implementing fib.""" import argparse +import logging + +logger = logging.getLogger() def parse_args(): """Parse command line arguments.""" parser = argparse.ArgumentParser(description="Compute fibonacci sequence numbers.") parser.add_argument("n", type=int, help="To compute the nth element of fib.") + parser.add_argument("-v", "--verbose", action="count", help="Be verbose", default=0) return parser.parse_args() def fib(n: int) -> int: """Return the *n*th element in the Fibonacci sequence.""" a, b = 1, 1 + if n < 0: + logger.error("It'll go wrong with a negative number.") + logger.info(f"In fib, starting with: {a}, {b}") for _ in range(n): + logger.debug(f"In fib, loop {_} with: {a}, {b}") a, b = b, a + b return a @@ -20,6 +28,7 @@ def fib(n: int) -> int: def main() -> None: """The module entry point.""" args = parse_args() + logging.basicConfig(level=[logging.ERROR, logging.INFO, logging.DEBUG][args.verbose]) print(fib(args.n))