From 9a6773a62a467b1a3a6059096ed79b855c154f14 Mon Sep 17 00:00:00 2001 From: Disch Date: Thu, 2 May 2019 15:20:47 +0200 Subject: [PATCH] initial commit --- .../example-checkpoint.ipynb | 324 +++++++++++++++++ .../exampledebugging-checkpoint.ipynb | 243 ++++++++++++- .../exampletesting-checkpoint.ipynb | 6 - examplepydebuggingandtesting/example.ipynb | 344 ++++++++++++++++++ .../{exampledebugging.py => example.py} | 16 +- .../exampledebugging.ipynb | 174 --------- .../exampletesting.ipynb | 62 ---- .../exampletesting.py | 43 --- 8 files changed, 914 insertions(+), 298 deletions(-) create mode 100644 examplepydebuggingandtesting/.ipynb_checkpoints/example-checkpoint.ipynb delete mode 100644 examplepydebuggingandtesting/.ipynb_checkpoints/exampletesting-checkpoint.ipynb create mode 100644 examplepydebuggingandtesting/example.ipynb rename examplepydebuggingandtesting/{exampledebugging.py => example.py} (72%) delete mode 100644 examplepydebuggingandtesting/exampledebugging.ipynb delete mode 100644 examplepydebuggingandtesting/exampletesting.ipynb delete mode 100644 examplepydebuggingandtesting/exampletesting.py diff --git a/examplepydebuggingandtesting/.ipynb_checkpoints/example-checkpoint.ipynb b/examplepydebuggingandtesting/.ipynb_checkpoints/example-checkpoint.ipynb new file mode 100644 index 0000000..e786148 --- /dev/null +++ b/examplepydebuggingandtesting/.ipynb_checkpoints/example-checkpoint.ipynb @@ -0,0 +1,324 @@ +{ + "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\u001b[0m in \u001b[0;36m\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", + "> (12)()->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 (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\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\u001b[0m in \u001b[0;36m\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 (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\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)? (, line 15)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\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 +} diff --git a/examplepydebuggingandtesting/.ipynb_checkpoints/exampledebugging-checkpoint.ipynb b/examplepydebuggingandtesting/.ipynb_checkpoints/exampledebugging-checkpoint.ipynb index 2fd6442..088596b 100644 --- a/examplepydebuggingandtesting/.ipynb_checkpoints/exampledebugging-checkpoint.ipynb +++ b/examplepydebuggingandtesting/.ipynb_checkpoints/exampledebugging-checkpoint.ipynb @@ -1,6 +1,245 @@ { - "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\u001b[0m in \u001b[0;36m\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", + "> (12)()->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 } diff --git a/examplepydebuggingandtesting/.ipynb_checkpoints/exampletesting-checkpoint.ipynb b/examplepydebuggingandtesting/.ipynb_checkpoints/exampletesting-checkpoint.ipynb deleted file mode 100644 index 2fd6442..0000000 --- a/examplepydebuggingandtesting/.ipynb_checkpoints/exampletesting-checkpoint.ipynb +++ /dev/null @@ -1,6 +0,0 @@ -{ - "cells": [], - "metadata": {}, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/examplepydebuggingandtesting/example.ipynb b/examplepydebuggingandtesting/example.ipynb new file mode 100644 index 0000000..9e35442 --- /dev/null +++ b/examplepydebuggingandtesting/example.ipynb @@ -0,0 +1,344 @@ +{ + "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\u001b[0m in \u001b[0;36m\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", + "> (12)()->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 (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\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\u001b[0m in \u001b[0;36m\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 (, line 1)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\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)? (, line 15)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\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" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (, line 3)", + "output_type": "error", + "traceback": [ + "\u001b[1;36m File \u001b[1;32m\"\"\u001b[1;36m, line \u001b[1;32m3\u001b[0m\n\u001b[1;33m use test runner to explore results\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n" + ] + } + ], + "source": [ + "### PyCharm\n", + "\n", + "use test runner to explore results" + ] + } + ], + "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 +} diff --git a/examplepydebuggingandtesting/exampledebugging.py b/examplepydebuggingandtesting/example.py similarity index 72% rename from examplepydebuggingandtesting/exampledebugging.py rename to examplepydebuggingandtesting/example.py index 79afbf4..018f1ff 100644 --- a/examplepydebuggingandtesting/exampledebugging.py +++ b/examplepydebuggingandtesting/example.py @@ -1,7 +1,7 @@ #!/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) diff --git a/examplepydebuggingandtesting/exampledebugging.ipynb b/examplepydebuggingandtesting/exampledebugging.ipynb deleted file mode 100644 index 6bbfdad..0000000 --- a/examplepydebuggingandtesting/exampledebugging.ipynb +++ /dev/null @@ -1,174 +0,0 @@ -{ - "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", - "> (11)()->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)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## (Interpret errors)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "%run -i 'C:\\Users\\dischand\\PycharmProjects\\examplepydebuggingandtesting\\examplepydebuggingandtesting\\exampledebugging.py'" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "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 -} diff --git a/examplepydebuggingandtesting/exampletesting.ipynb b/examplepydebuggingandtesting/exampletesting.ipynb deleted file mode 100644 index ffdc157..0000000 --- a/examplepydebuggingandtesting/exampletesting.ipynb +++ /dev/null @@ -1,62 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Testing code in Python" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## unittests" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## hooks for unittests (or than use a continuous-integration server)\n", - ".git/hooks" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### PyCharm\n", - "use test runner to explore results" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "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 -} diff --git a/examplepydebuggingandtesting/exampletesting.py b/examplepydebuggingandtesting/exampletesting.py deleted file mode 100644 index 5fed0cd..0000000 --- a/examplepydebuggingandtesting/exampletesting.py +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -"""Example testing module.""" - -import pandas -import numpy -import pandas.api.types - - -class UrbanDrainageTS(pandas.DataFrame): - """ class for handling single measurement time series. """ - - def __init__(self, time, value): - """description of the classes initiation.""" - if isinstance(time, list): - time = pandas.Series(time) - if isinstance(value, list): - value = pandas.Series(value) - d_f = pandas.DataFrame({'time': time, 'value': value}) - assert pandas.api.types.is_datetime64_any_dtype(time), "time is not a datetime: %r" % time - assert pandas.api.types.is_float_dtype(value), "value is not a float: %r" % value - super().__init__(d_f) - return - - def sample_entropy(self, m_m=2): - """compute sample entropy.""" - u_u = self.value - r_r = 0.2 * numpy.std(self.value) - - def _maxdist(x_i, x_j): - """compute sample entropy.""" - result = max([abs(ua_ua - va_va) for ua_ua, va_va in zip(x_i, x_j)]) - return result - - def _phi(m_m): - """compute sample entropy.""" - x_x = [[u_u[j_j] for j_j in range(i_i, i_i + m_m - 1 + 1)] for i_i in range(n_n - m_m + 1)] - c_c = 1. * numpy.array([len([1 for j_j in range(len(x_x)) if i_i != j_j and _maxdist(x_x[i_i], x_x[j_j]) <= r_r]) for i_i in range(len(x_x))]) - return sum(c_c) - - n_n = len(u_u) - return -numpy.log(_phi(m_m + 1) / _phi(m_m)) -- GitLab