"""(late posting) -> str of apology letter
To readers,
[I apologize for the late upload. This post was on as a draft and I forgot to press the “publish” button after editing it. Hope you had more time to work on the functions I gave out.]
Cheers,
Your blogger
"""
In Week 9, we mostly reviewed the second assignment with learning the testing methods. Coding becomes useless if it cannot be run, and if it does not run properly either, it also becomes useless. This is why testing method is very important that we were required to write doc test in every assignment we did. For people who do not know what doc test, doc test is a test example showing how the function/method works written in doc string area. For example, let’s say we made a function that adds two numbers. It will be like:
def add(i, j):
return i + j
Coding part seems right but it feels like something is missing. To make this function more user friendly to people who will use your function later, employers, clients, or other scientists for example, you may want to add some explanation of how this code works. You always should keep in mind that sometimes users are not familiar with any computer languages. So it is the best to think of explaining to someone who does not know anything about programming language.
Therefore, doc string is essential in writing functions/methods.
Doc string has three elements: (Input Type) -> Output type, information, doc test (examples).
Also, it is important that you put 3 [ " ] or [ ' ] so that you can write multiple lines of comments (this will not affect any codes when you run the program unless you specially demand to).
The layout of doc string will be:
def some_function(parameter1, parameter2):
""" [(input 1 type, input 2 type) -> output type]
[This part will simply explain what this function/method does and you can add instructions of the function/method or restrictions for the parameters.]
>>> parameter1 = something
>>> parameter2 = something
>>> some_function(parameter1, parameter2)
[Where you write output]
"""
[body of the function goes here]
Before I write an example of full function with doc string, if your function/method returns NoneType a.k.a. returns nothing, you are not required to write doc test for that specific function/method.
Now, going back to our add function, with proper doc string, it will look like:
def add(i, j):
""" (float, float) -> float
Returns a sum of two numbers i and j.
>>> i = 2.7
>>> j = 3.4
>>> add(i, j)
6.1
"""
return i + j
I wrote inputs’ types as float because we did not specify what the numbers we are adding. When two float types are added, the output will be float type as well and that is why I wrote float in output type spot.
Let’s say we want to make a function that only adds integers. Then,
def int_add(i, j):
"""(int, int) -> int
Return a sum of two integers.
Condition:
Both i and j should be integer-type.
>>> i = 3
>>> j = 4
>>> int_add(i, j)
7
"""
if not isinstance(i, int) or not isinstance(j, int):
raise TypeError("Needs integers!")
else:
return i + j
This function only takes integers as input and if something else has been used as a parameter, it raises a TypeError. When you implement this TypeError, and test out the function to raise the error, you’ll get something like this on Python Shell:
>>> int_add(3.0, 5)
Traceback (most recent call last):
File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "/Applications/Wing101.app/Contents/MacOS/src/debug/tserver/_sandbox.py", line 19, in int_add
builtins.TypeError: Needs integers!
Now we have covered the raising errors, and doc string with doc test, we should know how to ‘properly’ test the function.
Let’s look at our int_add function. The function only adds integers and raises an error if numbers are not integers.
Let’s say two students Ted and Ned want to write a doc test for this function.
Ted’s doc test examples look like this:
int_add(3, 4)
int_add(0, 4)
int_add(3, 0)
int_add(0, 0)
Ned’s doc test examples look like this:
int_add(3, 4)
int_add(3, 0)
int_add(3.0, 4)
int_add(3.0, ‘four’)
Whose doc test examples are better?
The answer is Ned. Here are reasons:
Ted’s doc test examples do not quite tell us the information of the function with the improper input values. With Ted’s doc test examples, we won’t be able to find the bugs in the function when improper input values are given.
Ned’s doc tests examples, however, are good enough to test to see if the function runs properly or not. It has an example with two proper input values, an example with one value as 0, an example with a proper input and an improper input value, and an example with two improper input values.
With Ned’s doc tests, we will be able to find out how function works with improper input values pretty well, which is the point of the whole doc test process.
When you guys make a doc test, remember to try the best to test out all the possibilities.
Thank you for reading and I will see you guys next week with my impression on my CSC148 blog.
P.S I see the lack of participation in my last function tests, so I will post the answer in Week12 when I sum up this course's SLog.
No comments:
Post a Comment