Effective PyCharm Transcripts
Chapter: Debugging Python applications
Lecture: Conditional breakpoints

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


Talk Python's Mastodon Michael Kennedy's Mastodon