Computer Science 15-112, Fall 2011
Class Notes:  Debugging


  1. Required Reading
  2. Stack Traces -- A Sea of Information!
  3. Debug with print Statements
  4. Actually, use db() instead of print
  5. Use Assertions and Invariants
  6. Disable Events + Simulate Events
  7. Profile with time.time() and cProfile.run("myTest()")
  8. Use the pdb Python debugger

  1. Required Reading
    1. Python Standard Library, Ch 26.2 - 26.3 (pdb: The Python Debugger + Debugger Commands)
       
  2. Stack Traces -- A Sea of Information!
     
  3. Debug with print Statements
    # sample buggy code
    def digitFactors(n):
        # returns the number of digits of n that are factors of n
        count = 0
        while (n > 0):
            digit = n % 10
            n /= 10
            if (n % digit == 0):
                # found another digit that divides n
                count += 1
        return count
    
    # 6367 is prime, so digitFactors(6367) should equal 0
    print digitFactors(6367) # prints 2 (uh oh)
  4. Actually, use db() instead of print
    1. Sample Code
      def debug():
          return __debug__  # set to False before you submit your hw!
      
      def db(*args):
          if debug():
              for arg in args:
                  print arg,
              print
      
      x = 0
      for n in xrange(5):
          x += n
          db("n=",n,", x=",x)
      print x
    2. debug() vs __debug__
       
  5. Use Assertions and Invariants
    See "Correctness" notes
     
  6. Disable Events + Simulate Events
    (This applies once we cover graphics and events, later in the course)
     
  7. Profile with time.time() and cProfile.run("myTest()")
     
  8. Use the pdb Python debugger

carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem