Python代写|CSC108 Lab 10: Testing and Regex


Welcome to your last lab in CSC108! This lab will focus on testing and regular expressions. In particular, we will focus on
using regular expressions for searching and unit testing. There will be two parts for you to complete. The first part involves
implementing a function using the re library to find an email within a string. The second part is related to the first part, and
you will write unittests to test the function that you just implemented.

1 Using Regular Expressions

There are many uses for regular expressions. But they are perhaps most useful when it comes to input validation. For example,
if you want to verify that the postal code entered in a particular text field is valid, or for when you want to parse a post and
highlight all of the valid hashtags. In these scenarios, it is much easier to use a regular expression than many layered levels of if
statements and loops. As a result, regular expressions are built into many modern programming languages, including Python’s
re library, which was added in Python 1.5 (released in 1997!).

Task: Your first task is to implement a regex function that finds a email within a string. Download from Quercus and have a look at the find special email() function. Read the docstring of the function and implement it. You must
use the re library, as we want you to practice using regular expressions, but we are not restricting you to what you must use
within the library. You may, however, find useful. Here is a quick demo of how to use it:

>> import re
>>> s = ‘Michael Liut’
>>> match =’M\w+l’, s)

Once you finish implementing this function, move on to the next step.

2 Testing

For this part of the lab, you will write tests to check that the previous function you just wrote is correct. Download lab10
from Quercus and read the file carefully. You should add more tests to this file following the format of the given test template.
In particular, the functions that you add must start with the word test and must have an assert statement as the very last
line. The test template has a good explanation in the docstring which you should read.

Be careful when writing your tests that you are not writing redundant test cases. It is easy to get carried away and write
tests that are extremely similar. However, the quality of a set of tests is not determined by the number of tests, but rather by
the number of distinct cases that the tests cover as a whole.

We are providing a short function at the end of the file which will allow you to run your tests on your current implemen
tation. This can be invoked by simply running the file. It will tell you how many tests you have defined and how many pass
or fail. Be very careful not to hard-code your test functions to test a specific function. Note that the test template takes in a
Callable as it’s input parameter, and we will be using this to pass it different implementations to test. Also keep in mind that
the goal is not to have all tests pass, but rather to have some tests fail if given an incorrect implementation, and only all pass
when given a correct one.