Chapter 18: Debugging Programs

Program 18.1, Page number: 389

In [1]:
import logging

def process(i,j,k):
        return i+j+k

def main():

        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)

        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

        fh = logging.FileHandler('18.1_logFile.txt')
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)
        logger.addHandler(ch)

        arr=[]
        arr=map(int,"1 2 3".split())
        #arr=map(int,raw_input().split())
        logger.debug("Number of integers read= {0}".format(len(arr)))               #Debug statement
        logger.debug("i = {0}, j = {1}, k = {2}\n".format(arr[0],arr[1],arr[2]))    #Debug statement

        print("sum= %i\n"%process(arr[0],arr[1],arr[2]))


if __name__=='__main__':
        main()
2013-08-27 01:20:39,300 - DEBUG - Number of integers read= 3
2013-08-27 01:20:39,303 - DEBUG - i = 1, j = 2, k = 3

sum= 6

Program 18.2, Page number: 391

In [1]:
import logging,sys

def process(i1,i2):

        global logger 
        logger.debug("process (%i, %i)\n"%(i1, i2))
        val=i1*i2
        logger.debug("return %i\n"%(val))
        return val


def main():

        global logger
        logging.basicConfig(filename='18.2_logFile.txt', filemode='w', level=logging.DEBUG)
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)
        filemode = 'w'
        
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

        fh = logging.FileHandler('18.2_logFile.txt')
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)
        logger.addHandler(ch)


        arg1=0
        arg2=0
        #Un-Comment this while executing from terminal/command line
        #if(len(sys.argv)>1):
                #arg1=int(sys.argv[1])
        #if(len(sys.argv)==3):
                #arg2=int(sys.argv[2])

        #Dummy command-line-parameters
        #execute as "python <filename>.py arg1 arg2" while executing from terminal and un/comment respective statements
        a=['3','5']                                      
        logger.debug("processed %i arguments\n"%(len(a)))
        #logger.debug("processed %i arguments\n"%(len(sys.argv)))

        logger.debug("arg1 = %i, arg2 = %i\n"%(int(a[0]), int(a[1])))
        print("product = %i\n" %( process (int(a[0]),int( a[1]))))

        #logger.debug("arg1 = %i, arg2 = %i\n"%(arg1, arg2))
        #print("product = %i\n" %( process (arg1, arg2)))


if __name__=='__main__':
        main()
2013-08-27 17:50:53,392 - DEBUG - processed 2 arguments

2013-08-27 17:50:53,397 - DEBUG - arg1 = 3, arg2 = 5

2013-08-27 17:50:53,404 - DEBUG - process (3, 5)

2013-08-27 17:50:53,409 - DEBUG - return 15

product = 15

Program 18.3, Page number: 393

In [1]:
import logging,sys

def process(i1,i2):

        global logger 
        logger.warning("process (%i, %i)\n"%(i1, i2))
        val=i1*i2
        logger.error("return %i\n"%(val))
        return val


def main():

        global logger
        logging.basicConfig(filename='18.3_logFile.txt', filemode='w', level=logging.DEBUG)
        logger = logging.getLogger()
        logger.setLevel(logging.DEBUG)

        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')

        fh = logging.FileHandler('18.3_logFile.txt')
        fh.setLevel(logging.DEBUG)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        ch.setFormatter(formatter)
        logger.addHandler(ch)


        arg1=0
        arg2=0
        #Un-Comment this while executing from terminal/command line

        #if(len(sys.argv)>1):
                #arg1=int(sys.argv[1])
        #if(len(sys.argv)==3):
                #arg2=int(sys.argv[2])

        #Dummy command-line-parameters
        #execute as "python <filename>.py arg1 arg2" while executing from terminal and un/comment respective statements
        a=['3','5']
        logger.debug("processed %i arguments\n"%(len(a)))
        #logger.debug("processed %i arguments\n"%(len(sys.argv)))

        logger.info("arg1 = %i, arg2 = %i\n"%(int(a[0]), int(a[1])))
        print("product = %i\n" %( process (int(a[0]),int( a[1]))))

        #logger.debug("arg1 = %i, arg2 = %i\n"%(arg1, arg2))
        #print("product = %i\n" %( process (arg1, arg2)))


if __name__=='__main__':
        main()
2013-08-27 17:54:41,136 - DEBUG - processed 2 arguments

2013-08-27 17:54:41,142 - INFO - arg1 = 3, arg2 = 5

2013-08-27 17:54:41,149 - WARNING - process (3, 5)

2013-08-27 17:54:41,154 - ERROR - return 15

product = 15

Program 18.4, Page number: 396

In [4]:
def main():
        
        #Variable Declaration
        sum=0
        data = [1, 2, 3, 4, 5]

        #Calculation
        for i in range (0,5):
                sum += data[i]
        
        #Result
        print("sum = %i\n"%(sum))
        

if __name__=='__main__':
        main()
sum = 15

Program 18.5, Page number: 401

In [5]:
class date:
        def __init__(self,m,d,y):               #Class constructor
                self.month=m
                self.day=d
                self.year=y

        def foo(x):
                x.day+=1
                return x

def main():

        #Variable Declaration
        today=date(10,11,2004)
        array=[1,2,3,4,5]
        string="test string"
        i=3
        newdate=date(11,15,2004)
        today=date.foo(today)

        #Result
        print("today= %d/%d/%d"%(today.day,today.month,today.year))


if __name__=='__main__':
        main()
today= 12/10/2004