cheat_sheets
cheat_sheets
Reacting to bugs
1. Use debugger to isolate bug
2. Add test case that reproduces bug to test suite
3. Correct the bug
4. Check that all tests pass
2. Make changes
svn add
svn delete
svn copy
svn move
pylint
pylint display very long list with all options
pylint filename.py check file for consistency with standards
pylint module check module
unittest cheatsheet
Basic structure of a test suite
import unittest
class FirstTestCase(unittest.TestCase):
def setUp(self):
"""setUp is called before every test"""
pass
def tearDown(self):
"""tearDown is called at the end of every test"""
pass
def testtruisms(self):
"""All methods beginning with ‘test’ are executed"""
self.assertTrue(True)
self.assertFalse(False)
class SecondTestCase(unittest.TestCase):
def testapproximation(self):
self.assertAlmostEqual(1.1, 1.15, 1)
if __name__ == '__main__':
# run all TestCase's in this module
unittest.main()
assertRaises(exception, callable, ...) Fail if the function callable does not raise an
exception of class exception. If additional
positional or keyword arguments are given,
they are passed to callable.
timeit cheatsheet
Execute expression one million times, return elapsed time in seconds:
For a more precise control of timing, use the repeat method; it returns a list of repeated
measurements, in seconds:
In ipython:
%pdb enter the debugger automatically after an exception is raised
%debug enter the debugger post-mortem where the exception was thrown
Debugger commands
h (help) [command] print help about command
n (next) execute current line of code, go to next line
c (continue) continue executing the program until next
breakpoint, exception, or end of the program
s (step into) execute current line of code; if a function is
called, follow execution inside the function
l (list) print code around the current line
w (where) show a trace of the function call that led to the
current line
p (print) print the value of a variable
q (quit) leave the debugger
b (break) [lineno | function[, condition]] set a breakpoint at a given line number or
function, stop execution there if condition is
fulfilled
cl (clear) clear a breakpoint
! (execute) execute a python command
<enter> repeat last command