Effective PyCharm Transcripts Chapter: Debugging Python applications Lecture: A debugging example

0:00 Well let's take PyCharm's. Debugger for a spin. And I've got an interesting little program here that we can play with because it has
0:07 some pretty unique flow and we'll be able to use the step over step into step out of actually see some interesting things about how Python works.
0:17 You might be familiar with the Fibonacci numbers there, these numbers and then the next number is generated by taking the previous two and adding
0:24 them. So one plus two is 3 plus three is 5 plus three is 8 13. Anyone on and on or infinity.
0:33 I've got this program and we've got our other file here that's going to generate this
0:38 Fibonacci sequence. I've got this other math tricks thing that will given a sequence, generate a subsequent of it for just the odd numbers.
0:51 So if we give it 112358, it's gonna give us 1135 etc. What's interesting about this is this is an infinite sequence and that is modeled here in
1:01 Python were actually generating an infinite sequence. And when we feed this one an infinite sequence, the results is itself an infinite sequence,
1:09 assuming it has infinitely many odd numbers I suppose. So what we're gonna do is we're going to explore how that flows know it might
1:17 sound tricky to generate an infinite series but in fact with the yield keyword it's ridiculously
1:23 simple. We're here, we're generating the numbers starting with 10, they want to do this add together here,
1:30 we do the step to calculate them and then this yield is going to return the value, we'll go work with it and then when we need more these co-routines
1:39 will allow us to jump back to where we left off basically this line and just keep running one time and then another through that loop.
1:47 Same thing here we go through whatever series we find an odd one. We give it up, it's on,
1:53 we're going to combine these two things in some interesting ways. So first let's just run it and see what we get because this is an infinite
2:01 series. We can't just print them out. Going to gather them up in this list actually then print it, we have to say after some point,
2:10 I'm no longer interested in looking at the Fibonacci numbers so let's stop. So let's run this real quick and there you have it.
2:18 1135 13 21. And then boom, this stuff takes off super quick. It's really interesting how big these get how quickly,
2:26 So isn't that cool? We generated this infinite sequence and then we filtered it down
2:31 and then we stopped. So let's go and actually set a breakpoint here and just see what's going on. So we already have a run configuration so I can click
2:40 this, I could sort of skip it, I guess by going debug here, but just press this here we are in our definition of defining data,
2:49 we've done our import but that's about it. We could step into we don't want to more likely to step into my code is
2:57 what we want. But I'm just going to step over for now. We're just gonna look at this particular file the first time through first of all notice
3:04 right over here, there's this really cool grey thing at the end that wasn't there before. And look at this, this is the value of this list.
3:13 It's not very interesting yet it's empty but you can even see that we can interact with it kind of like a hyperlink drop down thing.
3:20 Incredible. Now these are going to return what are called generators if you hover over
3:25 it, PyCharm even knows this is a generator didn't show us to us there but if we weren't in the debugger it would show us that we step over
3:33 here is our generator named 'fibs'. And then this one is going to be a generator named fib so look how quick
3:40 we generated to infinite series. That's because they're lazily evaluated. Find some way to keep going.
3:46 We've got our variable O and are odd fibs and we're going to start to go over them. Let's keep going.
3:52 Step along notice here's our one are always one when append. Watch what happens online for appear around data.
4:00 Oh yeah, so it changed and because it changed, it changes in this little overlay mode to orange, that means had a value and now it's got a new value.
4:11 So that's really cool and you can see down here we can even expand out the list and see it has one item.
4:16 Let's keep going. But one again now we should have our data has more, see the index where it changes Now we have our three,
4:25 remember the Fibonacci sequence is 1123 but we filtered out the two with our generator for
4:31 the odd numbers. I keep going and just see it stepping along here. Super cool. Now what if we said I'd like to see at this point exactly
4:42 what would happen if O was 9000? So no problem. We can come down here, we can click set value Or we could add as an in line.
4:53 Watch down here but I'm gonna just set the value. What do you want to be said?
4:56 9000. Remember this is a situation that basically because of this function couldn't exist but I want to figure out what happens if it's the case.
5:05 So now we've got these numbers and what should happen is we're going to break out of this. Right? So we step done,
5:12 I printed out gone back over here are console there, that's not the same output as before because we played with the debugger and we
5:20 changed it. How cool is that? I cannot tell you how much it delights me to see the variables over laid and
5:27 code. Let's just go down here and say I want to run, put a breakpoint first Run down here, then I want to just go straight to this location bam.
5:36 Run to cursor and we've got there for the first time. Notice all the variables here, but I cannot tell you how delightful it is to
5:44 be able to see these without, you know, fiddling with this stuff down here or some other window,
5:48 You just see it right there and you can even interact with it as we've seen So super, super cool.
5:55 Let's do another thing. Let's try to understand the flow. These restartable co-routines, they are mind bending if you have not played with them before.
6:04 So let's go back over here, start to debugger. I'm gonna step into my code. I just called that function and it didn't go into it.
6:15 That is weird. I'm gonna step into this one also didn't go into it. Okay, confusing. A normal function it would step into,
6:22 but with the generators, what we do is we create these lazily evaluated sequences. It's not until we first begin to interact with them,
6:29 like loop over them or try to get to some of them or something like that but we actually execute them.
6:34 So here, vice type step into were in our odd series and then if I type step into that, it's going to pull on the series.
6:43 The series is actually the Fibonacci generator. You can see right there again. Fantastically. Just over laid. All right. Step, we're at the beginning,
6:52 this is the first time we call this function. So we're going to step compute this now. That's one and one. We're going to yield that.
6:58 It's going to come back for the value here. So hold on for that. Oh, there we are. And as one now, we're going to yield that back over here.
7:06 This looks good when I put it on the list. Now, if I step back into normal functions,
7:11 would go back to the beginning but not co routines and generators remember in is already one going to step in again,
7:19 look where it went to, what's going to happen to current? not 2, there's our to actually the next one will be too. So this is one going to append it.
7:28 Step in again, this one is to we're not going to yield it, so we're gonna loop inside. Go back here, that'll be three and so on.
7:38 Hopefully that gives you a sense of what these generators are doing. Their flow through code is very different than standard code.
7:45 But look how easy it was for us to see that just using step into my code, basically. Really, really need to be able to do this with the
7:53 debuggers. And again, when we were in here we could see that the series that was passed. Is this Fibonacci generator. Super cool.

Talk Python's Mastodon Michael Kennedy's Mastodon