Software Carpentry Day 2: Debugger Exercises

Debugger (Morning)

For all of the following exercises, you should avoid using cut-and-paste and avoid adding print statements.

  1. Using the Wing IDE, type in the following program verbatim.
    import sys
    
    # From http://en.literateprograms.org/Fibonacci_numbers_(Python)
    def fib(n):
        if n == 0:
            return 0
        elif n == 1:
            return 1
        else:
            return fib(n-1) + fib(n-2)
    
    for i in range(len(sys.argv)):
        arg = sys.argv[i];
        num = int(arg)
        print "fib of", num, " = ", fib(num)
    

    Note that program, as written, has a small, subtle bug when you run it, such as:

    % python bug.py 1 2 3 4
    Traceback (most recent call last):
      File "bug.py", line 14, in 
        num = int(arg)
    ValueError: invalid literal for int() with base 10: 'bug.py'
    

    Use the Wing IDE debugger to find and fix the bug. Remember, do not add any print statements.


  2. Using the Wing IDE, type in the following program verbatim.
    import sys
    
    # Modified From Software Carpentry notes
    def quick_sort(list):
        ''' Sort list in non-decreasing order, using Quick Sort. '''
        # If list contains at most 1 element, it is already sorted.
        if len(list) <= 1:
            return list
        # Select a pivot, then partition the list.
        pivot = list[0]
        smaller = [x for x in list if x < pivot]
        equal   = [x for x in list if x == pivot]
        greater = [x for x in list if x > pivot]
        if len(greater) > 1:
            print # Set breakpoint here.  Inspect 'pivot' and list 'greater'
        # Recurse and copy the results back into list.
        quick_sort(smaller)
        quick_sort(greater)
        list[:] = smaller + equal + greater
        return list
    
    list = sys.argv[:]
    print "Before:  ", list
    sorted = quick_sort(list)
    print "After:   ", sorted
    

    Note that the program does NOT have any (known) bugs. You can run the program as follows:

    % python debug.sort.py 2 3 1 4 alpha beta 10
    Before:   ['debug.sort.py', '2', '3', '1', '4', 'alpha', 'beta', '10']
    
    
    
    After:    ['1', '10', '2', '3', '4', 'alpha', 'beta', 'debug.sort.py']
    

    The extra blank lines are due to the (empty) print after the if len(greater) > 1:. The if statement and the print are there to make it easier to use breakpoints, etc. with the debugger.

    Use the Wing IDE debugger to determine the value of variables pivot and list greater at that point in the function, for each of the following invocations/runs.

    % python debug.sort.py 2 3 1 4 alpha beta 10
    % python debug.sort.py the quick brown fox jumped over the lazy dog
    % python debug.sort.py python can be fun but my number one language is still C
    

    Remember, do not add any print statements.

Version Control (Afternoon)

  1. Setup your own project in the repository
    1. Create your own directory on your local filesystem with a README.txt file
    2. Import that into your subversion repository (suggestion of location is yourname/trunk)
    3. Make sure you have added the README.txt file to the repository and commit the change.
    4. Make some changes to README.txt, use SmartSVN to review your changes before doing a commit to the repository
    5. Add some new files to your project (Python source code, images, directories) and add them to the project, commit to repository
  2. Share your project
    1. Work with your neighbour so you can check out a new project with SmartSVN by checking out THEIR project
    2. Make some changes to a file in their project and commit.
    3. Delete a file from the project and commit.
  3. What did your partner do?
    1. Now load your project back up.
    2. BEFORE you hit the update button, let's check what the status is
    3. Check the changes between your local state and state of the repository
    4. Make note of the file that was deleted
    5. Update your local version to the HEAD
    6. Undo the deleted file, various ways of doing this