Commit 9a6773a6 authored by andy.disch's avatar andy.disch

initial commit

parent c92c539b
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Debugging code in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prints debugging"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"before import\n",
"before function a\n",
"before function b\n",
"before __name__ guard\n",
"while function a\n",
"while function b\n",
"function b 10.0\n"
]
},
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-7-7ba457144831>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mfunction_a\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[0mfunction_b\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[1;36m7\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 18\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"after __name__ guard\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"print(\"before import\")\n",
"import math\n",
"\n",
"print(\"before function a\")\n",
"def function_a():\n",
" print(\"while function a\")\n",
"\n",
"print(\"before function b\")\n",
"def function_b():\n",
" print(\"while function b\")\n",
" print(\"function b {}\".format(math.sqrt(100)))\n",
"\n",
"print(\"before __name__ guard\")\n",
"if __name__ == '__main__':\n",
" function_a()\n",
" function_b()\n",
" 7 / 0\n",
"print(\"after __name__ guard\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Breakpoint debugging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pdb\n",
"\n",
"Load as module ('import pdb')\n",
"\n",
" b: set a breakpoint\n",
" c: continue debugging until you hit a breakpoint\n",
" s: step through the code\n",
" n: to go to next line of code\n",
" l: list source code for the current file (default: 11 lines including the line being executed)\n",
" u: navigate up a stack frame\n",
" d: navigate down a stack frame\n",
" p: to print the value of an expression in the current context\n",
"\n",
"(and 'ENTER')\n",
"\n",
"! (and the following will be a python statement: assigning variables, etc.)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--Return--\n",
"> <ipython-input-8-f97d9006584f>(12)<module>()->None\n",
"-> pdb.set_trace()\n",
"(Pdb) l\n",
" 7 \t return s3\n",
" 8 \t\n",
" 9 \t\n",
" 10 \ta = \"aaa\"\n",
" 11 \tb = \"bbb\"\n",
" 12 ->\tpdb.set_trace()\n",
" 13 \tc = \"ccc\"\n",
" 14 \tfinal = combine(a, b)\n",
" 15 \tprint(final)\n",
"[EOF]\n",
"(Pdb) c\n",
"\"aaabbbaaa\"\n"
]
}
],
"source": [
"import pdb\n",
"\n",
"def combine(s1, s2):\n",
" \"\"\"combine arguments and add quotation marks\"\"\"\n",
" s3 = s1 + s2 + s1\n",
" s3 = '\"' + s3 + '\"'\n",
" return s3\n",
"\n",
"\n",
"a = \"aaa\"\n",
"b = \"bbb\"\n",
"pdb.set_trace()\n",
"c = \"ccc\"\n",
"final = combine(a, b)\n",
"print(final)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Execution of pdb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A. Running from command line"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-10-13a4e65d8d0d>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-10-13a4e65d8d0d>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m python -m pdb scriptName.py\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"python -m pdb scriptName.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"B. Within the Interpreter"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'pdb_script'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-11-14471eb6f964>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpdb_script\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mpdb\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mpdb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'pdb_script.MyObj(5).go()'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pdb_script'"
]
}
],
"source": [
"import pdb_script\n",
"import pdb\n",
"pdb.run('pdb_script.MyObj(5).go()')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-12-d15a1049ba44>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-12-d15a1049ba44>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m C. Within Your Program\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"C. Within Your Program"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "Missing parentheses in call to 'print'. Did you mean print(i)? (<ipython-input-13-16ddb2fede32>, line 15)",
"output_type": "error",
"traceback": [
"\u001b[1;36m File \u001b[1;32m\"<ipython-input-13-16ddb2fede32>\"\u001b[1;36m, line \u001b[1;32m15\u001b[0m\n\u001b[1;33m print i\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(i)?\n"
]
}
],
"source": [
"#!/usr/bin/env python\n",
"# encoding: utf-8\n",
"#\n",
"\n",
"import pdb\n",
"\n",
"class MyObj(object):\n",
" count = 5\n",
" def __init__(self):\n",
" self.count= 9\n",
"\n",
" def go(self):\n",
" for i in range(self.count):\n",
" pdb.set_trace()\n",
" print i\n",
" return\n",
"\n",
"if __name__ == '__main__':\n",
" MyObj(5).go()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PyCharm\n",
"\n",
"Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/example.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## (Interpret errors)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Test code in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unittest\n",
"\n",
"Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/example.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## hooks for unittests (or use a continuous-integration server)\n",
"\n",
"Go to: .git/hooks"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [],
"metadata": {},
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Debugging code in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prints debugging"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"before import\n",
"before function a\n",
"before function b\n",
"before __name__ guard\n",
"while function a\n",
"while function b\n",
"function b 10.0\n"
]
},
{
"ename": "ZeroDivisionError",
"evalue": "division by zero",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-7-7ba457144831>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0mfunction_a\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[0mfunction_b\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[1;36m7\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 18\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"after __name__ guard\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"print(\"before import\")\n",
"import math\n",
"\n",
"print(\"before function a\")\n",
"def function_a():\n",
" print(\"while function a\")\n",
"\n",
"print(\"before function b\")\n",
"def function_b():\n",
" print(\"while function b\")\n",
" print(\"function b {}\".format(math.sqrt(100)))\n",
"\n",
"print(\"before __name__ guard\")\n",
"if __name__ == '__main__':\n",
" function_a()\n",
" function_b()\n",
" 7 / 0\n",
"print(\"after __name__ guard\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Breakpoint debugging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pdb\n",
"\n",
"Load as module ('import pdb')\n",
"\n",
" b: set a breakpoint\n",
" c: continue debugging until you hit a breakpoint\n",
" s: step through the code\n",
" n: to go to next line of code\n",
" l: list source code for the current file (default: 11 lines including the line being executed)\n",
" u: navigate up a stack frame\n",
" d: navigate down a stack frame\n",
" p: to print the value of an expression in the current context\n",
"\n",
"(and 'ENTER')\n",
"\n",
"! (and the following will be a python statement: assigning variables, etc.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--Return--\n",
"> <ipython-input-8-f97d9006584f>(12)<module>()->None\n",
"-> pdb.set_trace()\n"
]
}
],
"source": [
"import pdb\n",
"\n",
"def combine(s1, s2):\n",
" \"\"\"combine arguments and add quotation marks\"\"\"\n",
" s3 = s1 + s2 + s1\n",
" s3 = '\"' + s3 + '\"'\n",
" return s3\n",
"\n",
"\n",
"a = \"aaa\"\n",
"b = \"bbb\"\n",
"pdb.set_trace()\n",
"c = \"ccc\"\n",
"final = combine(a, b)\n",
"print(final)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Execution of pdb"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A. Running from command line"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"python -m pdb scriptName.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"B. Within the Interpreter"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pdb_script\n",
"import pdb\n",
"pdb.run('pdb_script.MyObj(5).go()')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"C. Within Your Program"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#!/usr/bin/env python\n",
"# encoding: utf-8\n",
"#\n",
"\n",
"import pdb\n",
"\n",
"class MyObj(object):\n",
" count = 5\n",
" def __init__(self):\n",
" self.count= 9\n",
"\n",
" def go(self):\n",
" for i in range(self.count):\n",
" pdb.set_trace()\n",
" print i\n",
" return\n",
"\n",
"if __name__ == '__main__':\n",
" MyObj(5).go()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PyCharm\n",
"\n",
"Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/exampledebugging.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## (Interpret errors)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
This diff is collapsed.
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""Example debugging module."""
"""Example module."""
import datetime
import pandas
......@@ -50,16 +50,10 @@ if __name__ == '__main__':
TIME_TIME = [datetime.datetime.strptime('2018-01-01 12:00:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:05:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:10:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:15:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:20:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:25:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:30:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:35:00', '%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime('2018-01-01 12:40:00', '%Y-%m-%d %H:%M:%S')]
datetime.datetime.strptime('2018-01-01 12:10:00', '%Y-%m-%d %H:%M:%S')]
VALUE_VALUE = [6.789, 4.567, 9.09, 6.70, 8.70, 3.4, 2.3, 4.56, 2.78]
VALUE_VALUE = [6.789, 4.567, 9.09]
TEST_TS = UrbanDrainageTS(TIME_TIME, VALUE_VALUE)
TMP_TS = UrbanDrainageTS(TIME_TIME, VALUE_VALUE)
TEST_TS.sample_entropy()
TMP_TS.sample_entropy(m_m=1)
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Debugging code in Python"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prints debugging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interactive shells\n",
"\n",
"python in Bash or other system shell\n",
"ipython with synthax highlighting, media support, tab completion etc."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Breakpoint debugging"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Pdb\n",
"Load as module\n",
"Breakpoint possible\n",
"Commands: n, p, l, c, r, q (and ENTER)\n",
"! (and the following will be a python statement: assigning variables) "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--Return--\n",
"> <ipython-input-11-9442abc5edeb>(11)<module>()->None\n",
"-> pdb.set_trace()\n"
]
}
],
"source": [
"import pdb\n",
"def combine(s1, s2):\n",
" \"\"\"combine arguments and add quotation marks\"\"\"\n",
" s3 = s1 + s2 + s1\n",
" s3 = '\"' + s3 + '\"'\n",
" return s3\n",
"\n",
"\n",
"a = \"aaa\"\n",
"b = \"bbb\"\n",
"pdb.set_trace()\n",
"c = \"ccc\"\n",
"final = combine(a, b)\n",
"print(final)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### PyCharm\n",
"run -> view breakpoints: list of all breakpoints, condition when to trigger breakpoint\n",
"debug remote processes: run -> attach local process *never used \n",
"Interpreter with loaded environment (actual sandbox): run debugger, change to console (python with green arrow symbol)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\"aaabbbaaa\"\n"
]
}
],
"source": [
"def combine(s1, s2):\n",
" \"\"\"combine arguments and add quotation marks\"\"\"\n",
" s3 = s1 + s2 + s1\n",
" s3 = '\"' + s3 + '\"'\n",
" return s3\n",
"\n",
"\n",
"a = \"aaa\"\n",
"b = \"bbb\"\n",
"c = \"ccc\"\n",
"final = combine(a, b)\n",
"print(final)"
]
},
{