-
Notifications
You must be signed in to change notification settings - Fork 1
/
debugging.py
146 lines (117 loc) · 5.08 KB
/
debugging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'''Debugging'''
# General Tips
# -----------------------------------------------------------------------------
# Though not very advanced, dropping print() statements and other reflection
# related functions can tell you a lot about what's going on. Beyond that:
# print the values of your local arguments with:
print(vars())
# If you run your program with an -i flag, it will drop you into the
# interactive interpreter if the program fails:
# $ python3 -i myfile.py
# Standard Library: pdb module
# -----------------------------------------------------------------------------
# Bug test: read a file of countries and their capital cities, separated by a
# comma, and write them out as capital, country. In addition:
# – Make sure they capitalized incorrectly
# – Remove any extra spaces extra spaces
# – Stop the program if we find the word quit (uppercase or lowercase)
# Here's a sample data file data/cities.csv:
'''
France, Paris
venuzuela,caracas
LithuniA,vilnius
argentina,buenos aires
bolivia,la paz
brazil,brasilia
chile,santiago
colombia,Bogotá
ecuador,quito
falkland islands,stanley
french guiana,cayenne
guyana,georgetown
paraguay,Asunción
peru,lima
suriname,paramaribo
uruguay,montevideo
venezuela,caracas
quit
'''
# Here's pseudocode:
'''
for each line in the text file:
read the line
strip leading and trailing spaces
if 'quit' occurs in a lowercase copy of the line:
stop
else:
split the country and capital by the comma character
trim any leading and trailing spaces
convert the country and capital to titlecase
print the capital, a comma, and the country
'''
# Here's the actual code:
def process_cities(filename):
with open(filename, 'rt') as file:
for line in file:
line = line.strip()
if 'quit' in line.lower(): # should be if 'quit' == line.lower():
return
country, city = line.split(',')
city = city.strip()
country = country.strip()
print(city.title(), country.title(), sep=',')
# import pdb; pdb.set_trace() # this will launch the debugger
breakpoint() # this will launch the debugger as of Python 3.7
process_cities('data/cities.csv')
# pdb Commands
# -----------------------------------------------------------------------------
# Documented commands (type help <topic>):
# EOF c d h list q rv undisplay
# a cl debug help ll quit s unt
# alias clear disable ignore longlist r source until
# args commands display interact n restart step up
# b condition down j next return tbreak w
# break cont enable jump p retval u whatis
# bt continue exit l pp run unalias where
# type 'c' to continue (the program will run until it ends normally or via
# an Error/Exception). If it ends normally, we now it's logic error
# rather than a syntax error.
# type 's' single step through lines and into functions/methods
# (this will include any modules you might be using like sys)
# type 'n' to step past a function/method (execute and move onto the next).
# Generally speaking you would 'n' to step past any library code.
# type 'l' to see the next few lines at once
# type 'll' to see even more of the next few lines at once
# type 'l' and a line number to see lines from that point onward
# type 'b' and a line number to insert a breakpoint
# type 'b' alone to see all your breakpoints
# type 'cl' and a number to (clear) a breakpoint
# NOTE: Your program will ONLY stop at a breakpoint if it has a chance to
# actually get to that spot in the code.
# type 'u' to go up (back)
# type 'p' to print the value of something. i.e. p line
# type a variable name and it will output its current value
# type '?' or help when in the pdb to see all the commands.
# type help command to get help on that one.
# NOTE: when viewing lines, 'B' indicates a breakpoint you've inserted and
# '->' indicates your current line
# To use the debugger from the command line, import the pdb module by typing:
# $ python3 -m pdb myfile.py
# For the example above if we drop a breakpoint at line 68 and continue our
# program, when it stops if we type line to see the current value of line we
# see our problem: ecuador,quito.
# breakpoint(*args, **kws)
# ------------------------------------------------------------------------------
# New to Python 3.7, this function drops you into the debugger where ever you
# call it in your code. Specifically, it calls sys.breakpointhook(), passing
# *args and **kws straight through. By default, sys.breakpointhook() calls
# pdb.set_trace() expecting no arguments. It is purely a convenience function
# so you don’t have to explicitly import pdb or type as much code to enter the
# debugger.
images = ['pickle.png', 'dog.jpg', 'car.png', 'apple.gif', 'baloon.psd']
todo = []
for i in images:
if not i.endswith('.png'):
breakpoint()
todo.append(i)
print(todo)