In-class exercise#

The in-class exercise is distributed via a GitHub Classroom repository. To get access to your group’s git repository, you can follow this link.

The first person will create a group and set up the repository for the group; the others will land on the webpage of your group’s repository immediately. All group members can then clone the repository.

Introduction#

Today you will add error handling and testing to functions you have written in previous in-class exercises and assignments.

If your group does not have a good solution to these functions, you can ask other groups to send you their solution.

Task 1#

Work in prime_number.py.

Think about error handling for the function is_prime.

Which kinds of invalid inputs would you expect in a typical scenario?

Add error handling to the function.

Task 2#

Work in test_prime_number.py.

Write several tests for the is_prime function that you import from prime_number.

Are there any special cases you should include in your tests?

Remember the naming conventions for tests.

Run all the tests from your shell.

Task 3#

Discuss in your group why the is_prime function is a very good candidate for unit tests. Be prepared to discuss this in the plenary session later.

Task 4 (Bonus Question)#

Work in test_prime_number.py

Use pytest.mark.parametrize to run the same test with multiple inputs. For example it would be a good idea to have one parametrized test where all inputs are prime numbers and one where all inputs are not prime.

To work on this you need to have watched this screencast

Task 5#

Work in production_functions.py

Copy over the production functions from your solution to Assignment 1.

Task 6#

Think about error handling for the production functions. What kinds of invalid inputs would you expect in a typical scenario? Look specifically for economic restrictions on factors and parameters.

Add error handling for the cobb_douglas, leontief, ces and general_ces functions. Do the checks in _fail functions and try to re-use as many checks as possible across the different functions.

Try to follow the structure in the worked error handling example.

Define at least one custom exception. Typically, the custom exceptions should not be specific to one function, such that you can re-use them. A good candidate for custom exceptions is the economic restriction that factors must be non-negative.

Task 7#

Write tests for all production functions. You can re-use the inputs you used in assignment 1.

Since floating point numbers are not exact, you can not expect to get fully precise results as soon as a floating point number was involved anywhere inside your function (even if the mathematical reult is a round integer value). Therefore, there are several tools you can use for comparisons and assertions that involve floating point numbers. For now, you only need to know np.allclose.

To check whether two numbers or two arrays are close, you can use

assert np.allclose(1, 1.00001)

You can influence the tolerance using optional arguments:

assert np.allclose(1, 1.00001, rtol=1e-8, atol=1e-8)

This will raise an error.

A function that achieves the same thing but can give you a better error message in case of failures is numpy.testig.assert_array_almost_equal

Task 8 (Bonus Question)#

Write test for the error handling in your is_prime and production functions.

For this you need to have watched this screencast