Before the Lab
Discussion
- Time
- 5 min =Activity= Group Discussion
- Questions about the quiz?
- Questions about A4
A first example
- Time
- 10 min
- Activity
- Demo
Background for this section is DiP3 1.1
Download humansize.py and save it as
~fcshome/cs2613/labs/L14/humansize.py
Run it from the command line
$ python humansize.py
Run the debugger
$ pudb3 humansize.py
Use
Ctrl-X
to switch to the bottom debugger window (Command line
) and run>>> from humansize import approximate_size >>> approximate_size(10000) >>> approximate_size(10000,False)
Repeat the previous step by running in a terminal
$ python
and then typing in the same lines.
Pytest
- Time
- 20 min
- Activity
- individual
In this part of the course we will be using pytest to write unit tests.
download an initial test file and save it as
~fcshome/cs2613/labs/L14/test_humansize.py
open a terminal
$ pytest-3 test_humansize.py
Convert each working example in DiP3 1.2.1 into a test.
Test coverage reports can be gotten with
pytest-3 --cov=mymodule --cov-report=term-missing
where mymodule is the name of the module in question. Most likely you will need to add more tests for complete coverage. You can do this after the lab.
Modules
- Time
- 15 min
- Activity
- individual
Create a new file
~fcshome/cs2613/labs/L14/client.py
Import the module
humansize
inclient.py
Define a new function
approximate_size
that calls the functionapproximate_size
from the humansize module, but has a default ofFalse
for the parametera kilobyte_is_1024_bytes
Observe that the code in
humansize.py
guarded byif __name__ =='__main__'
does not run when imported intoclient.py
(you might see echos of Racket submodules from Lab 5). Create a similar block inclient.py
, and run it from the command line.
More testing, docstrings
- Time
- 15 min
- Activity
- individual
create
test_client.py
by copying and modifying (if necessary)test_humansize.py
from the command line, run
$ pytest-3
add a test to
test_client.py
to ensure that your new function has a docstringmake sure your new function has a docstring (i.e. that the test you just added passes) and that it makes sense.
Indentation
- Time
- 15 min
- Activity
- individual
One initially surprising aspect of Python is it's use of indentation to define blocks
Start a new file
~/fcshome/cs2613/labs/L14/fizzbuzz.by
. Add the following code, and fix the indentation so that it runs
for i in range(1,101):
if (i%3 == 0 and i%5 == 0):
print("FizzBuzz")
elif (i%5==0):
print("Buzz")
else:
print(i)
Exceptions
- Time
- 20 min
- Activity
- individual
Python throws exceptions when dividing by zero. Suppose we want to return
NaN
, JavaScript style.Start with the following code as
~/fcshome/cs2613/labs/L14/divisive.py
.
def fraction(a,b):
return a/b;
- Add a
try/except
block tofraction
so that the following test-suite (saved astest_divisive.py
) passes
from divisive import fraction
import math
def test_fraction_int():
assert fraction(4,2) == 2;
def test_fraction_NaN():
assert math.isnan(fraction(4,0))
Hint: you can use float('nan')
or math.nan
to generate a NaN
On your own
Lookup up the definition of the FizzBuzz problem and add the missing case for "Fizz" to the program discussed above.
Add enough tests to get complete test coverage for
humansize.py