example.ipynb 10 KB
Newer Older
andy.disch's avatar
andy.disch committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
{
 "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.)"
andy.disch's avatar
andy.disch committed
95
    "invoke debugger Postmortem: %debug"
andy.disch's avatar
andy.disch committed
96
    "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
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
   ]
  },
  {
   "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"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-2-23f75e5e4fc4>, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-23f75e5e4fc4>\"\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
}