Friday, August 16, 2013

doctest and then run sphinx

When you need to run unit-test and make documentation for it with Python. doctest and sphinx is the one of the best choices you can have.
"""
This is the "example" module.

The example module supplies one function, factorial().  For example,

>>> factorial(5)
120

"""

def factorial(n):
    """Return the factorial of n, an exact integer >= 0.

    If the result is small enough to fit in an int, return an int.
    Else return a long.

    >>> [factorial(n) for n in range(6)]
    [1, 1, 2, 6, 24, 120]
    """
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
        
if __name__ == "__main__":
    import doctest
    doctest.testmod()
Then execute `sphinx-quickstart`, modify `conf.py` to make Python importable the source code directory. Finally, run make html.
Screen Shot 2013 08 16 at 4 45 11 PM