Effective PyCharm (2021 edition) Transcripts
Chapter: Debugging Python applications
Lecture: Concept: Debugging

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We have a whole variety of ways of viewing variable state.
0:03 When we stop in the debugger obviously down here in the bottom right,
0:07 we've got the watch list of variables.
0:10 So we've got our creatures and if it's something interesting like a list we can expand
0:14 it out and see more details.
0:15 We've got our command, we've got the active creature turns out to be a bat
0:19 of level three right now. So while this is fine,
0:22 it doesn't really show you in context what's happening,
0:25 doesn't show you here this value.
0:28 We accept it from. The user is an A right on the line where we
0:32 did the input and so on.
0:33 So up here we can see way more information.
0:36 We can see that we have this great text.
0:39 This is not a comment, this is something that debugger puts here says here is
0:43 a hero And the text representation of that is a creature.
0:47 Gandalf of level 75. And we saw that we can even highlight it and pull
0:51 it down basically the same interaction we have in the watch window Over here we have
0:56 the active creature, that's a bat of level three and the color is important.
1:00 Remember the hero is great because that's the value has always had since we've recently looked
1:06 at it, but this creature has turned orange and that means it's changed last time
1:12 through the loop, it was something else.
1:13 Who knows what a toad or a dragon or whatever it is in our little story
1:17 here, But now it's become a creature a bat of level three.
1:22 And so the fact that it changed,
1:23 orange tells you not just what is its value,
1:25 but that it has somehow been updated over here.
1:29 You can see there's this input that we got from the user,
1:32 what did they type for this particular run of the game loop?
1:36 They typed A so they're going to be attacking,
1:39 What's going to do it here?
1:40 Oh, Gandalf of level 75 is going to be attacking a creature bat of level
1:43 three super cool. The way we can put all this together,
1:48 seeing the values is great. But sometimes you might want to change the situation here
1:52 We've asked the user to input something.
1:55 What do you want to do? Attack run away or look around.
1:57 They said attack like oh, you know,
1:59 we really should have typed run away because that's the scenario we're trying to test.
2:04 Well let's just change it. There's all these situations and programs that are hard to
2:10 come up with exactly the right situation.
2:13 So you can use the debugger to change it.
2:14 So if we go down to the command and the variable window and choose set value
2:19 or even to the gray part that the debugger put in and pull it down and
2:23 say set value, we can change it.
2:25 What if we put in [r] if we set this value and change into [r] what
2:30 happens even though it says in the console up here.
2:32 What do you want to do?
2:33 Attack or runaway or look around,
2:34 we set attack, but then all of a sudden the wizard has become unsure of
2:38 his power and flees Why?
2:40 Because we changed what happens. That's what happens if he were to run away.
2:44 So very cool that we're able to sort of carefully control the flow for different situations
2:50 to build up exactly what we want to test,
2:54 It's pretty obvious you can watch variables,
2:55 but you can also come over here and hit the plus and add a watch expression
2:59 So I want to know any time.
3:02 Is there an active creature whose level is greater than 15?
3:06 Yes or no. We can watch that expression and you can put all sorts of
3:10 interesting things like some database level call that says is the current user and admin yes
3:16 or no. Right, That's not necessarily a property of the uh the current user
3:20 object. Although maybe if it's not though you can still check that here in this
3:25 evaluation watch area. So it's not just about watching variables,
3:29 even though quite literally the title of the Windows variables really it's variables plus expressions.
3:38 We saw the breakpoints are incredibly powerful here,
3:41 we have two break points on our creature wizard game,
3:44 we want to add a new one.
3:45 You click in the gutter, Just the right of where the line numbers are,
3:48 if you're showing them and that will create a default on off type of breakpoint is
3:54 going to stop every time you hit it.
3:56 We saw that if you right click on it though,
3:58 you can actually change this to be something conditional.
4:02 So imagine in this scenario we described before,
4:04 I want to only hit the breakpoint if the user says R for run away.
4:09 So the first bit of this test,
4:11 if it's a else if it's it and or whatever,
4:14 I'm going to put a breakpoint here and set the condition to 'cmd==r'
4:18 right. Just a standard python conditional.
4:21 If that ever evaluates to true,
4:23 we stop. So then we'll skip over all the other cases that are not running
4:27 away scenarios and not have to deal with jumping out of the debugger too much.
4:32 Remember this gives you auto complete and you can do pretty advanced python right there.
4:37 If you expand it, you can do even more like check the evaluate and log
4:40 then print out some sort of computed message with details about what's happening every time
4:46 the breakpoint hit. And if you uncheck suspend,
4:49 that will basically become a breakpoint.
4:51 That just logs out messages. Remember once you've got a conditional breakpoint,
4:55 it has this little question mark by it.
4:58 So normally it would just be a circle But now because that question mark,
5:02 that means it's conditional. Be careful if you click on it without right clicking,
5:07 you're just going to disable it.
5:08 It will go away and well who knows what the condition was,
5:11 but it's gone. So just be careful to not wipe these away unless you're sure
5:15 you're done with them. Finally,
5:17 if we turn this into a breakpoint that only logs messages,
5:21 it turns yellow so you can see here we have suspend unchecked but evaluating log is
5:27 turned on and we have this little F.
5:29 String. The command is whatever they've typed the creature is the name of the active
5:33 creature. And when we do this we get these little messages here like breakpoint reached
5:38 The command is 'a' and the active creatures.
5:41 The Tiger command is a active creature is a dragon and so on.
5:46 Remember in my example I put like some stars or something to make it stand out
5:49 because here without these boxes it's super hard to see what is the breakpoint and what
5:54 is actually just program output around it.
5:57 So be sure to add a little bit something to make these stand out.