Upgrade to Pro — share decks privately, control downloads, hide ads and more …

2017 - Albert Sweigart - Logging and Testing

PyBay
August 13, 2017

2017 - Albert Sweigart - Logging and Testing

Description
Logging messages, unit tests, and using the debugger are best practices, but often left out of introductory programming tutorials. As a result, beginners come away with the idea that these time-saving features are somehow "advanced" or things to be learned later on. In this presentation, Al Sweigart dispels these notions by providing quickstart guides to Python's logging, doctest, and pdb modules.

Abstract
Logging messages, unit tests, and using the debugger are best practices, but often left out of introductory programming tutorials. As a result, beginners come away with the idea that these time-saving features are somehow "advanced" or things to be learned later on. In this presentation, Al Sweigart dispels these notions by providing quickstart guides to Python's logging, doctest, and pdb modules, including:

Why beginners (incorrectly) avoid these programming best practices.
The "if you're doing this" signs that you aren't using these best practices.
The 4 lines of code you need from the logging module.
The 4 lines of code you need from the doctest module.
The 2 lines of code you need from the pdf module.
How much time you'll save, not spend, by using these not-so-advanced modules.

Bio
Al Sweigart is the author of several programming books, including Automate the Boring Stuff with Python. He releases all of his books online for free under a Creative Commons license. He lives in San Francisco, and his cat weighs 12 pounds.

Video: https://youtu.be/TDx_Mv3MUw0

PyBay

August 13, 2017
Tweet

More Decks by PyBay

Other Decks in Programming

Transcript

  1. Why do logging instead of print? • Debug output vs.

    normal output • Easier to remove debug messages. • Easier to put debug messages back. • Fine grain control with logging levels.
  2. Logging Levels • DEBUG • INFO • WARN • ERROR

    • CRITICAL • logging.basicConfig( level=logging.DEBUG) • logging.disable(logging.CRITICAL)
  3. DEBUG • Low-level debugging information. • Variables values, etc. •

    You only look at when something has gone wrong. • Probably going to ignore this.
  4. Logging Levels • logging.debug('message') • logging.info('message') • logging.warn('message') • logging.error('message')

    • logging.critical('message') • logging.basicConfig( level=logging.DEBUG) • logging.disable(logging.CRITICAL)
  5. Following log files in real time tail –f <filename> (On

    Windows, install Cygwin.) Back to the live demo…
  6. Stepping s (Step Into) n (Next / step over) r

    (Return / step out) c (continue running until next breakpoint)
  7. Breakpoints • b (list all breakpoints & their numbers) •

    b <line number> • b <function name> • cl <breakpoint number> (clear bp) • import pdb; pdb.set_trace()
  8. • Doctests are not a replacement for unit tests. •

    Doctests are unit tests for documentation. • Back to the live demo…