0% found this document useful (0 votes)
12 views

Debugging

Module 5 of the Introduction to Python Programming course covers debugging techniques, including raising exceptions, obtaining tracebacks, using assertions, and implementing logging. It explains how to raise exceptions with informative messages, retrieve detailed error information through tracebacks, and use assertions to validate assumptions in code. Additionally, it introduces the logging module for tracking program execution and categorizing log messages by importance.

Uploaded by

sharmamohan2457
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Debugging

Module 5 of the Introduction to Python Programming course covers debugging techniques, including raising exceptions, obtaining tracebacks, using assertions, and implementing logging. It explains how to raise exceptions with informative messages, retrieve detailed error information through tracebacks, and use assertions to validate assumptions in code. Additionally, it introduces the logging module for tracking program execution and categorizing log messages by importance.

Uploaded by

sharmamohan2457
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Introduction to Python Programming (BPLCK105B) Module 5

DEBUGGING

1. Raising Exceptions
 Python raises an exception whenever it tries to execute invalid code.
 Raising an exception is a way of saying, “Stop running the code in this function and
move the program execution to the except statement.”
 Exceptions are raised with a raise statement. In code, a raise statement consists of the
following:
 The raise keyword
 A call to the Exception() function
 A string with a helpful error message passed to the Exception() function
 For example

>>> raise Exception('This is the error message.')


Traceback (most recent call last):
File "<pyshell#191>", line 1, in <module>
raise Exception('This is the error message.')
Exception: This is the error message

 If there are no try and except statements covering the raise statement that raised the
exception, the program simply crashes and displays the exception’s error message..

Dept of CSE, RNSIT


Page 17
Introduction to Python Programming (BPLCK105B) Module 5

def boxPrint(symbol, width, height):


if len(symbol) != 1:
raise Exception('Symbol must be a single character string.')
if width <= 2:
raise Exception('Width must be greater than 2.')
if height <= 2:
raise Exception('Height must be greater than 2.')
print(symbol * width)
for i in range(height - 2):
print(symbol + (' ' * (width - 2)) + symbol)
print(symbol * width)

for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):
try:
boxPrint(sym, w, h)
except Exception as err:
print('An exception happened: ' + str(err))

 This program uses the except Exception as err form of the except statement If an
Exception object is returned from boxPrint()
 This except statement will store it in a variable named err. The Exception object can
then be converted to a string by passing it to str() to produce a userfriendly error
message

Output
****
**
**
****
OOOOOOOOOOOOOOOOOOOO
O O
O O
O O
OOOOOOOOOOOOOOOOOOOO
An exception happened: Width must be greater than 2.
An exception happened: Symbol must be a single character string.

2. Getting the Traceback as a String


 When Python encounters an error, it produces a treasure trove of error
information called the traceback.

Dept of CSE, RNSIT


Page 18
Introduction to Python Programming (BPLCK105B) Module 5

 The traceback includes the error message, the line number of the line that
caused the error, and the sequence of the function calls that led to the error.

def spam():
bacon()
def bacon():
raise Exception('This is the error message.')
spam()

Output:

Traceback (most recent call last):


File "errorExample.py", line 7, in <module>
spam()
File "errorExample.py", line 2, in spam
bacon()
File "errorExample.py", line 5, in bacon
raise Exception('This is the error message.')
Exception: This is the error message.

 The traceback is displayed by Python whenever a raised exception goes unhandled


 But you can also obtain it as a string by calling traceback.format_exc(). This function
is useful if you want the information from an exception’s traceback but also want an
except statement to gracefully handle the exception.
 For example,

Dept of CSE, RNSIT


Page 19
Introduction to Python Programming (BPLCK105B) Module 5

>>> import traceback


>>> try:
raise Exception('This is the error message.')
except:
errorFile = open('errorInfo.txt', 'w')
errorFile.write(traceback.format_exc())
errorFile.close()
print('The traceback info was written to errorInfo.txt.')
116
The traceback info was written to errorInfo.txt.

 The 116 is the return value from the write() method, since 116 characters were written
to the file.

Traceback (most recent call last):


File "<pyshell#28>", line 2, in <module>
Exception: This is the error message.
3. Assertions
 An assertion is a sanity check to make sure your code isn’t doing something
obviously wrong.
 These sanity checks are performed by assert statements. If the sanity check fails, then
an AssertionError exception is raised.
 assert statement consists of the following:
 The assert keyword
 A condition (that is, an expression that evaluates to True or False)
 A comma
 A string to display when the condition is False
 For example,

Dept of CSE, RNSIT


Page 20
Introduction to Python Programming (BPLCK105B) Module 5

>>> podBayDoorStatus = 'open'


>>> assert podBayDoorStatus == 'open', 'The pod bay doors need to be "open".'
>>> podBayDoorStatus = 'I\'m sorry, Dave. I\'m afraid I can't do that.''
>>> assert podBayDoorStatus == 'open', 'The pod bay doors need to be "open".'
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
assert podBayDoorStatus == 'open', 'The pod bay doors need to be "open".'
AssertionError: The pod bay doors need to be "open".

 set podBayDoorStatus to 'open', so from now on, we fully expect the value of this
variable to be 'open'
 In a program that uses this variable, we might have written a lot of code under the
assumption that the value is 'open'—code that depends on its being 'open' in order to
work as we expect. So we add an assertion to make sure we’re right to assume
podBayDoorStatus is 'open'
 Here, we include the message 'The pod bay doors need to be "open".' so it’ll be easy
to see what’s wrong if the assertion fails.

Using an Assertion in a Traffic Light Simulation


 The data structure representing the stoplights at an intersection is a dictionary with
keys 'ns' and 'ew', for the stoplights facing north-south and east-west, respectively.
 The values at these keys will be one of the strings 'green', 'yellow', or 'red'. The code
would look something like this:

market_2nd = {'ns': 'green', 'ew': 'red'}


mission_16th = {'ns': 'red', 'ew': 'green'}
 To start the project, you want to write a switchLights() function, which will take an
intersection dictionary as an argument and switch the lights.

Dept of CSE, RNSIT


Page 21
Introduction to Python Programming (BPLCK105B) Module 5

 At first, you might think that switchLights() should simply switch each light to the
next color in the sequence: Any 'green' values should change to 'yellow', 'yellow'
values should change to 'red', and 'red' values should change to 'green'.
Program:
def switchLights(stoplight):
for key in stoplight.keys():
if stoplight[key] == 'green':
stoplight[key] = 'yellow'
elif stoplight[key] == 'yellow':
stoplight[key] = 'red'
elif stoplight[key] == 'red':
stoplight[key] = 'green'
switchLights(market_2nd)

 while writing switchLights() you had added an assertion to check that at least one of
the lights is always red,
 include the following at the bottom of the function:

assert 'red' in stoplight.values(), 'Neither light is red! ' + str(stoplight)


 With this assertion in place, your program would crash with this error message:

Traceback (most recent call last):


File "carSim.py", line 14, in <module>
switchLights(market_2nd)
File "carSim.py", line 13, in switchLights
assert 'red' in stoplight.values(), 'Neither light is red! ' + str(stoplight)
u AssertionError: Neither light is red! {'ns': 'yellow', 'ew': 'green'}
 The important line here is the AssertionError
 Neither direction of traffic has a red light, meaning that traffic could be going both

Dept of CSE, RNSIT


Page 22
Introduction to Python Programming (BPLCK105B) Module 5
ways. By failing fast early in the program’s execution

4. Logging
 Logging is a great way to understand what’s happening in your program and
in what order its happening.
 Python’s logging module makes it easy to create a record of custom messages
that you write.
 These log messages will describe when the program execution has reached the
logging function call and list any variables you have specified at that point in
time.
 On the other hand, a missing log message indicates a part of the code was
skipped and never executed.
Using the logging Module
 To enable the logging module to display log messages on your screen as your
program runs,

import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s -
%(levelname)s
- %(message)s')
 when Python logs an event, it creates a LogRecord object that holds
information about that event
 The logging module’s basicConfig() function lets you specify what details
about the LogRecord object you want to see and how you want those details
displayed

Dept of CSE, RNSIT


Page 23
Introduction to Python Programming (BPLCK105B) Module 5

Program:

import logging
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s
- %(message)s')
logging.debug('Start of program')
def factorial(n):
logging.debug('Start of factorial(%s%%)' % (n))
total = 1
for i in range(n + 1):
total *= i
logging.debug('i is ' + str(i) + ', total is ' + str(total))
logging.debug('End of factorial(%s%%)' % (n))
return total
print(factorial(5))
logging.debug('End of program')

 debug() function will call basicConfig(), and a line of information will be printed.
 This information will be in the format we specified in basicConfig() and will include
the messages we passed to debug().
Output:

2015-05-23 16:20:12,664 - DEBUG - Start of program


2015-05-23 16:20:12,664 - DEBUG - Start of factorial(5)
2015-05-23 16:20:12,665 - DEBUG - i is 0, total is 0
2015-05-23 16:20:12,668 - DEBUG - i is 1, total is 0
2015-05-23 16:20:12,670 - DEBUG - i is 2, total is 0
2015-05-23 16:20:12,673 - DEBUG - i is 3, total is 0
2015-05-23 16:20:12,675 - DEBUG - i is 4, total is 0
2015-05-23 16:20:12,678 - DEBUG - i is 5, total is 0
2015-05-23 16:20:12,680 - DEBUG - End of factorial(5)
0
2015-05-23 16:20:12,684 - DEBUG - End of program

Dept of CSE, RNSIT


Page 24
Introduction to Python Programming (BPLCK105B) Module 5
 The factorial() function is returning 0 as the factorial of 5, which isn’t right.

 The for loop should be multiplying the value in total by the numbers from 1 to 5. But
the log messages displayed by logging.debug() show that the i variable is starting at 0
instead of 1.
 Since zero times anything is zero, the rest of the iterations also have the wrong value
for total
 Logging messages provide a trail of breadcrumbs that can help you figure out when
things started to go wrong.
 Change the for i in range(n + 1): line to for i in range(1, n + 1):, and run the program
again

Output

2015-05-23 17:13:40,650 - DEBUG - Start of program


2015-05-23 17:13:40,651 - DEBUG - Start of factorial(5)
2015-05-23 17:13:40,651 - DEBUG - i is 1, total is 1
2015-05-23 17:13:40,654 - DEBUG - i is 2, total is 2
2015-05-23 17:13:40,656 - DEBUG - i is 3, total is 6
2015-05-23 17:13:40,659 - DEBUG - i is 4, total is 24
2015-05-23 17:13:40,661 - DEBUG - i is 5, total is 120
2015-05-23 17:13:40,661 - DEBUG - End of factorial(5)
120
2015-05-23 17:13:40,666 - DEBUG - End of program

Logging Levels
 Logging levels provide a way to categorize your log messages by importance. There
are five logging levels
 Messages can be logged at each level using a different logging function.

Dept of CSE, RNSIT


Page 25
Introduction to Python Programming (BPLCK105B) Module 5

Level Logging Function Description


DEBUG logging.debug() The lowest level. Used for small details.
Usually you care about these messages
only when diagnosing problems.

INFO logging.info() Used to record information on general


events in your program or confirm that
things are working at their point in the
program.

WARNING l logging.warning() Used to indicate a potential problem that


doesn’t prevent the program from
working but might do so in the future.

ERROR logging.error() Used to record an error that caused the


program to fail to do something.
CRITICAL logging.critical() The highest level. Used to indicate a fatal
error that has caused or is about to cause
the program to stop running entirely.

Table 10-1: Logging Levels in Python

>>> import logging


>>> logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s -
%(levelname)s - %(message)s')
>>> logging.debug('Some debugging details.')
2015-05-18 19:04:26,901 - DEBUG - Some debugging details.
>>> logging.info('The logging module is working.')
2015-05-18 19:04:35,569 - INFO - The logging module is working.
>>> logging.warning('An error message is about to be logged.')
2015-05-18 19:04:56,843 - WARNING - An error message is about to be
logged.
>>> logging.error('An error has occurred.')
2015-05-18 19:05:07,737 - ERROR - An error has occurred.
>>> logging.critical('The program is unable to recover!')
2015-05-18 19:05:45,794 - CRITICAL - The program is unable to recover!

Page 26
Introduction to Python Programming (BPLCK105B) Module 5

Disabling Logging

 The logging.disable() function disables these so that you don’t have to go into your
program and remove all the logging calls by hand.
 pass logging.disable() a logging level, and it will suppress all log messages at that
level or lower

>>> import logging

>>> logging.basicConfig(level=logging.INFO, format=' %(asctime)s -%(levelname)s -


%(message)s')

>>> logging.critical('Critical error! Critical error!')


2015-05-22 11:10:48,054 - CRITICAL - Critical error! Critical error!
>>> logging.disable(logging.CRITICAL)
>>> logging.critical('Critical error! Critical error!')
>>> logging.error('Error! Error!')

 Since logging.disable() will disable all messages after it, you will probably want to
add it near the import logging line of code in your program

Logging to a File
 Instead of displaying the log messages to the screen, you can write them to a text file.
The logging.basicConfig() function takes a filename keyword argument,
import logging
logging.basicConfig(filename='myProgramLog.txt',level=logging.DEBUG,
format=' %(asctime)s - %(levelname)s - %(message)s')

Page 27
Introduction to Python Programming (BPLCK105B) Module 5

4. IDLE ’s Debugger
 The debugger is a feature of IDLE that allows you to execute your program one line
at a time.
 The debugger will run a single line of code and then wait for you to tell it to continue
 To enable IDLE’s debugger, click Debug4Debugger in the interactive shell window.
 When the Debug Control window appears, select all four of the Stack, Locals, Source,
and Globals checkboxes so that the window shows the full set of debug information
 While the Debug Control window is displayed, any time you run a program from the
file editor
 debugger will pause execution before the first instruction and display the following:
 The line of code that is about to be executed
 A list of all local variables and their values
 A list of all global variables and their values

Page 28
Introduction to Python Programming (BPLCK105B) Module 5

Figure: The Debug Control window

 You’ll notice that in the list of global variables there are several variables you haven’t
defined, such as builtins , doc , file , and so on. These are variables that
Python automatically sets whenever it runs a program.
 The program will stay paused until you press one of the five buttons in the Debug
Control window: Go, Step, Over, Out, or Quit.
Go
 Clicking the Go button will cause the program to execute normally until it terminates
or reaches a breakpoint
 If you are done debugging and want the program to continue normally, click the Go
button.
Step
 Clicking the Step button will cause the debugger to execute the next line of code and
then pause again
 The Debug Control window’s list of global and local variables will be updated if their
values change.
 If the next line of code is a function call, the debugger will “step into” that function
and jump to the first line of code of that function.

Page 29
Introduction to Python Programming (BPLCK105B) Module 5

Over
 Clicking the Over button will execute the next line of code, similar to the Step button.
 The Over button will “step over” the code in the function. The function’s code will be
executed at full speed, and the debugger will pause as soon as the function call
returns.
 For example, if the next line of code is a print() call, you don’t really care about code
inside the built-in print() function; you just want the string you pass it printed to the
screen.
Quit
 If you want to stop debugging entirely and not bother to continue executing the rest of
the program, click the Quit button
 The Quit button will immediately terminate the program. If you want to run your
program normally again, select Debug4Debugger again to disable the debugger.

Debugging a Number Adding Program

print('Enter the first number to add:')


first = input()
print('Enter the second number to add:')
second = input()
print('Enter the third number to add:')
third = input()
print('The sum is ' + first + second + third)

Save it as buggyAddingProgram.py and run it first without the debugger enabled.

Enter the first number to add:


5
Enter the second number to add:
3
Enter the third number to add:
42
The sum is 5342

Page 30
Introduction to Python Programming (BPLCK105B) Module 5

 The program hasn’t crashed, but the sum is obviously wrong. Let’s enable the Debug
Control window and run it again, this time under the debugger
 When you press F5 or select Run4Run Module (with Debug4Debugger enabled and
all four checkboxes on the Debug Control window checked), the program starts in a
paused state on line 1.
 The debugger will always pause on the line of code it is about to execute.

Figure The Debug Control window when the program first starts under the debugger

Page 31
Introduction to Python Programming (BPLCK105B) Module 5

 Click the Over button once to execute the first print() call. You should use Over
instead of Step here, since you don’t want to step into the code for the print() function.
 The Debug Control window will update to line 2, and line 2 in the file editor window
will be highlighted.

Figure: The Debug Control window after clicking Over

Page 32
Introduction to Python Programming (BPLCK105B) Module 5

 Click Over again to execute the input() function call, and the buttons in the Debug
Control window will disable themselves while IDLE waits for you to type something
for the input() call into the interactive shell window.
 Enter 5 and press Return. The Debug Control window buttons will be reenabled.
 Keep clicking Over, entering 3 and 42 as the next two numbers, until the debugger is
on line 7, the final print() call in the program
 Globals section that the first, second, and third variables are set to string values '5', '3',
and '42' instead of integer values 5, 3, and 42.
 When the last line is executed, these strings are concatenated instead of added
together, causing the bug.

Figure The Debug Control window on the last line. The variables are set to strings, causing
the bug.

Page 33
Introduction to Python Programming (BPLCK105B) Module 5

Breakpoints

 A breakpoint can be set on a specific line of code and forces the debugger to mpause
whenever the program execution reaches that line.
 Open a new file editor window and enter the following program, which simulates flipping a
coin 1,000 times.

import random
heads = 0
for i in range(1, 1001):
if random.randint(0, 1) == 1:
heads = heads + 1
if i == 500:
print('Halfway done!')
print('Heads came up ' + str(heads) + ' times.')

 The random.randint(0, 1) call u will return 0 half of the time and 1 the other half of
the time.
 This can be used to simulate a 50/50 coin flip where 1 represents heads.

Output:

Halfway done!
Heads came up 490 times.
 If you ran this program under the debugger, you would have to click the Over button
thousands of times before the program terminated.
 If you were interested in the value of heads at the halfway point of the program’s
execution, when 500 of 1000 coin flips have been completed, you could instead just
set a breakpoint on the line print('Halfway done!')
 To set a breakpoint, right-click the line in the file editor and select Set Breakpoint,

Page 34
Introduction to Python Programming (BPLCK105B) Module 5

Figure : Setting a breakpoint

Jupyter Notebook: Web-Based IDE

Jupyter Notebook is an open-source, web-based Integrated Development Environment (IDE)


primarily used for data science, machine learning, and scientific computing. It allows users to create
and share documents that contain live code, equations, visualizations, and narrative text. It supports
many programming languages, but it is most commonly used with Python.

Key Features:

1. Interactive Coding:
o Allows users to write and execute code in an interactive manner, with immediate
feedback.
2. Multilingual Support:
o While it is commonly used with Python, Jupyter supports other languages like R, Julia,
and more via kernels.
3. Rich Text Support:
o Users can include Markdown for rich text formatting, LaTeX for mathematical
equations, and HTML for custom visualizations.

Page 35
Introduction to Python Programming (BPLCK105B) Module 5

4. Data Visualization:
o Direct integration with libraries like Matplotlib, Seaborn, and Plotly for easy data
visualization.
5. Export Options:
o Notebooks can be exported to various formats like HTML, PDF, or slides for sharing
or presentation purposes.
6. Interactive Widgets:
o Enables dynamic visualizations and the use of interactive widgets to enhance user
engagement.
7. Integrated Debugging:
o Tools for debugging and profiling code are integrated, aiding in performance
optimization and error handling.
8. Support for Reproducibility:
o Ensures that the code, outputs, and data are captured together, enhancing
reproducibility and transparency.
9. Extensible with Plugins:
o A wide range of extensions and plugins can be added for enhanced functionality, such
as version control and collaboration tools.

Applications:

 Data analysis, machine learning, scientific research, and academic teaching.

Jupyter Notebooks are widely used in academia, research, and industries dealing with large-scale
data analysis.

Page 36
Introduction to Python Programming (BPLCK105B) Module 5

Page 37
Introduction to Python Programming (BPLCK105B) Module 5

NumPy

NumPy (Numerical Python) is a library used for numerical and mathematical computations in
Python. It provides support for working with arrays, matrices, and a collection of mathematical
functions to operate on these structures.

Key Features:

1. ndarray: Powerful N-dimensional array object for handling large datasets.


2. Fast Performance: Optimized for performance, much faster than Python lists.
3. Broadcasting: Enables element-wise operations on arrays of different shapes.
Page 38
Introduction to Python Programming (BPLCK105B) Module 5

4. Mathematical Functions: Built-in functions like mean, median, standard deviation, linear
algebra operations, and more.
5. Indexing and Slicing: Allows slicing of arrays using rich indexing.
6. Integration: Easily integrates with other Python libraries like SciPy, Pandas, and Matplotlib.

Common Functions:

 Array creation:
import numpy as np
arr = np.array([1, 2, 3]) # 1D array
zeros = np.zeros((2, 3)) # 2D array of zeros
ones = np.ones((2, 3)) # 2D array of ones

 Array operations:
arr + 2 # Adds 2 to all elements
arr1 * arr2 # Element-wise multiplication
np.dot(a, b) # Matrix multiplication

 Aggregations:
np.mean(arr), np.sum(arr), np.std(arr)

Pandas

Pandas is a data manipulation and analysis library built on top of NumPy. It provides tools for
working with structured data, like tables (dataframes).

Key Features:

 DataFrames: 2D labeled data structures similar to Excel tables.


 Series: 1D labeled array-like objects.
 Handling Missing Data: Functions for detecting, filling, and dropping missing values.
 Data Manipulation: Filtering, grouping, merging, and reshaping data.
 File I/O: Read/write support for CSV, Excel, JSON, SQL, etc.
 Indexing and Slicing: Advanced indexing and selection for both rows and columns.

Common Functions:

 Data creation:
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)

Page 39
Introduction to Python Programming (BPLCK105B) Module 5

 File I/O:
df = pd.read_csv('file.csv') # Reading CSV
df.to_excel('file.xlsx') # Writing to Excel

 Basic operations:
df.head() # First 5 rows
df.describe() # Summary statistics
df['Age'] # Access column
df.iloc[0, 1] # Access element by index

 Data cleaning:
df.dropna() # Drop rows with missing values
df.fillna(0) # Fill missing values with 0

 Grouping and Aggregation:


df.groupby('Name').sum()

Quick Comparison:

Feature NumPy Pandas


Data Type ndarray DataFrame, Series
Purpose Numerical computation Data analysis & manipulation
File Handling Limited Extensive (CSV, Excel, SQL)
Ease of Use Complex for large datasets Intuitive for tabular data

If you’re working with structured/tabular data, Pandas is better suited. For numerical operations or
working with large datasets, NumPy is the preferred choice.

Page 40

You might also like