Skip to main content

This page contains a list of functions or tools we found to be helpful when writing or debugging code.

print

A pretty simple way to quickly debug your Python functions is to use the print function to verify that variable values are what you expect it to be. Since Python is dynamically typed, you may find it particularly helpful to place print statements at the start and end of your functions to explicitly check the inputs and outputs. We recommend you use the string format function when you print debugging messages in order to make things clearer for yourself and any TAs helping you debug your code. For example, if you're working on the SLAM project and need to check on the Kalman Gain, you should try using:

print 'Kalman Gain: {}'.format(kalmanGain)

assert

Assert statements help ensure that your program's state is valid. In Python (and other dynamically typed languages), assert statements can also check the types of certain arguments to ensure they're valid. You may optionally attach a message with your assert statement. Here's two examples:

# name2id_dict and id2name_dict must be inverses
id
= self.name2id_dict[name]
assert self.id2name_dict[id] == name
class MyDB:
    ...
    def add(self, id, name):
      assert type(id) is IntType, "id is not an integer: {}".format(id)
      assert type(name) is StringType, "name is not a string: {}".format(name)

ipdb

If you want a more robust way to debug your Python code, it's well worth it to learn how to use ipdb. ipdb allows you to set breakpoints in your code, view the state of your program, manipulate variables, and much more. To get started with ipdb, run the command "easy_install ipdb" and check out this short guide. The TAs highly recommend using ipdb.

Division

In many of your assignments, you will be working with probabilities and other values that should be represented as floats. Standard division in Python will take the floor of the quotient result when the arguments are not of the float type. You can see an example of this in the following code snippet:
>>> a = 3
>>> b = 2
>>> div = a / b
>>> print div
1
>>>

One way around this problem is to explicitly cast division arguments as floats, however this becomes rather tedious and makes the code slightly more difficult to read. Instead, you should import the division operator from the __future__ module which will overwrite the standard division operator in the entire Python file and always return the proper division result. The division import must be the FIRST import statement in the file.
>>> from __future__ import division
>>> a = 3
>>> b = 2
>>> div = a / b
>>> print div
1.5
>>>

Log probability smoothing

Since the logarithm of 0 is undefined, Python will throw a ValueError any time you attempt to take the log of a variable with a value is 0. For example, when dealing with classification probabilities, it is possible to have a probability value of 0 which will cause your program to throw an error when you attempt to find the log probability. To avoid this, we suggest you add a small smoothing constant to values you take the logarithm of, if that value can be 0. You may find it easiest to define a global smoothing constant and then simply add it where appropriate. Make sure that you only smooth the appropriate variables whose value can be 0 and the logarithm function must be applied; smoothing all uses of the log function may mask errors in your code (e.g. variables with values that should always be non-zero).

NumPy

NumPy ("Numerical Python") is a powerful library that handles linear algebra and large calculations more efficiently than using Python's standard operations. Much of the course's support code uses NumPy, and you may find it useful for the last three projects in the course. You can introduce yourself to NumPy with this handy guide and learn more on the official NumPy site.