Created
June 27, 2015 13:29
-
-
Save damontallen/8e68b7b823573ecc9d60 to your computer and use it in GitHub Desktop.
This is short example of using some features of the debug magic in IPython
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def bad_fun(x):\n", | |
" \"This is a functionwritten to test the debug magic\"\n", | |
" y = x^2 +3*x+4\n", | |
" if x ==3 and y!=22:\n", | |
" raise(RuntimeError(\"The symbol ^ is not a power symbol but rather a bit shift.\"))\n", | |
" try:\n", | |
" z = x/0\n", | |
" except ZeroDivisionError:\n", | |
" print(\"Don't divide by zero\")\n", | |
" print(\"{x}\".format(x=x))\n", | |
" print(y)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "RuntimeError", | |
"evalue": "The symbol ^ is not a power symbol but rather a bit shift.", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mRuntimeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-2-09ec772c4271>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mbad_fun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;32m<ipython-input-1-1f18c9d5c240>\u001b[0m in \u001b[0;36mbad_fun\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m^\u001b[0m\u001b[1;36m2\u001b[0m \u001b[1;33m+\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m==\u001b[0m\u001b[1;36m3\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m!=\u001b[0m\u001b[1;36m22\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[1;32mraise\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mRuntimeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The symbol ^ is not a power symbol but rather a bit shift.\"\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 6\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[0mz\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;31mRuntimeError\u001b[0m: The symbol ^ is not a power symbol but rather a bit shift." | |
] | |
} | |
], | |
"source": [ | |
"bad_fun(3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"> \u001b[1;32m<ipython-input-1-1f18c9d5c240>\u001b[0m(5)\u001b[0;36mbad_fun\u001b[1;34m()\u001b[0m\n", | |
"\u001b[1;32m 4 \u001b[1;33m \u001b[1;32mif\u001b[0m \u001b[0mx\u001b[0m \u001b[1;33m==\u001b[0m\u001b[1;36m3\u001b[0m \u001b[1;32mand\u001b[0m \u001b[0my\u001b[0m\u001b[1;33m!=\u001b[0m\u001b[1;36m22\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[0m\u001b[1;32m----> 5 \u001b[1;33m \u001b[1;32mraise\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mRuntimeError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"The symbol ^ is not a power symbol but rather a bit shift.\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[0m\u001b[1;32m 6 \u001b[1;33m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[0m\n", | |
"ipdb> h\n", | |
"\n", | |
"Documented commands (type help <topic>):\n", | |
"========================================\n", | |
"EOF cl disable interact pdef quit source up \n", | |
"a clear display j pdoc r step w \n", | |
"alias commands down jump pfile restart tbreak whatis\n", | |
"args condition enable ll pinfo return u where \n", | |
"b cont exit longlist pinfo2 retval unalias \n", | |
"break continue h n pp run undisplay\n", | |
"bt d help next psource rv unt \n", | |
"c debug ignore p q s until \n", | |
"\n", | |
"Miscellaneous help topics:\n", | |
"==========================\n", | |
"pdb exec\n", | |
"\n", | |
"Undocumented commands:\n", | |
"======================\n", | |
"l list\n", | |
"\n", | |
"ipdb> a\n", | |
"x = 3\n", | |
"ipdb> p y\n", | |
"12\n", | |
"ipdb> q\n" | |
] | |
} | |
], | |
"source": [ | |
"%debug" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"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.4.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment