lec_file_debugging
lec_file_debugging
Prof. Kim
1 2
Storage
• Memory: Temporary, short-term storage • https://websites.umich.edu/~umfandsf/other/ebo
– Once you close the program, it’s gone oks/alice30.txt
– Once you turn off the computer, it’s gone
3 4
import sys, os
Some questions about reading from files
#setup file path/directory: to where the .py file is
__location__ = os.path.realpath(os.path.join(os.getcwd(),
os.path.dirname(__file__))) • What happens if the file does not exist?
It is telling the computer
that you are going to READ
def readFile(fileName):
from the file
#setup file to open • How do you process the data once read?
file = open(os.path.join(__location__,fileName), 'r')
– It is stored as an array of strings (line by line)
#read data as an array of strings → character matrix – It can be treated as a matrix of characters
data = file.readlines()
print(data)
5 6
1
import sys, os
Exercise
#setup file path/directory: to where the .py file is
__location__ = os.path.realpath(os.path.join(os.getcwd(),
os.path.dirname(__file__))) • Write a function that counts the occurrence of certain
It is telling the computer characters in the text file
that you are going to READ
def readFile2(fileName):
#setup file to open
from the file def countLetter(data, myChar):
file = open(os.path.join(__location__,fileName), 'r') #return how many times myChar shows up in data
#read data as an array of strings → character matrix
data = file.readlines()
for row in range(len(data)):
print(data[row])
7 8
def writeFile(fileName):
#open file so we can write to it.
f = open(os.path.join(__location__,fileName), 'w')
Solving a Mystery
It is telling the computer that you are
going to WRITE to the file
{(“The case is one where we have been
compelled to reason backward from
for i in range(10): effects to causes.”
f.write("wow. I'm writing to a file. i=" + str(i) + "\n")
– Sherlock Holmes)
The write function takes in ONE parameter (as a string).
Anything you want written must be converted to a single string, OR – The Adventure of the Cardboard Box}
you can call the write function multiple times (each time passing in a string).
– Sir Arthur Conan Doyle
f.close() #safely closest out the file
Exercise:
What happens if the file with the same name already exists?
What if it does not exist?
© 2016-2022 Sun-il Kim. 9 © 2016-2024 Sun-il Kim. 10
9 10
• Run time problems are also easier now (the tool tells you
where the program crashed)
11 12
2
initial inspection:
How do you start debugging? def testFunc3():
# here we have a more elusive logical problem that does NOT
• Compile-time errors are easy… # result in a runtime error. Rather, the result is just wrong.
Output example:
Enter side length a:3
• Run-time problems: a=float(input("Enter side length a:")) Enter side length b:4
– May or may not crash b=float(input("Enter side length b:"))
c=float(input("Enter side length c:")) Enter side length c:5
x (opposite side a): 50.20818050044277
• Start with an educated guess on where the problem is #get angles y (opposite side b): 0.0
• Inspect the program STATE x = math.acos(((b*b)+(c*c)-(a*a))/(2*c*c)) z (opposite side c): 90.0
– The program is obviously not working as you intended… y = math.acos(((a*a)+(c*c)-(b*b))/(2*a*a)) Area 6.0
z = math.acos(((a*a)+(b*b)-(c*c))/(2*b*b))
– You will NOT find the solution in your head → It’s in the code…
inspect it! #get area
– Do NOT randomly move things around area = a*b*math.sin(z)/2
13 14
Using the power of deduction Use prints to help inspect INTERMEDIATE values
def testFunc3(): def testFunc3():
# here we have a more elusive logical problem that does NOT Output example: # here we have a more elusive logical problem that does NOT Output example:
# result in a runtime error. Rather, the result is just wrong. # result in a runtime error. Rather, the result is just wrong. Enter side length a:3
Input side a:3
Input side b:4
Input side c:5 a=float(input("Enter side length a:"))
Enter side length b:4
a=float(input("Enter side length a:"))
b=float(input("Enter side length b:")) x(opposite a):NaN b=float(input("Enter side length b:"))
y(opposite b):55.771133672187425 c=float(input("Enter side length c:")) Enter side length c:5
c=float(input("Enter side length c:"))
z(opposite c):90.0 print (a, b, c) 3.0 4.0 5.0
Area:6.0 0.8762980611683406 0.0 1.5707963267948966
#get angles #get angles
x = math.acos(((b*b)+(c*c)-(a*a))/(2*c*c))
x (opposite side a): 50.20818050044277
x = math.acos(((b*b)+(c*c)-(a*a))/(2*c*c))
y = math.acos(((a*a)+(c*c)-(b*b))/(2*a*a)) y = math.acos(((a*a)+(c*c)-(b*b))/(2*a*a))
y (opposite side b): 0.0
z = math.acos(((a*a)+(b*b)-(c*c))/(2*b*b)) z (opposite side c): 90.0
3 5 z = math.acos(((a*a)+(b*b)-(c*c))/(2*b*b))
Area 6.0
print(x, y, z)
#get area
area = a*b*math.sin(z)/2 4 #get area
area = a*b*math.sin(z)/2
15 16
#get angles
z(opposite c):90.0
Area:6.0
– Pause and Inspect variables during run-time
x = math.acos(((b*b)+(c*c)-(a*a))/(2*c*c))
y = math.acos(((a*a)+(c*c)-(b*b))/(2*a*a))
z = math.acos(((a*a)+(b*b)-(c*c))/(2*b*b)) 5
#get area
3
• You can use break points
4
– You can set break points to any line of code
area = a*b*math.sin(z)/2
17 18
3
Double click to the left or line number
to set a break point.
19 20
When you run via the debug option, it will stop Notice that the variable explorer now has the new
at the break point. variable a with its value assigned.
Use the “run current line” button to execute Note that we are now at line 65.
code on line 64.
To see what the function call does, you have to
step into the function, instead of doing “run
current line.”
21 22
We are now inside testFunc2. Keep going down the line by clicking “run current
line,” and see what happens to the variables, and
You can continue by clicking “run current line.” watch the computer execute your program line by
line.
23 24
4
Exercise
• Test out the debugging.py program.
After a while, we are still in the loop. • Run one function at a time by modifying main.
The value of i is currently at 10 (see table above).
We are about to index into list[i] and it will cause
an array out of bounds error and return the
function with an index error (as you continue to
• Use the debugging tool to monitor the program
click on “run current line”) and see what happens.
25 26