Commit 55c6163d authored by andy.disch's avatar andy.disch
Browse files

test hook

parent 1cb2a31a
%% Cell type:markdown id: tags:
# Debugging code in Python
%% Cell type:markdown id: tags:
## Debugging with the 'print' Statement
- not flexible
- time consuming
- very simple tool
- definitely not best practice
%% Cell type:code id: tags:
``` python
print("before import")
import math
print("before function a")
def function_a():
print("while function a")
print("before function b")
def function_b():
print("while function b")
print("function b {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
7 / 0
print("after __name__ guard")
```
%%%% Output: stream
before import
before function a
before function b
before __name__ guard
while function a
while function b
function b 10.0
%%%% Output: error
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-7-7ba457144831> in <module>
<ipython-input-1-7ba457144831> in <module>
15 function_a()
16 function_b()
---> 17 7 / 0
18 print("after __name__ guard")
ZeroDivisionError: division by zero
%% Cell type:markdown id: tags:
## Debugging with the Python Debugger (pdb)
- more functionality
- flexible
- different option to debug files, modules and code in general
%% Cell type:markdown id: tags:
### Pdb
Load as module ('import pdb')
b: set a breakpoint
c: continue debugging until you hit a breakpoint
s: step through the code
n: to go to next line of code
l: list source code for the current file (default: 11 lines including the line being executed)
u: navigate up a stack frame
d: navigate down a stack frame
p: to print the value of an expression in the current context
(and 'ENTER')
! (and the following will be a python statement: assigning variables, etc.)
Note:
"invoke debugger Postmortem: %debug"
"if you have a bug in C code embedded in Python, pdb is useless. For this we turn to the gnu debugger, gdb, available on Linux."
%% Cell type:code id: tags:
``` python
import pdb
def combine(s1, s2):
"""combine arguments and add quotation marks"""
s3 = s1 + s2 + s1
s3 = '"' + s3 + '"'
return s3
a = "aaa"
b = "bbb"
pdb.set_trace()
c = "ccc"
final = combine(a, b)
print(final)
```
%%%% Output: stream
--Return--
> <ipython-input-6-4787f25a8269>(12)<module>()->None
> <ipython-input-2-4787f25a8269>(12)<module>()->None
-> pdb.set_trace()
(Pdb) l
7 return s3
8
9
10 a = "aaa"
11 b = "bbb"
12 -> pdb.set_trace()
13 c = "ccc"
14 final = combine(a, b)
15 print(final)
[EOF]
(Pdb) p a
'aaa'
(Pdb) p c
'ccc'
(Pdb) c
"aaabbbaaa"
%% Cell type:markdown id: tags:
### Execution of pdb
%% Cell type:markdown id: tags:
**A. Running from Command Line**
%% Cell type:markdown id: tags:
python -m pdb "/home/heinrich/gitrepos/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py"
python -m pdb "C:/Users/dischand/PycharmProjects/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py"
%% Cell type:markdown id: tags:
**B. Within the Interpreter**
%% Cell type:code id: tags:
``` python
from script2debug import Calculator
import pdb
pdb.run('ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,"2")')
```
%%%% Output: stream
> <string>(1)<module>()
(Pdb) n
TypeError: unsupported operand type(s) for +: 'int' and 'str'
> <string>(1)<module>()
(Pdb) ci.num
5
(Pdb) l
[EOF]
(Pdb) ci
*** NameError: name 'ci' is not defined
(Pdb)
*** NameError: name 'ci' is not defined
(Pdb) c
%%%% Output: error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-dcdc60e27635> in <module>
1 from script2debug import Calculator
2 import pdb
----> 3 pdb.run('ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,"2")')
/usr/lib/python3.6/pdb.py in run(statement, globals, locals)
1570
1571 def run(statement, globals=None, locals=None):
-> 1572 Pdb().run(statement, globals, locals)
1573
1574 def runeval(expression, globals=None, locals=None):
/usr/lib/python3.6/bdb.py in run(self, cmd, globals, locals)
432 sys.settrace(self.trace_dispatch)
433 try:
--> 434 exec(cmd, globals, locals)
435 except BdbQuit:
436 pass
c:\users\ingadmin\appdata\local\programs\python\python37-32\Lib\pdb.py in run(statement, globals, locals)
1592
1593 def run(statement, globals=None, locals=None):
-> 1594 Pdb().run(statement, globals, locals)
1595
1596 def runeval(expression, globals=None, locals=None):
c:\users\ingadmin\appdata\local\programs\python\python37-32\Lib\bdb.py in run(self, cmd, globals, locals)
583 sys.settrace(self.trace_dispatch)
584 try:
--> 585 exec(cmd, globals, locals)
586 except BdbQuit:
587 pass
<string> in <module>
~/gitrepos/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py in betafunc(self, *args)
18
~\PycharmProjects\examplepydebuggingandtesting\examplepydebuggingandtesting\script2debug.py in betafunc(self, *args)
19 def betafunc(self,*args):
---> 20 self.args = args
21 return sum(self.args)
20 self.args = args
---> 21 return sum(self.args)
22
23
TypeError: unsupported operand type(s) for +: 'int' and 'str'
%% Cell type:markdown id: tags:
**C. Within Your Program**
%% Cell type:code id: tags:
``` python
#!/usr/bin/env python
# encoding: utf-8
#
import pdb
class MyObj(object):
def __init__(self,count):
self.count= count
def go(self):
for i in range(self.count):
pdb.set_trace()
print(i)
return
if __name__ == '__main__':
MyObj(5).go()
```
%%%% Output: stream
> <ipython-input-4-91e9c298fd93>(14)go()
> <ipython-input-2-91e9c298fd93>(14)go()
-> print(i)
(Pdb) p i
(Pdb) i
0
(Pdb) c
0
> <ipython-input-4-91e9c298fd93>(13)go()
> <ipython-input-2-91e9c298fd93>(13)go()
-> pdb.set_trace()
(Pdb) c
1
> <ipython-input-4-91e9c298fd93>(14)go()
> <ipython-input-2-91e9c298fd93>(14)go()
-> print(i)
(Pdb) c
2
> <ipython-input-4-91e9c298fd93>(13)go()
> <ipython-input-2-91e9c298fd93>(13)go()
-> pdb.set_trace()
(Pdb) c
3
> <ipython-input-4-91e9c298fd93>(14)go()
> <ipython-input-2-91e9c298fd93>(14)go()
-> print(i)
(Pdb) c
4
%% Cell type:markdown id: tags:
**D.With Cell Magic in Jupyter Notebook**
%% Cell type:code id: tags:
``` python
%%debug
a=4
b="4"
if 8 == b + a:
print("sucess!")
```
%%%% Output: stream
NOTE: Enter 'c' at the ipdb> prompt to continue execution.
> <string>(3)<module>()
ipdb> n
> <string>(5)<module>()
ipdb> l
ipdb> n
TypeError: must be str, not int
> <string>(5)<module>()
ipdb> p a
4
ipdb> c
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: must be str, not int
%% Cell type:markdown id: tags:
### PyCharm
Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/example.py
%% Cell type:markdown id: tags:
# Test code in Python
%% Cell type:markdown id: tags:
## Unittest
Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/example.py
%% Cell type:markdown id: tags:
## hooks for unittests (or use a continuous-integration server)
### PyCharm
Go to: .git/hooks
use test runner to explore results
%% Cell type:markdown id: tags:
### PyCharm
## hooks for unittests (or use a continuous-integration server)
use test runner to explore results
%% Cell type:markdown id: tags:
go to: C:\Users\dischand\PycharmProjects\examplepydebuggingandtesting\.git\hooks
#!/bin/sh
if [[ `python -m unittest C:/Users/dischand/PycharmProjects/examplepydebuggingandtesting/tests/test_urban_drainage_ts.py` ]]
then
echo "> Tests DID NOT pass !"
exit 1
else
echo "> Tests passed !"
fi
echo "Ready to commit !"
exit 0
%% Cell type:code id: tags:
``` python
```
......
%% Cell type:markdown id: tags:
# Debugging code in Python
%% Cell type:markdown id: tags:
## Debugging with the 'print' Statement
- not flexible
- time consuming
- very simple tool
- definitely not best practice
%% Cell type:code id: tags:
``` python
print("before import")
import math
print("before function a")
def function_a():
print("while function a")
print("before function b")
def function_b():
print("while function b")
print("function b {}".format(math.sqrt(100)))
print("before __name__ guard")
if __name__ == '__main__':
function_a()
function_b()
7 / 0
print("after __name__ guard")
```
%%%% Output: stream
before import
before function a
before function b
before __name__ guard
while function a
while function b
function b 10.0
%%%% Output: error
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-1-7ba457144831> in <module>
15 function_a()
16 function_b()
---> 17 7 / 0
18 print("after __name__ guard")
ZeroDivisionError: division by zero
%% Cell type:markdown id: tags:
## Debugging with the Python Debugger (pdb)
- more functionality
- flexible
- different option to debug files, modules and code in general
%% Cell type:markdown id: tags:
### Pdb
Load as module ('import pdb')
b: set a breakpoint
c: continue debugging until you hit a breakpoint
s: step through the code
n: to go to next line of code
l: list source code for the current file (default: 11 lines including the line being executed)
u: navigate up a stack frame
d: navigate down a stack frame
p: to print the value of an expression in the current context
(and 'ENTER')
! (and the following will be a python statement: assigning variables, etc.)
Note:
"invoke debugger Postmortem: %debug"
"if you have a bug in C code embedded in Python, pdb is useless. For this we turn to the gnu debugger, gdb, available on Linux."
%% Cell type:code id: tags:
``` python
import pdb
def combine(s1, s2):
"""combine arguments and add quotation marks"""
s3 = s1 + s2 + s1
s3 = '"' + s3 + '"'
return s3
a = "aaa"
b = "bbb"
pdb.set_trace()
c = "ccc"
final = combine(a, b)
print(final)
```
%%%% Output: stream
--Return--
> <ipython-input-2-4787f25a8269>(12)<module>()->None
-> pdb.set_trace()
%% Cell type:markdown id: tags:
### Execution of pdb
%% Cell type:markdown id: tags:
**A. Running from Command Line**
%% Cell type:markdown id: tags:
python -m pdb "C:/Users/dischand/PycharmProjects/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py"
%% Cell type:markdown id: tags:
**B. Within the Interpreter**
%% Cell type:code id: tags:
``` python
from script2debug import Calculator
import pdb
pdb.run('ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,"2")')
```
%%%% Output: stream
> <string>(1)<module>()
(Pdb) l
[EOF]
(Pdb) ci
*** NameError: name 'ci' is not defined
(Pdb)
*** NameError: name 'ci' is not defined
(Pdb) c
%%%% Output: error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-dcdc60e27635> in <module>
1 from script2debug import Calculator
2 import pdb
----> 3 pdb.run('ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,"2")')
c:\users\ingadmin\appdata\local\programs\python\python37-32\Lib\pdb.py in run(statement, globals, locals)
1592
1593 def run(statement, globals=None, locals=None):
-> 1594 Pdb().run(statement, globals, locals)
1595
1596 def runeval(expression, globals=None, locals=None):
c:\users\ingadmin\appdata\local\programs\python\python37-32\Lib\bdb.py in run(self, cmd, globals, locals)
583 sys.settrace(self.trace_dispatch)
584 try:
--> 585 exec(cmd, globals, locals)
586 except BdbQuit:
587 pass
<string> in <module>
~\PycharmProjects\examplepydebuggingandtesting\examplepydebuggingandtesting\script2debug.py in betafunc(self, *args)
19 def betafunc(self,*args):
20 self.args = args
---> 21 return sum(self.args)
22
23
TypeError: unsupported operand type(s) for +: 'int' and 'str'
%% Cell type:markdown id: tags:
**C. Within Your Program**
%% Cell type:code id: tags:
``` python
#!/usr/bin/env python
# encoding: utf-8
#
import pdb
class MyObj(object):
def __init__(self,count):
self.count= count
def go(self):
for i in range(self.count):
pdb.set_trace()
print(i)
return
if __name__ == '__main__':
MyObj(5).go()
```
%%%% Output: stream
> <ipython-input-2-91e9c298fd93>(14)go()
-> print(i)
(Pdb) i
0
(Pdb) c
0
> <ipython-input-2-91e9c298fd93>(13)go()
-> pdb.set_trace()
(Pdb) c
1
> <ipython-input-2-91e9c298fd93>(14)go()
-> print(i)
(Pdb) c
2
> <ipython-input-2-91e9c298fd93>(13)go()
-> pdb.set_trace()
(Pdb) c
3
> <ipython-input-2-91e9c298fd93>(14)go()
-> print(i)
(Pdb) c
4
%% Cell type:markdown id: tags:
**D.With Cell Magic in Jupyter Notebook**
%% Cell type:code id: tags:
``` python
%%debug
a=4
b="4"
if 8 == b + a:
print("sucess!")
```
%%%% Output: stream
NOTE: Enter 'c' at the ipdb> prompt to continue execution.
> <string>(3)<module>()
ipdb> n
> <string>(5)<module>()
ipdb> l