Logging debug.
This commit is contained in:
parent
697ae95047
commit
32b7b2c14b
9
fib.py
9
fib.py
|
@ -1,18 +1,26 @@
|
||||||
"""A demo module implementing fib."""
|
"""A demo module implementing fib."""
|
||||||
import argparse
|
import argparse
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
def parse_args():
|
def parse_args():
|
||||||
"""Parse command line arguments."""
|
"""Parse command line arguments."""
|
||||||
parser = argparse.ArgumentParser(description="Compute fibonacci sequence numbers.")
|
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("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()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def fib(n: int) -> int:
|
def fib(n: int) -> int:
|
||||||
"""Return the *n*th element in the Fibonacci sequence."""
|
"""Return the *n*th element in the Fibonacci sequence."""
|
||||||
a, b = 1, 1
|
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):
|
for _ in range(n):
|
||||||
|
logger.debug(f"In fib, loop {_} with: {a}, {b}")
|
||||||
a, b = b, a + b
|
a, b = b, a + b
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
@ -20,6 +28,7 @@ def fib(n: int) -> int:
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
"""The module entry point."""
|
"""The module entry point."""
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
logging.basicConfig(level=[logging.ERROR, logging.INFO, logging.DEBUG][args.verbose])
|
||||||
print(fib(args.n))
|
print(fib(args.n))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue