Mastering PyCharm Transcripts
Chapter: Debugging Python applications
Lecture: A debugging example
0:01 Notice over here I have created a debugging folder
0:03 for demos that we're doing in this chapter
0:06 and I have a program that goes across two files
0:09 you don't want to make it too complicated but not too simplistic as well.
0:13 So what we're going to run is program
0:15 and I have a run configuration for that up here
0:18 and it imports some things from math_tricks
0:20 and one of the math tricks we can do is
0:22 give me all the fibonacci numbers.
0:24 The other math trick is I can take any sequence of numbers
0:26 and turn them into odd numbers
0:28 and so if I give it the fibonacci numbers, I'll get the odd fibonacci numbers.
0:32 Now what's kind of insane is this is an infinite sequence, and so is this.
0:37 We've got to break out at some point,
0:40 we can't just loop over it or try to sum it up or something,
0:43 let's just run it and see what happens.
0:46 Okay, that looks like the fibonacci numbers except for 2 and 8 are missing
0:51 well because we only got the odd fibonacci numbers.
0:54 Our goal is to use the debugger to understand the flow of this program.
0:58 Now, how do we generate an infinite series?
1:01 Well, actually it's much simpler than you might think, here while true yield this,
1:05 so this is a co-routine, a generator function
1:09 and what it does is it just goes through the fibonacci algorithm
1:13 and uses the while true loop and the yield
1:16 and so we'll see what that means in a moment,
1:18 and then similarly, this just takes a series
1:21 and then if it's odd it will yield numbers back in that series.
1:25 So both of these, at least this top one is an infinite series
1:29 and this one is potentially infinite,
1:32 it's the somewhat shorter than what you send into it potentially
1:35 if you send in infinite numbers you might get an infinite set back.
1:40 Okay so let's go and just start by setting a breakpoint, that's pretty easy
1:46 so we'll set a breakpoint right here and hit the debug,
1:50 and immediately it runs down to this spot
1:55 and if we were over here on this page, like this
1:59 we could say show me where you are,
2:01 okay that's right, we're right here,
2:03 and you can see the call stack not super interesting
2:05 because all of this stuff down here,
2:07 this is all just Python start up -- here is our code.
2:10 We also have our variables, like we have our data here
2:13 and you can see that we have our list and it's empty for the moment
2:17 and we also have things like the various special stuff
2:21 like the __name__ is __main__ this time in the file and so on.
2:25 So, the other thing to notice that's pretty awesome
2:29 is up here right next to data, there's this and notice I can't select it
2:33 but there's this green that actually shows you what the value of data is
2:38 and you'll see that change over time,
2:40 and let's just step through for a minute,
2:44 so we step down, now we have a generator
2:46 because we're using yield which is pretty awesome
2:49 and then we're going to step down again and we have another generator
2:52 and notice how fast that was, we just got an infinite series of numbers
2:55 and it basically ran instantaneously,
2:58 and that's because we're not generating them yet until we go over them here,
3:01 so let's just step through a little bit
3:04 and notice now that o is 1, I can see that right here, that's really cool,
3:07 also you can see it down here
3:10 and we'll go and append it, notice now,
3:13 data is a list and it has the one that we just put in
3:16 we go through, do this a few times,
3:19 and now notice o changed to 3, its color here has changed
3:22 this should change color in a second when its value changes as well
3:26 now this turned orange because it's 1, 1, 3, so this is really great
3:31 and if for some reason we're coming along and say
3:34 well what would happen if o was not 13 but 9000
3:40 so we could actually come down here and say set value
3:44 maybe we'll be able to do that here as well—
3:51 no, I don't think so, we have to do it down here.
3:53 So we'll go over here and we'll say set value
3:56 and then let's just say what if it was 9000 okay
4:01 and we'll take our next step, it's 9000 and so that's bigger than this,
4:07 we'll break out and then we'll print our data, how cool is that,
4:10 over here in the console we have the ones that we got back.
4:12 So that's pretty amazing, let's do a little bit more though,
4:16 let's try to understand how this infinite series work
4:21 so we are just going to come down here and say run the cursor,
4:23 that's great, we already know that those work,
4:26 and instead of stepping over, let's step into our code
4:29 so we're going to step into here and a series itself is a generator
4:34 it actually says right there on the right, the fibonacci one up there
4:37 and we're going to step down here like this and go through this
4:43 and we could step into valuating this series
4:46 and notice, now we're over here,
4:49 so these generators have really funky behavior
4:51 they're like restartable functions, so let me just step around a little bit
4:55 notice we have 5 and 8
4:59 now if I step out, go down here, we are 5
5:05 now if I step in again, it's not going to go and rerun this function
5:07 it's going to step right here probably with the values of 8 and 13 or something
5:14 so let's step in, 8 and 5 and so you go right back
5:18 so you can use the debugger actually really well
5:21 to sort of figure out the flow, even though these things are quite odd
5:25 so let's step out a little bit, out again,
5:28 step through here right, now it's 13, now it's 21,
5:32 now we can step back into this one through here, or step back in there, right
5:38 so this is really, really great.