Debugging
Debugging
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
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..
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.
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:
The 116 is the return value from the write() method, since 116 characters were written
to the file.
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.
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:
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
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:
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
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.
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
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
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.
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.
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
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:
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:
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:
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
Quick Comparison:
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