Effective PyCharm (2021 edition) Transcripts
Chapter: Debugging Python applications
Lecture: Conditional breakpoints
0:00 Let's imagine that we need to check out what's happening to the Fibonacci sequence right near
0:06 when we hit this break, You could even make this bigger,
0:09 we could say this is going to be 10,000 here.
0:13 one way to do it would be I'm going to put a breakpoint right here and
0:17 press debug and we go over here and we can just run be good step
0:23 stop. What does those three?
0:27 Okay, it's 5,13 this might take a while to get to something around 10,000.
0:32 I could press this button to just run until you hit the next breakpoint.
0:36 That's pretty good. Then eventually it exits.
0:41 Okay, well I thought maybe there was one higher but I guess this is the
0:45 one we want to stop and so as soon as it's over 5000,
0:48 let's do this again. Just feeling a little bit cumbersome help so let's go and
0:53 actually we could go down here and we could put something like you know you put
0:56 'O' in this case and I'll show that.
0:58 But what we really could do It is better we could put that test like is
1:02 O greater than 5000. I notice we get auto complete of all the things here
1:08 but I'm just gonna use the constant 5000 boss,
1:12 we could just hit this again until it gets to be true,
1:15 click it a little bit, here we go.
1:17 True, that's the first one and then I could go and look around here and
1:20 see what this value is and so on.
1:23 But we can do better still.
1:25 Okay imagine you're doing something like reading through a log file parsing through a text file
1:31 and you're reading it line by line If there's 5000 lines and there's something going wrong
1:36 on one of them. You may well want to say does this text which I
1:40 know is where the problem is appear in the line,
1:43 we're trying to process something like that.
1:46 How do you make that happen?
1:47 one. Super cheesy way would be to say if O was greater than 5000
1:53 X=1 and set up at a breakpoint right there that would do it.
2:00 Okay, so what is what is O now O it's the 6000 were looking for
2:06 but don't do this, you don't need this,
2:08 check this out, we'll go back to this breakpoint and then we're gonna right click
2:13 and check that out. There's these options,
2:15 you can enable it and disable it.
2:17 So and it's a breakpoint that's here,
2:19 you can come back to you but right now you don't need it,
2:22 you can say what you want to suspend the whole program are just the thread and
2:26 then you can write code here like O notice the auto complete for all the stuff
2:30 that would be here like oh dot but we'll just say O greater than 5000 so
2:39 instead of writing that as a placing code where we set a breakpoint on it temporarily
2:43 just do it on the breakpoint it again.
2:46 Going to stop once, Where is the value?
2:48 6765 exactly where we wanted to pause and check things out,
2:54 you can also right click on this and there's a little more here.
2:57 It's a whole bunch more we can do so this bit you saw already but look
3:01 we can log when the breakpoint hits and print out the stack trace to see what
3:06 called it to get here. We could evaluate something along so check this out,
3:10 we could do like an F string,
3:11 we could say the value of O is no auto complete and everything well and we
3:22 can also remove it once it's hit if we want or disable other things.
3:27 So now run it one more time.
3:32 Okay, get hit let's go to the console,
3:34 look at that, the value of O is this.
3:37 And actually let's do one more quick quick thing let me put over here.
3:41 Always have a hard time seeing the log messages because they basically look the same as
3:46 everything else. So this is an option here,
3:49 we could do that. Let's run it again,
3:53 that should make it pretty obvious.
3:56 So setting the break point here and logging out what the value is can make our
4:00 debugging. Super, super easy.
4:02 We skip the non interesting cases by adding a condition and we can evaluate and log
4:06 it and then off it goes,
4:08 we could even uncheck suspend Here and let's just say this is greater than 500 and
4:15 all we'll get is basically a log message,
4:16 notice how it turns yellow, so we run this,
4:19 it finishes, boom. But then any time it was over 500,
4:23 we got that breakpoint became basically a conditional logger.
4:27 And then if you don't want any more boom,
4:29 it's gone. So, so many amazing things you can do with conditional breakpoints.
4:34 It's easy to just see them as on and off.
4:37 Little red buttons try to remember to take advantage of them in the situations where it
4:41 will really, really help you.