Skip to Content
Modern Python Standard Library Cookbook
book

Modern Python Standard Library Cookbook

by Alessandro Molina
August 2018
Intermediate to advanced
366 pages
10h 14m
English
Packt Publishing
Content preview from Modern Python Standard Library Cookbook

How to do it...

To test logging to a file, we are going to create a short tool that computes up to the nth Fibonacci number based on the current time. If it's 3:01 P.M., we want to compute only 1 number, while if it's 3:59 P.M., we want to compute 59 numbers.

The software will provide the computed numbers as the output, but we also want to log up to which number it computed and when it was run:

import logging, sys if __name__ == '__main__': if len(sys.argv) < 2: print('Please provide logging file name as argument') sys.exit(1) logging_file = sys.argv[1] logging.basicConfig(level=logging.INFO, filename=logging_file, format='%(asctime)s %(name)s %(levelname)s: %(message)s') log = logging.getLogger(__name__) def fibo(num): log.info('Computing ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Advanced Python Development: Using Powerful Language Features in Real-World Applications

Matthew Wilkes

Publisher Resources

ISBN: 9781788830829Supplemental Content