Commit 4c754847 authored by christian.foerster's avatar christian.foerster

added some things in jupyter notebook

parent 463d34d5
...@@ -11,7 +11,12 @@ ...@@ -11,7 +11,12 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Prints debugging" "## Debugging with the 'print' Statement\n",
"\n",
"- not flexible\n",
"- time consuming\n",
"- very simple tool\n",
"- definitely not best practice"
] ]
}, },
{ {
...@@ -69,7 +74,11 @@ ...@@ -69,7 +74,11 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Breakpoint debugging" "## Debugging with the Python Debugger (pdb)\n",
"\n",
"- more functionality\n",
"- flexible\n",
"- different option to debug files, modules and code in general"
] ]
}, },
{ {
...@@ -100,7 +109,7 @@ ...@@ -100,7 +109,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -108,10 +117,10 @@ ...@@ -108,10 +117,10 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"--Return--\n", "--Return--\n",
"> <ipython-input-8-f97d9006584f>(12)<module>()->None\n", "> <ipython-input-6-4787f25a8269>(12)<module>()->None\n",
"-> pdb.set_trace()\n", "-> pdb.set_trace()\n",
"(Pdb) l\n", "(Pdb) l\n",
" 7 \t return s3\n", " 7 \t return s3\n",
" 8 \t\n", " 8 \t\n",
" 9 \t\n", " 9 \t\n",
" 10 \ta = \"aaa\"\n", " 10 \ta = \"aaa\"\n",
...@@ -121,6 +130,10 @@ ...@@ -121,6 +130,10 @@
" 14 \tfinal = combine(a, b)\n", " 14 \tfinal = combine(a, b)\n",
" 15 \tprint(final)\n", " 15 \tprint(final)\n",
"[EOF]\n", "[EOF]\n",
"(Pdb) p a\n",
"'aaa'\n",
"(Pdb) p c\n",
"'ccc'\n",
"(Pdb) c\n", "(Pdb) c\n",
"\"aaabbbaaa\"\n" "\"aaabbbaaa\"\n"
] ]
...@@ -130,10 +143,10 @@ ...@@ -130,10 +143,10 @@
"import pdb\n", "import pdb\n",
"\n", "\n",
"def combine(s1, s2):\n", "def combine(s1, s2):\n",
" \"\"\"combine arguments and add quotation marks\"\"\"\n", " \"\"\"combine arguments and add quotation marks\"\"\"\n",
" s3 = s1 + s2 + s1\n", " s3 = s1 + s2 + s1\n",
" s3 = '\"' + s3 + '\"'\n", " s3 = '\"' + s3 + '\"'\n",
" return s3\n", " return s3\n",
"\n", "\n",
"\n", "\n",
"a = \"aaa\"\n", "a = \"aaa\"\n",
...@@ -155,86 +168,101 @@ ...@@ -155,86 +168,101 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"A. Running from command line" "**A. Running from Command Line**"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": 10,
"metadata": {}, "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": [ "source": [
"python -m pdb scriptName.py" "python -m pdb \"/home/heinrich/gitrepos/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py\""
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"B. Within the Interpreter" "**B. Within the Interpreter**"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "ModuleNotFoundError", "name": "stdout",
"evalue": "No module named 'pdb_script'", "output_type": "stream",
"text": [
"> <string>(1)<module>()\n",
"(Pdb) n\n",
"TypeError: unsupported operand type(s) for +: 'int' and 'str'\n",
"> <string>(1)<module>()\n",
"(Pdb) ci.num\n",
"5\n",
"(Pdb) c\n"
]
},
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
"output_type": "error", "output_type": "error",
"traceback": [ "traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;31mTypeError\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[0;32m<ipython-input-1-dcdc60e27635>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mscript2debug\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mCalculator\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpdb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mpdb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,\"2\")'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pdb_script'" "\u001b[0;32m/usr/lib/python3.6/pdb.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(statement, globals, locals)\u001b[0m\n\u001b[1;32m 1570\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1571\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstatement\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1572\u001b[0;31m \u001b[0mPdb\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstatement\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1573\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1574\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mruneval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexpression\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.6/bdb.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, cmd, globals, locals)\u001b[0m\n\u001b[1;32m 432\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msettrace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrace_dispatch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 433\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 434\u001b[0;31m \u001b[0mexec\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcmd\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 435\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mBdbQuit\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 436\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<string>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n",
"\u001b[0;32m~/gitrepos/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py\u001b[0m in \u001b[0;36mbetafunc\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbetafunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
] ]
} }
], ],
"source": [ "source": [
"import pdb_script\n", "from script2debug import Calculator\n",
"import pdb\n", "import pdb\n",
"pdb.run('pdb_script.MyObj(5).go()')" "pdb.run('ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,\"2\")')"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": 12,
"metadata": {}, "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": [ "source": [
"C. Within Your Program" "**C. Within Your Program**"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "SyntaxError", "name": "stdout",
"evalue": "Missing parentheses in call to 'print'. Did you mean print(i)? (<ipython-input-13-16ddb2fede32>, line 15)", "output_type": "stream",
"output_type": "error", "text": [
"traceback": [ "> <ipython-input-4-91e9c298fd93>(14)go()\n",
"\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" "-> print(i)\n",
"(Pdb) p i\n",
"0\n",
"(Pdb) c\n",
"0\n",
"> <ipython-input-4-91e9c298fd93>(13)go()\n",
"-> pdb.set_trace()\n",
"(Pdb) c\n",
"1\n",
"> <ipython-input-4-91e9c298fd93>(14)go()\n",
"-> print(i)\n",
"(Pdb) c\n",
"2\n",
"> <ipython-input-4-91e9c298fd93>(13)go()\n",
"-> pdb.set_trace()\n",
"(Pdb) c\n",
"3\n",
"> <ipython-input-4-91e9c298fd93>(14)go()\n",
"-> print(i)\n",
"(Pdb) c\n",
"4\n"
] ]
} }
], ],
...@@ -246,14 +274,13 @@ ...@@ -246,14 +274,13 @@
"import pdb\n", "import pdb\n",
"\n", "\n",
"class MyObj(object):\n", "class MyObj(object):\n",
" count = 5\n", " def __init__(self,count):\n",
" def __init__(self):\n", " self.count= count\n",
" self.count= 9\n",
"\n", "\n",
" def go(self):\n", " def go(self):\n",
" for i in range(self.count):\n", " for i in range(self.count):\n",
" pdb.set_trace()\n", " pdb.set_trace()\n",
" print i\n", " print(i)\n",
" return\n", " return\n",
"\n", "\n",
"if __name__ == '__main__':\n", "if __name__ == '__main__':\n",
...@@ -264,16 +291,55 @@ ...@@ -264,16 +291,55 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### PyCharm\n", "**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",
"\n", "\n",
"Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/example.py" "a=4\n",
"b=\"4\"\n",
"if 8 == b + a:\n",
" print(\"sucess!\")"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## (Interpret errors)" "### PyCharm\n",
"\n",
"Go to: ./examplepydebuggingandtesting/examplepydebuggingandtesting/example.py"
] ]
}, },
{ {
...@@ -293,10 +359,8 @@ ...@@ -293,10 +359,8 @@
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [],
"source": [ "source": [
"## hooks for unittests (or use a continuous-integration server)\n", "## hooks for unittests (or use a continuous-integration server)\n",
"\n", "\n",
...@@ -304,24 +368,20 @@ ...@@ -304,24 +368,20 @@
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": 2,
"metadata": {}, "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": [ "source": [
"### PyCharm\n", "### PyCharm\n",
"\n", "\n",
"use test runner to explore results" "use test runner to explore results"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {
...@@ -340,7 +400,7 @@ ...@@ -340,7 +400,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.7.3" "version": "3.6.7"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
...@@ -11,7 +11,12 @@ ...@@ -11,7 +11,12 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Prints debugging" "## Debugging with the 'print' Statement\n",
"\n",
"- not flexible\n",
"- time consuming\n",
"- very simple tool\n",
"- definitely not best practice"
] ]
}, },
{ {
...@@ -69,7 +74,11 @@ ...@@ -69,7 +74,11 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Breakpoint debugging" "## Debugging with the Python Debugger (pdb)\n",
"\n",
"- more functionality\n",
"- flexible\n",
"- different option to debug files, modules and code in general"
] ]
}, },
{ {
...@@ -100,7 +109,7 @@ ...@@ -100,7 +109,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -108,10 +117,10 @@ ...@@ -108,10 +117,10 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"--Return--\n", "--Return--\n",
"> <ipython-input-8-f97d9006584f>(12)<module>()->None\n", "> <ipython-input-6-4787f25a8269>(12)<module>()->None\n",
"-> pdb.set_trace()\n", "-> pdb.set_trace()\n",
"(Pdb) l\n", "(Pdb) l\n",
" 7 \t return s3\n", " 7 \t return s3\n",
" 8 \t\n", " 8 \t\n",
" 9 \t\n", " 9 \t\n",
" 10 \ta = \"aaa\"\n", " 10 \ta = \"aaa\"\n",
...@@ -121,6 +130,10 @@ ...@@ -121,6 +130,10 @@
" 14 \tfinal = combine(a, b)\n", " 14 \tfinal = combine(a, b)\n",
" 15 \tprint(final)\n", " 15 \tprint(final)\n",
"[EOF]\n", "[EOF]\n",
"(Pdb) p a\n",
"'aaa'\n",
"(Pdb) p c\n",
"'ccc'\n",
"(Pdb) c\n", "(Pdb) c\n",
"\"aaabbbaaa\"\n" "\"aaabbbaaa\"\n"
] ]
...@@ -130,10 +143,10 @@ ...@@ -130,10 +143,10 @@
"import pdb\n", "import pdb\n",
"\n", "\n",
"def combine(s1, s2):\n", "def combine(s1, s2):\n",
" \"\"\"combine arguments and add quotation marks\"\"\"\n", " \"\"\"combine arguments and add quotation marks\"\"\"\n",
" s3 = s1 + s2 + s1\n", " s3 = s1 + s2 + s1\n",
" s3 = '\"' + s3 + '\"'\n", " s3 = '\"' + s3 + '\"'\n",
" return s3\n", " return s3\n",
"\n", "\n",
"\n", "\n",
"a = \"aaa\"\n", "a = \"aaa\"\n",
...@@ -155,86 +168,101 @@ ...@@ -155,86 +168,101 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"A. Running from command line" "**A. Running from Command Line**"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": 10,
"metadata": {}, "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": [ "source": [
"python -m pdb scriptName.py" "python -m pdb \"/home/heinrich/gitrepos/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py\""
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"B. Within the Interpreter" "**B. Within the Interpreter**"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "ModuleNotFoundError", "name": "stdout",
"evalue": "No module named 'pdb_script'", "output_type": "stream",
"text": [
"> <string>(1)<module>()\n",
"(Pdb) n\n",
"TypeError: unsupported operand type(s) for +: 'int' and 'str'\n",
"> <string>(1)<module>()\n",
"(Pdb) ci.num\n",
"5\n",
"(Pdb) c\n"
]
},
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
"output_type": "error", "output_type": "error",
"traceback": [ "traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;31mTypeError\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[0;32m<ipython-input-1-dcdc60e27635>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mscript2debug\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mCalculator\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mpdb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mpdb\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,\"2\")'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pdb_script'" "\u001b[0;32m/usr/lib/python3.6/pdb.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(statement, globals, locals)\u001b[0m\n\u001b[1;32m 1570\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1571\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstatement\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1572\u001b[0;31m \u001b[0mPdb\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstatement\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1573\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1574\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mruneval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mexpression\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.6/bdb.py\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, cmd, globals, locals)\u001b[0m\n\u001b[1;32m 432\u001b[0m \u001b[0msys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msettrace\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrace_dispatch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 433\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 434\u001b[0;31m \u001b[0mexec\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcmd\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mglobals\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlocals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 435\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mBdbQuit\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 436\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<string>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n",
"\u001b[0;32m~/gitrepos/examplepydebuggingandtesting/examplepydebuggingandtesting/script2debug.py\u001b[0m in \u001b[0;36mbetafunc\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 19\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mbetafunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 20\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0margs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 21\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
] ]
} }
], ],
"source": [ "source": [
"import pdb_script\n", "from script2debug import Calculator\n",
"import pdb\n", "import pdb\n",
"pdb.run('pdb_script.MyObj(5).go()')" "pdb.run('ci = Calculator(5); ci.square(); ci.divide(2); ci.betafunc(2,\"2\")')"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": 12,
"metadata": {}, "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": [ "source": [
"C. Within Your Program" "**C. Within Your Program**"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"ename": "SyntaxError", "name": "stdout",
"evalue": "Missing parentheses in call to 'print'. Did you mean print(i)? (<ipython-input-13-16ddb2fede32>, line 15)", "output_type": "stream",
"output_type": "error", "text": [
"traceback": [ "> <ipython-input-4-91e9c298fd93>(14)go()\n",
"\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" "-> print(i)\n",
"(Pdb) p i\n",
"0\n",
"(Pdb) c\n",
"0\n",
"> <ipython-input-4-91e9c298fd93>(13)go()\n",
"-> pdb.set_trace()\n",
"(Pdb) c\n",
"1\n",
"> <ipython-input-4-91e9c298fd93>(14)go()\n",
"-> print(i)\n",
"(Pdb) c\n",
"2\n",
"> <ipython-input-4-91e9c298fd93>(13)go()\n",
"-> pdb.set_trace()\n",
"(Pdb) c\n",
"3\n",
"> <ipython-input-4-91e9c298fd93>(14)go()\n",
"-> print(i)\n",
"(Pdb) c\n",
"4\n"
] ]
} }
], ],
...@@ -246,14 +274,13 @@ ...@@ -246,14 +274,13 @@
"import pdb\n", "import pdb\n",
"\n", "\n",
"class MyObj(object):\n", "class MyObj(object):\n",
" count = 5\n", " def __init__(self,count):\n",
" def __init__(self):\n", " self.count= count\n",
" self.count= 9\n",
"\n", "\n",