Sonntag, 27. April 2014

Python doctest

Most of my exercises for SCIP come as a stub function where you have to implement the logic. To provide a way to self test your implementation, the stub functions contain also something called doctests.

doctests are a nice way to quickly add tests to your functions. This is what the Python manual says: The doctest module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown.

Here is an example of one of my exercises:
from operator import add, sub
def a_plus_abs_b(a, b):
    """Return a+abs(b), but without calling abs.

    >>> a_plus_abs_b(2, 3)
    >>> a_plus_abs_b(2, -3)
    if b < 0:
        op = sub
        op = add
    return op(a, b)
To run the two tests in the comment against the implementation I'm using the following command:
python3 -m doctest -v

First Status - Studying "Structure And Interpretation Of Computer Programs"

Five weeks ago I announced my plans to self-study the online material of "Structure And Interpretation Of Computer Programs", provided by UC Berkely.

This is a quick status. I made a printout of the course schedule and pinned it on my wall:

As you can see I am slowly progressing, slower than I did expect. The reason is that due to my private duties I can't commit to much time to my studies. Also, the course project on higher order functions (the "hog" project) is supposed to be worked on by a group of students - I had to do it on my own.

Working on an exercise, it is also sometimes tricky to understand the problem itself. This is mainly due to some gaps in my English math vocabulary (English is not my mother's tongue).

However, so far I had a very good and enlightening time with higher order functions and lambda expressions. Currently, I am working my way through the various exercises ( lab, discussion, homework...) for recursion. This is great since I never had the chance to train (yes, it is possible) creating recursive functions.

If you're interested, I'm collecting my study material and results on github.