Git repository for the talk: https://git.becs.aalto.fi/complex-networks/tutorial
In this talk, I will cover:
Here is a briefest example:
1 2 3 4 | import factorial
if factorial.fact(5) != 120:
print "factorial of 5 is wrong"
|
Here is an example that uses testing tools:
1 2 3 4 5 6 7 8 | from nose import assert_equal, assert_almost_equal
import factorial
def test_factorial():
assert_equal(120, factorial.fact(5))
assert_equal(.88622, factorial.gamma(1.5))
|
Use to run tests.
Usage:
1 2 3 $ nosetests $ nosetests test_NAME.py $ nosetests test_NAME.py:test1
Standard output is hidden by default, unless a test fails! Use -s to make all standard output be shown.
Example:
1 2 3 4 5 6 | >>> assert_set_equal(set([1, 2, 3]), set([1, 2, 4]) )
AssertionError: Items in the first set but not the second:
3
Items in the second set but not the first:
4
|
Look at how it prints exactly what the difference is. Maybe you don't even need to go debugging it yourself if this is enough to realize what went wrong.
You can also use the assert keyword:
1 | assert func(5) == 1
|
See the list of assert* methods https://docs.python.org/2/library/unittest.html#assert-methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | nose.tools.assert_almost_equal
nose.tools.assert_almost_equals
nose.tools.assert_dict_contains_subset
nose.tools.assert_dict_equal
nose.tools.assert_equal
nose.tools.assert_equals
nose.tools.assert_false
nose.tools.assert_greater
nose.tools.assert_greater_equal
nose.tools.assert_in
nose.tools.assert_is
nose.tools.assert_is_instance
nose.tools.assert_is_none
nose.tools.assert_is_not
nose.tools.assert_is_not_none
nose.tools.assert_items_equal
nose.tools.assert_less
nose.tools.assert_less_equal
nose.tools.assert_list_equal
nose.tools.assert_multi_line_equal
nose.tools.assert_not_almost_equal
nose.tools.assert_not_almost_equals
nose.tools.assert_not_equal
nose.tools.assert_not_equals
nose.tools.assert_not_in
nose.tools.assert_not_is_instance
nose.tools.assert_not_regexp_matches
nose.tools.assert_raises
nose.tools.assert_raises_regexp
nose.tools.assert_regexp_matches
nose.tools.assert_sequence_equal
nose.tools.assert_set_equal
nose.tools.assert_true
nose.tools.assert_tuple_equal
|
If a test fails, you can automatically invoke the debugger:
Useful pdb commands:
Full list of commands: https://docs.python.org/2/library/pdb.html#debugger-commands
The ipython debugger is functionally equivalent to pdb.
If you want to invoke the debugger at one specific point, just use the raise keyword at that point:
1 | raise
|
1 2 | if n == 5:
raise
|
Instructions:
Instructions:
Problem: https://projecteuler.net/problem=1
or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23**.
Find the sum of all the multiples of 3 or 5 below 1000.
Instructions:
Make a module e001.py and solve this problem for general n.
Make a module test_001.py and write a test for this function. Hint:
1 2 3 4 5 | from nose.tools import *
from e001 import euler001
def test_001():
...
|