Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

Time for action – executing doctests


Let's write a simple example that is supposed to calculate the well-known factorial, but doesn't cover all of the possible boundary conditions. In other words, some tests will fail.

  1. The docstring will look like text you would see in a Python shell (including a prompt). Rig one of the tests to fail, just to see what will happen:

    """
    Test for the factorial of 3 that should pass.
    >>> factorial(3)
    6
    Test for the factorial of 0 that should fail.
    >>> factorial(0)
    1
    """
    
  2. Write the following line of NumPy code:

    return np.arange(1, n+1).cumprod()[-1]

    We want this code to fail from time to time for demonstration purposes.

  3. Run the doctest by calling the rundocs() function of the numpy.testing module, for instance, in the Python shell:

    >>> from numpy.testing import rundocs
    >>> rundocs('docstringtest.py')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "…/numpy/testing/utils.py", line 998,...