example.ipynb 14.3 KB
Newer Older
andy.disch's avatar
andy.disch committed
1 2 3 4 5 6 7 8 9 10 11 12 13
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Debugging code in Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
14 15 16 17 18 19
    "## Debugging with the 'print' Statement\n",
    "\n",
    "- not flexible\n",
    "- time consuming\n",
    "- very simple tool\n",
    "- definitely not best practice"
andy.disch's avatar
andy.disch committed
20 21 22 23
   ]
  },
  {
   "cell_type": "code",
andy.disch's avatar
andy.disch committed
24
   "execution_count": 1,
andy.disch's avatar
andy.disch committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
   "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)",
andy.disch's avatar
andy.disch committed
47
      "\u001b[1;32m<ipython-input-1-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",
andy.disch's avatar
andy.disch committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
      "\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": [
77 78 79 80 81
    "## Debugging with the Python Debugger (pdb)\n",
    "\n",
    "- more functionality\n",
    "- flexible\n",
    "- different option to debug files, modules and code in general"
andy.disch's avatar
andy.disch committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
   ]
  },
  {
   "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",
andy.disch's avatar
andy.disch committed
103 104 105 106 107
    "! (and the following will be a python statement: assigning variables, etc.)\n",
    "\n",
    "Note: \n",
    "\"invoke debugger Postmortem: %debug\"\n",
    "\"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.\""
andy.disch's avatar
andy.disch committed
108 109 110 111
   ]
  },
  {
   "cell_type": "code",
andy.disch's avatar
andy.disch committed
112
   "execution_count": null,
andy.disch's avatar
andy.disch committed
113 114 115 116 117 118 119
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--Return--\n",
andy.disch's avatar
andy.disch committed
120 121
      "> <ipython-input-2-4787f25a8269>(12)<module>()->None\n",
      "-> pdb.set_trace()\n"
andy.disch's avatar
andy.disch committed
122 123 124 125 126 127 128
     ]
    }
   ],
   "source": [
    "import pdb\n",
    "\n",
    "def combine(s1, s2):\n",
129 130 131 132
    "    \"\"\"combine arguments and add quotation marks\"\"\"\n",
    "    s3 = s1 + s2 + s1\n",
    "    s3 = '\"' + s3 + '\"'\n",
    "    return s3\n",
andy.disch's avatar
andy.disch committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    "\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": [
154
    "**A. Running from Command Line**"
andy.disch's avatar
andy.disch committed
155 156 157
   ]
  },
  {
158
   "cell_type": "markdown",
andy.disch's avatar
andy.disch committed
159 160
   "metadata": {},
   "source": [
andy.disch's avatar
andy.disch committed
161
    "python -m pdb \"C:/Users/dischand/PycharmProjects/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py\""
andy.disch's avatar
andy.disch committed
162 163 164 165 166 167
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
168
    "**B. Within the Interpreter**"
andy.disch's avatar
andy.disch committed
169 170 171 172
   ]
  },
  {
   "cell_type": "code",
173
   "execution_count": 1,
andy.disch's avatar
andy.disch committed
174 175 176
   "metadata": {},
   "outputs": [
    {
177 178 179 180
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "> <string>(1)<module>()\n",
andy.disch's avatar
andy.disch committed
181 182 183 184 185 186
      "(Pdb) l\n",
      "[EOF]\n",
      "(Pdb) ci\n",
      "*** NameError: name 'ci' is not defined\n",
      "(Pdb) \n",
      "*** NameError: name 'ci' is not defined\n",
187 188 189 190 191 192
      "(Pdb) c\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for +: 'int' and 'str'",
andy.disch's avatar
andy.disch committed
193 194
     "output_type": "error",
     "traceback": [
andy.disch's avatar
andy.disch committed
195 196 197 198 199 200 201 202
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-dcdc60e27635>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mscript2debug\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mCalculator\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\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[1;32m----> 3\u001b[1;33m \u001b[0mpdb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,\"2\")'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\users\\ingadmin\\appdata\\local\\programs\\python\\python37-32\\Lib\\pdb.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(statement, globals, locals)\u001b[0m\n\u001b[0;32m   1592\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1593\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstatement\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\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-> 1594\u001b[1;33m     \u001b[0mPdb\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstatement\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1595\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1596\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mruneval\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mexpression\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\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;32mc:\\users\\ingadmin\\appdata\\local\\programs\\python\\python37-32\\Lib\\bdb.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, cmd, globals, locals)\u001b[0m\n\u001b[0;32m    583\u001b[0m         \u001b[0msys\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msettrace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtrace_dispatch\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    584\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 585\u001b[1;33m             \u001b[0mexec\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcmd\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    586\u001b[0m         \u001b[1;32mexcept\u001b[0m \u001b[0mBdbQuit\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    587\u001b[0m             \u001b[1;32mpass\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<string>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n",
      "\u001b[1;32m~\\PycharmProjects\\examplepydebuggingandtesting\\examplepydebuggingandtesting\\script2debug.py\u001b[0m in \u001b[0;36mbetafunc\u001b[1;34m(self, *args)\u001b[0m\n\u001b[0;32m     19\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mbetafunc\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\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     20\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0margs\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     22\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     23\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
andy.disch's avatar
andy.disch committed
203 204 205 206
     ]
    }
   ],
   "source": [
207
    "from script2debug import Calculator\n",
andy.disch's avatar
andy.disch committed
208
    "import pdb\n",
209
    "pdb.run('ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,\"2\")')"
andy.disch's avatar
andy.disch committed
210 211 212
   ]
  },
  {
213
   "cell_type": "markdown",
andy.disch's avatar
andy.disch committed
214 215
   "metadata": {},
   "source": [
216
    "**C. Within Your Program**"
andy.disch's avatar
andy.disch committed
217 218 219 220
   ]
  },
  {
   "cell_type": "code",
andy.disch's avatar
andy.disch committed
221
   "execution_count": 2,
andy.disch's avatar
andy.disch committed
222 223 224
   "metadata": {},
   "outputs": [
    {
225 226 227
     "name": "stdout",
     "output_type": "stream",
     "text": [
andy.disch's avatar
andy.disch committed
228
      "> <ipython-input-2-91e9c298fd93>(14)go()\n",
229
      "-> print(i)\n",
andy.disch's avatar
andy.disch committed
230
      "(Pdb) i\n",
231 232 233
      "0\n",
      "(Pdb) c\n",
      "0\n",
andy.disch's avatar
andy.disch committed
234
      "> <ipython-input-2-91e9c298fd93>(13)go()\n",
235 236 237
      "-> pdb.set_trace()\n",
      "(Pdb) c\n",
      "1\n",
andy.disch's avatar
andy.disch committed
238
      "> <ipython-input-2-91e9c298fd93>(14)go()\n",
239 240 241
      "-> print(i)\n",
      "(Pdb) c\n",
      "2\n",
andy.disch's avatar
andy.disch committed
242
      "> <ipython-input-2-91e9c298fd93>(13)go()\n",
243 244 245
      "-> pdb.set_trace()\n",
      "(Pdb) c\n",
      "3\n",
andy.disch's avatar
andy.disch committed
246
      "> <ipython-input-2-91e9c298fd93>(14)go()\n",
247 248 249
      "-> print(i)\n",
      "(Pdb) c\n",
      "4\n"
andy.disch's avatar
andy.disch committed
250 251 252 253 254 255 256 257 258 259 260
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "# encoding: utf-8\n",
    "#\n",
    "\n",
    "import pdb\n",
    "\n",
    "class MyObj(object):\n",
261 262
    "    def __init__(self,count):\n",
    "        self.count= count\n",
andy.disch's avatar
andy.disch committed
263 264 265 266
    "\n",
    "    def go(self):\n",
    "        for i in range(self.count):\n",
    "            pdb.set_trace()\n",
267
    "            print(i)\n",
andy.disch's avatar
andy.disch committed
268 269 270 271 272 273 274 275 276 277
    "        return\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    MyObj(5).go()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    "**D.With Cell Magic in Jupyter Notebook**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NOTE: Enter 'c' at the ipdb>  prompt to continue execution.\n",
      "> \u001b[0;32m<string>\u001b[0m(3)\u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n",
      "\n",
      "ipdb> n\n",
      "> \u001b[0;32m<string>\u001b[0m(5)\u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n",
      "\n",
      "ipdb> l\n",
      "\n",
      "ipdb> n\n",
      "TypeError: must be str, not int\n",
      "> \u001b[0;32m<string>\u001b[0m(5)\u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n",
      "\n",
      "ipdb> p a\n",
      "4\n",
      "ipdb> c\n",
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)\n",
      "\u001b[0;31mTypeError\u001b[0m: must be str, not int\n"
     ]
    }
   ],
   "source": [
    "%%debug \n",
andy.disch's avatar
andy.disch committed
313
    "\n",
314 315 316 317
    "a=4\n",
    "b=\"4\"\n",
    "if 8 == b + a:\n",
    "    print(\"sucess!\")"
andy.disch's avatar
andy.disch committed
318 319 320 321 322 323
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
324 325 326
    "### PyCharm\n",
    "\n",
    "Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/example.py"
andy.disch's avatar
andy.disch committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test code in Python"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unittest\n",
    "\n",
    "Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/example.py"
   ]
  },
  {
346
   "cell_type": "markdown",
andy.disch's avatar
andy.disch committed
347 348
   "metadata": {},
   "source": [
andy.disch's avatar
andy.disch committed
349 350 351 352 353 354 355 356 357 358
    "## hooks for unittests (or use a continuous-integration server)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install python-githooks"
andy.disch's avatar
andy.disch committed
359 360 361
   ]
  },
  {
362
   "cell_type": "markdown",
andy.disch's avatar
andy.disch committed
363 364 365 366 367 368
   "metadata": {},
   "source": [
    "### PyCharm\n",
    "\n",
    "use test runner to explore results"
   ]
369 370 371 372 373 374 375
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
andy.disch's avatar
andy.disch committed
376 377 378 379 380 381 382
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
andy.disch's avatar
andy.disch committed
383 384 385 386 387 388 389 390 391 392 393 394
  },
  "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"
andy.disch's avatar
andy.disch committed
395 396 397 398 399
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}