summaryrefslogtreecommitdiff
path: root/Programming_in_C/Chapter_18.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Programming_in_C/Chapter_18.ipynb')
-rw-r--r--Programming_in_C/Chapter_18.ipynb472
1 files changed, 472 insertions, 0 deletions
diff --git a/Programming_in_C/Chapter_18.ipynb b/Programming_in_C/Chapter_18.ipynb
new file mode 100644
index 00000000..675ba691
--- /dev/null
+++ b/Programming_in_C/Chapter_18.ipynb
@@ -0,0 +1,472 @@
+{
+ "metadata": {
+ "name": "Chapter 18"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "heading",
+ "level": 1,
+ "metadata": {},
+ "source": [
+ "Chapter 18: Debugging Programs"
+ ]
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 18.1, Page number: 389"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#18.1.py\n",
+ "#Adding Debug statements in program\n",
+ "\n",
+ "#Program logs will be saved in file- 18.1_logFile.txt\n",
+ "\n",
+ "#Import library\n",
+ "import logging\n",
+ "\n",
+ "#Function to return sum of 3 numbers\n",
+ "def process(i,j,k):\n",
+ " return i+j+k\n",
+ "\n",
+ "#Main()\n",
+ "def main():\n",
+ "\n",
+ " logger = logging.getLogger()\n",
+ " logger.setLevel(logging.DEBUG)\n",
+ "\n",
+ " formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\n",
+ "\n",
+ " fh = logging.FileHandler('18.1_logFile.txt')\n",
+ " fh.setLevel(logging.DEBUG)\n",
+ " fh.setFormatter(formatter)\n",
+ " logger.addHandler(fh)\n",
+ "\n",
+ " ch = logging.StreamHandler()\n",
+ " ch.setLevel(logging.DEBUG)\n",
+ " ch.setFormatter(formatter)\n",
+ " logger.addHandler(ch)\n",
+ "\n",
+ " arr=[]\n",
+ " arr=map(int,\"1 2 3\".split())\n",
+ " #arr=map(int,raw_input().split())\n",
+ " logger.debug(\"Number of integers read= {0}\".format(len(arr))) #Debug statement\n",
+ " logger.debug(\"i = {0}, j = {1}, k = {2}\\n\".format(arr[0],arr[1],arr[2])) #Debug statement\n",
+ "\n",
+ " print(\"sum= %i\\n\"%process(arr[0],arr[1],arr[2]))\n",
+ "\n",
+ "\n",
+ "#Setting top level conditional script\n",
+ "if __name__=='__main__':\n",
+ " main()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 01:20:39,300 - DEBUG - Number of integers read= 3\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 01:20:39,303 - DEBUG - i = 1, j = 2, k = 3\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "sum= 6\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 18.2, Page number: 391"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#18.2.py\n",
+ "#Compiling in Debug \n",
+ "\n",
+ "#Program logs will be saved in file- 18.2_logFile.txt\n",
+ "\n",
+ "#Import Libraries\n",
+ "import logging,sys\n",
+ "\n",
+ "#Function to return product of two numbers\n",
+ "def process(i1,i2):\n",
+ "\n",
+ " global logger \n",
+ " logger.debug(\"process (%i, %i)\\n\"%(i1, i2))\n",
+ " val=i1*i2\n",
+ " logger.debug(\"return %i\\n\"%(val))\n",
+ " return val\n",
+ "\n",
+ "\n",
+ "#Main()\n",
+ "def main():\n",
+ "\n",
+ " global logger\n",
+ " logging.basicConfig(filename='18.2_logFile.txt', filemode='w', level=logging.DEBUG)\n",
+ " logger = logging.getLogger()\n",
+ " logger.setLevel(logging.DEBUG)\n",
+ " filemode = 'w'\n",
+ " \n",
+ " formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\n",
+ "\n",
+ " fh = logging.FileHandler('18.2_logFile.txt')\n",
+ " fh.setLevel(logging.DEBUG)\n",
+ " fh.setFormatter(formatter)\n",
+ " logger.addHandler(fh)\n",
+ "\n",
+ " ch = logging.StreamHandler()\n",
+ " ch.setLevel(logging.DEBUG)\n",
+ " ch.setFormatter(formatter)\n",
+ " logger.addHandler(ch)\n",
+ "\n",
+ "\n",
+ " arg1=0\n",
+ " arg2=0\n",
+ " #Un-Comment this while executing from terminal/command line\n",
+ " #if(len(sys.argv)>1):\n",
+ " #arg1=int(sys.argv[1])\n",
+ " #if(len(sys.argv)==3):\n",
+ " #arg2=int(sys.argv[2])\n",
+ "\n",
+ " #Dummy command-line-parameters\n",
+ " #execute as \"python <filename>.py arg1 arg2\" while executing from terminal and un/comment respective statements\n",
+ " a=['3','5'] \n",
+ " logger.debug(\"processed %i arguments\\n\"%(len(a)))\n",
+ " #logger.debug(\"processed %i arguments\\n\"%(len(sys.argv)))\n",
+ "\n",
+ " logger.debug(\"arg1 = %i, arg2 = %i\\n\"%(int(a[0]), int(a[1])))\n",
+ " print(\"product = %i\\n\" %( process (int(a[0]),int( a[1]))))\n",
+ "\n",
+ " #logger.debug(\"arg1 = %i, arg2 = %i\\n\"%(arg1, arg2))\n",
+ " #print(\"product = %i\\n\" %( process (arg1, arg2)))\n",
+ "\n",
+ "\n",
+ "#Setting top level conditional script\n",
+ "if __name__=='__main__':\n",
+ " main()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 17:50:53,392 - DEBUG - processed 2 arguments\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 17:50:53,397 - DEBUG - arg1 = 3, arg2 = 5\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 17:50:53,404 - DEBUG - process (3, 5)\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 17:50:53,409 - DEBUG - return 15\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "product = 15\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 18.3, Page number: 393"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#18.3.py\n",
+ "#Compiling in Debug--version2\n",
+ "\n",
+ "#Program logs will be saved in file- 18.3_logFile.txt\n",
+ "\n",
+ "#Using different logging levels\n",
+ "#DEBUG Detailed information, typically of interest only when diagnosing problems.\n",
+ "#INFO Confirmation that things are working as expected.\n",
+ "#WARNING An indication of some problem in the near future (e.g.'disk space low')\n",
+ "#ERROR Due to a more serious problem, the software has not been able to perform some function.\n",
+ "#CRITICAL A serious error, indicating that the program itself may be unable to continue running.\n",
+ "\n",
+ "\n",
+ "#Import Libraries\n",
+ "import logging,sys\n",
+ "\n",
+ "#Function to return product of two numbers\n",
+ "def process(i1,i2):\n",
+ "\n",
+ " global logger \n",
+ " logger.warning(\"process (%i, %i)\\n\"%(i1, i2))\n",
+ " val=i1*i2\n",
+ " logger.error(\"return %i\\n\"%(val))\n",
+ " return val\n",
+ "\n",
+ "\n",
+ "#Main()\n",
+ "def main():\n",
+ "\n",
+ " global logger\n",
+ " logging.basicConfig(filename='18.3_logFile.txt', filemode='w', level=logging.DEBUG)\n",
+ " logger = logging.getLogger()\n",
+ " logger.setLevel(logging.DEBUG)\n",
+ "\n",
+ " formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')\n",
+ "\n",
+ " fh = logging.FileHandler('18.3_logFile.txt')\n",
+ " fh.setLevel(logging.DEBUG)\n",
+ " fh.setFormatter(formatter)\n",
+ " logger.addHandler(fh)\n",
+ "\n",
+ " ch = logging.StreamHandler()\n",
+ " ch.setLevel(logging.DEBUG)\n",
+ " ch.setFormatter(formatter)\n",
+ " logger.addHandler(ch)\n",
+ "\n",
+ "\n",
+ " arg1=0\n",
+ " arg2=0\n",
+ " #Un-Comment this while executing from terminal/command line\n",
+ "\n",
+ " #if(len(sys.argv)>1):\n",
+ " #arg1=int(sys.argv[1])\n",
+ " #if(len(sys.argv)==3):\n",
+ " #arg2=int(sys.argv[2])\n",
+ "\n",
+ " #Dummy command-line-parameters\n",
+ " #execute as \"python <filename>.py arg1 arg2\" while executing from terminal and un/comment respective statements\n",
+ " a=['3','5']\n",
+ " logger.debug(\"processed %i arguments\\n\"%(len(a)))\n",
+ " #logger.debug(\"processed %i arguments\\n\"%(len(sys.argv)))\n",
+ "\n",
+ " logger.info(\"arg1 = %i, arg2 = %i\\n\"%(int(a[0]), int(a[1])))\n",
+ " print(\"product = %i\\n\" %( process (int(a[0]),int( a[1]))))\n",
+ "\n",
+ " #logger.debug(\"arg1 = %i, arg2 = %i\\n\"%(arg1, arg2))\n",
+ " #print(\"product = %i\\n\" %( process (arg1, arg2)))\n",
+ "\n",
+ "\n",
+ "#Setting top level conditional script\n",
+ "if __name__=='__main__':\n",
+ " main()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 17:54:41,136 - DEBUG - processed 2 arguments\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 17:54:41,142 - INFO - arg1 = 3, arg2 = 5\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 17:54:41,149 - WARNING - process (3, 5)\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stderr",
+ "text": [
+ "2013-08-27 17:54:41,154 - ERROR - return 15\n",
+ "\n"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "product = 15\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 1
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 18.4, Page number: 396"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#18.4.py\n",
+ "#A simple program for use with pdb(gdb like python debugger)\n",
+ "\n",
+ "#Main()\n",
+ "def main():\n",
+ " \n",
+ " #Variable Declaration\n",
+ " sum=0\n",
+ " data = [1, 2, 3, 4, 5]\n",
+ "\n",
+ " #Calculation\n",
+ " for i in range (0,5):\n",
+ " sum += data[i]\n",
+ " \n",
+ " #Result\n",
+ " print(\"sum = %i\\n\"%(sum))\n",
+ " \n",
+ "\n",
+ "#Setting top level conditional script\n",
+ "if __name__=='__main__':\n",
+ " main()\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "sum = 15\n",
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 4
+ },
+ {
+ "cell_type": "heading",
+ "level": 3,
+ "metadata": {},
+ "source": [
+ "Program 18.5, Page number: 401"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "#18.5.py\n",
+ "#Working more with pdb\n",
+ "\n",
+ "#Class Declaration\n",
+ "class date:\n",
+ " def __init__(self,m,d,y): #Class constructor\n",
+ " self.month=m\n",
+ " self.day=d\n",
+ " self.year=y\n",
+ "\n",
+ " def foo(x):\n",
+ " x.day+=1\n",
+ " return x\n",
+ "\n",
+ "#Main()\n",
+ "def main():\n",
+ "\n",
+ " #Variable Declaration\n",
+ " today=date(10,11,2004)\n",
+ " array=[1,2,3,4,5]\n",
+ " string=\"test string\"\n",
+ " i=3\n",
+ " newdate=date(11,15,2004)\n",
+ " today=date.foo(today)\n",
+ "\n",
+ " #Result\n",
+ " print(\"today= %d/%d/%d\"%(today.day,today.month,today.year))\n",
+ "\n",
+ "\n",
+ "#Setting top level conditional script\n",
+ "if __name__=='__main__':\n",
+ " main()\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "today= 12/10/2004\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+} \ No newline at end of file