Key Word(s): Documentation, Testing, Coverage
Exercise: Documentation and Testing¶
The following little program needs some documentation and some tests. Since you didn't write it, I'll tell you what it's supposed to do. You'll need to document it. Feel free to test for additional exceptions if you have time but start with it as it is.
The point of the program is to compute the $L_{2}$ norm of a vector $v$. A second argument, if provided, will be interpreted as a vector of weights. The second argument must have the same length as the input vector.
NOTE: The input type of the vectors for this program should be a list of numbers.
As a reminder, the weighted $L_2$ norm of a vector $v$ is given by \begin{align*} \|v\|_{W} = \sqrt{\sum_{i=1}^{N}{\left(w_{i}v_{i}\right)^2}} \end{align*} where $N$ is the length of the vector $v$, $v_{i}$ is the i-th component of the vector $v$ and $w_{i}$ is the i-th component of the weight vector.
Requirements¶
- You must write the documentation and a decent test suite. Try to include some doctests as well!
- Use the
pytest
module to run the doctests and unit tests and to assess the code coverage.
If you don't already have pytest
, you can install it using pip install pytest
. If you have trouble installing, here's the website: pytest
installation.
Solution¶
The tests can be found in the file test_L2.py
and the function can be found in L2.py
.
Run pytest --doctest-modules --cov --cov-report term-missing
from inside the directory to run the test suite. Please don't use the Jupyter notebook this time.
Interpreter¶
Now we'll use the Jupyter notebook as a Python
interpreter.
# Import our module
import L2
# Print the documentation
import pydoc
pydoc.doc(L2)
# Run the doctests
import doctest
doctest.testmod(L2, verbose=True)