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