eroicaleo.github.io

Lecture 07 DEBUGGING

black box testing: exercise path through specification. glass box testing: exercise path through code.

TESTING and DEBUGGING

Design your code for ease testing and debugging

When are ready to test?

TEST SUITE

testing

test suite

An example

def isBigger(x, y):
  '''
  Assume x, y are integers
  '''

possible partition:

Partition

What if we don’t have natural partition?

BLACK-BOX TESTING

test suite is designed without actually looking at the code.

Paths through a specification

Also good to consider boundary conditions:

GLASS BOX TESTING

Rules of thumb for glass-box testing

TEST DRIVERS AND STUBS

Conducting test

test drivers and stubs

Driver are code that:

Drivers simulate parts of program that use unit being tested Stubs simulate parts of program used by unit being tested

Allow you to test units that depend on software not yet written

Good testing practice

DEBUGGING

Runtime bugs

Categories of bugs

Debugging skills

Treat as a search problem: looking for explanation for incorrect behaviour.

Some pragmatic hints

Lecture 08 Assertion and Exceptions

EXCEPTIONS

What is an exception?

What if the code hits an unexpected condition? These are called exceptions.

Test = [1, 2, 3]
Test[4]
int(Test)
'a' / 4
noThisName += 1

What to do with exceptions?

Dealing with exceptions

try:
  f = open('grade.txt')
  # Code to process
except:
  raise Exception('Cannot find file!')

Handling specific exceptions

Usually handler is only meant to deal with a particular type of exception.

try:
  f = open('grade.txt')
  # Code to process
except IOError, e:
  raise Exception('Cannot find file!')
  sys.exit(0)
except ArithmeticError, e:
  raise ValueError('Bug in grade calculation ' + str(e))

Types of exceptions

Other extensions to try

try:
else:
except:

EXCEPTION AS CONTROL FLOW

Exception as flow of control

ASSERTIONS

Assertions

def avg(grades, weights):
  assert not len(grades) == 0, 'not grades data'
  newgr = [convertLetter(elt) for elt in grades]
  return dotProduct(newgr, weights) / len(newgr)

Assertions as defensive programming

Where to use assertions?

Lecture 11 Classes

Advantages of OOP

A CLASS EXAMPLE

Creating an instance

class Coordinate(object):
  def __init__(self, x, y):
    self.x = x
    self.y = y

AN ENVIRONMENT VIEW OF CLASS

This section is interesting, needs more notes.

ADDING METHODS TO A CLASS

isinstance(c, Coordinate)
type(c)

Lecture 12 Object Oriented Programming

INHERITANCE

python list.sort() will use the lt method in the class

USING INHERITANCE SUBCLASSES TO EXTEND BEHAVIOR

class MITPerson(Person):
  # Bind to the class, not instance
  # Like the static variable in C++/JAVA
  nextIdNum = 0

  def __init__(self, name):
    Person.__init__(self, name)
    self.idNum = MITPerson.nextIdNum
    MITPerson.nextIdNum += 1

  def __lt__(self, other):
    return self.idNum < other.idNum

Note here the MITPerson ‘s __init__ and __lt__ will shadow the same method defined in Person class.

# One consequence is like this:
p1 = MITPerson('Eric')
p2 = Person('John')
# This is OK
p2 < p1
# This is not
p1 < p2

USING INHERITANCE: DESIGNING A CLASS HIERARCHY

EXAMPLE: A GRADEBOOK

GENERATORS

Any procedure with yield statement is a generator. generator has next() methods. When next() is called, it will execute until it hits yield, which suspend execution and returns a value. Until it raises a StopIteration exception