Python Exercises lab 9

9.1 Prerequisites and learning outcomes

Before you start you should have completed lab 8 and all previous labs. The exercises in this lab assume you to have a working knowledge of the Python language.

At the end of this lab you will have:

9.2 Testing in standalone mode

a. Copy the module downloaded from the module web site into your standalone Python development folder and test it as a standalone program. Study its source code, and make sure that your tests using the test menu have successfully exercised all functions within this module.

b. Describe the tests which you have carried out in your logbook.

9.3 Extending to validate integers

Study the code for is_email() and for is_valid() carefully. Extend this module with a function is_int() which works in a similar manner to is_email() but with 1 or 2 regular expressions which check for strings which are safely convertible to integer values. Note that an optional leading + or - is valid for an integer, but decimal points are not. Extend the test menu to test your function. Print copies of your function and paste it into your logbook. Write up the test inputs you used to test the function and the outputs for valid and invalid values in your logbook.

9.4 Importing within a CGI program to divide 2 integers

a. Develop a Python CGI program which imports your extended cgiutils module and which uses your function to validate 2 integers supplied via the same HTML form.

b. If these values are valid, your program must divide the first integer by the second and obtain the dividend and remainder and display these 2 results in the browser. You should use functions called from the cgiutils module where suitable, and code application logic within your program or static HTML files otherwise.

c. If an invalid value is submitted using the form make your program output a suitable error HTML message.

Hint: A second integer value 0 is a valid integer, but is not valid for dividing into the first. You probably want your is_int() to stay generic (i.e. treat 0 as valid) and have a specific test for zero in your main CGI program and a different error message compared to the error for an invalid integer.