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