Monitoring Scripts for Errors


Debugging and Fixing Errors

  • 3 categories:
    • Syntax errors
      • occur when the Python parser is unable to understand a line of code
      • usually the result of typos or misunderstandings about the Python language syntax
      • e.g., forgetting parenthesis
    • Runtime errors
      • occur during the execution of a program
      • often caused by
        • operations that are mathematically illegal
          • e.g., division by 0
        • or attempting to access a resource that isn’t available
    • Semantic errors
      • program runs without crashing, but doesn’t produce the expected output
      • could be due to error in program’s logic

Debugging is the process of identifying and correcting these bugs.

  • various tools help with debugging:
    • debuggers
    • linters
    • integrated development environments

Python Debugging

  • basic steps:
    • Understanding the Problem
      • involves reproducing the error and analyzing the error message or incorrect output
    • Isolating the Problem
      • isolate the section of code causing the error
      • done by commenting out sections of code or using print statements to check the values of variables at different stages of the program
    • Using a Debugger
      • Python comes with a built-in debugger called pdb
        • allows stepping through the code line by line, inspecting variables, and setting breakpoints at specific lines of code
    • Fixing the Error
      • could involve correcting a typo, changing a variable, or rewriting a section of code
    • Testing the Solution
      • test the solution under different scenarios to ensure the error has been completely resolved

Common Debugging Techniques

  • Print statements
    • use print statements to display the values of variables at certain points in the program
    • can help identify unexpected values or behavior
  • Using a Debugger
    • Python’s built-in debugger, pdb, allows stepping through the code line by line, inspecting variables, and setting breakpoints
    • powerful tool for understanding the flow of the program and identifying where things go wrong
  • Code Review
    • can be done individually or as part of a pair programming or code review session
  • Unit Testing
    • can help catch errors and prevent regressions
    • Python’s unittest module provides a framework for creating and running tests
  • Logging
    • Python’s logging module can provide valuable insights into the behavior of the program over time
  • Profilling
    • For performance issues, Python’s cProfile module can help identify bottlenecks in the code

IDE Code Errors

  • are issues that arise when writing code in an IDE
    • classified as:
      • syntax errors
      • runtime errors
      • semantic errors
  • IDEs can flag linting errors
    • not a bug per se, but violation of code conventions or best practices
    • resolving makes code more readable and maintainable
  • each IDE may have different ways of indicating these errors

Debugging Tools

  • common debugging tools in Python:
    • PDB
      • built-in Python debugger
    • PyCharm Debugger
      • PyCharm, a popular Python IDE, comes with a powerful debugger
    • Visual Studio Code Debugger
      • popular IDE that includes a versatile debugger with support for remote debugging, multi-threaded debugging, and conditional breakpoints
    • Logging
      • built-in logging module can be used to record the flow of the program and help identify issues
    • Unit Testing Tools
      • unittest, pytest, and doctest can help catch errors and prevent regressions
    • Linters
      • pylint and flake8 can catch potential issues in the code that might lead to errors
    • Profiling Tools
      • cProfile and memory_profiler can help identify performance bottlenecks
  • The Best Tools to Debug Python

Input Validation

try/except Blocks

try/except blocks are used for exception handling.

  • allow the program to continue running even if an error or exception occurs
try:
    # Code that might raise an exception
except ExceptionType:
    # Code to handle the exception
  • The try block contains the code that might raise an exception
    • Python will attempt to execute this code
  • If an exception is raised in the try block, the execution immediately moves to the except block
    • The ExceptionType is the type of exception that the except block can handle
    • If the exception type matches ExceptionType, then the code within the except block is executed
  • If no exception is raised in the try block, the except block is skipped