I'm debugging a python script, and I want to watch a variable and get notified whenever its value changes.
Is there a way to do this in pudb?
You can't simply ask for notification any time a value changes (that I'm aware of).
However, you can set both watch expressions and conditional breakpoints which should provide the capability that you're looking for.
First, go to the variable list (shift+V), then N to add a new watch. Enter in whatever variable you want to watch.
Now set a breakpoint at the places that your value can change - back to the main window ←, then find the lines and hit B. Then let your program run to that line or until your variable is defined.
Then shift+B to select the breakpoints window. Press enter to edit the breakpoint. Add a conditional expression - since your value should be set by now, you can see the value in your watch list. A simple <variable> != <current value> should do. Or you can enter a specific criteria.
Now ← back to the main window and let your program continue. When your conditional is true at that breakpoint, your program will stop and you will see the value in your watch list.
For an example, see the following screencast:
Related
I'm making a simplistic text adventure game for practice. I want players to be able to backtrack once they go farther into a scenario, for example after they go down a staircase I want them to be able to have "back" as an option and be able to retrace their steps.
I've already figured out every aspect of that except: How to clear the message that popped up telling them about the scenario they just entered.
I have code making their response a variable, and then determining if that variable is one of a set list of viable options, and if not looping them back to the start and deleting their previous entry with the line of code print("\033[A \033[A")
However, this code only deletes the last line of code, which varies wildly depending on screen ratio and different devices, and not the last output, which is the entire message that described to them the scenario.
Does anyone know a piece of code that can either
delete the entire last output of the console, in this case a printed line of code which exceeds one line and may extend for as many as 8-10 lines, detailing their scenario, or
clear the console and then warp them back to my predefined starting point (Example: def start0():), because after I clear the console the code start0() doesn't work.
When debugging with PyCharm, a variable list has more than 1000 data. When I go to see the value, the system just displays the first 100 data, I need to double click to see the next 100.
Is there any way to see all the data at once, or is there any way to let me see the last 100?
Open Debug window and in Variables panel click + (New Watch...) or press Insert.
Type variable_name[-100:].
That will let you see last 100 elements.
If variable is not a list an error {TypeError}'int' object is not subscriptable will be displayed instead.
Also unlike Evaluate Expression command, variables added to Watches persist from one debug session to another.
Is there a way to save the variable names in watches? Every time I debug, the list goes empty and I have to put in the same variables.
When you run the Debug script, there is Watches button.
Reading from a documentation about Watches:
While the Evaluate Expression command on the context menu of the Variables pane enables you to see one expression at a time, the Watches pane shows multiple expressions that persist from one debug session to another, until you remove them.
To add expression to persist from one session to another, simply click + sign and type the name of variable. Then, when you debug again, you will see it again being evaluated until you remove it (using - sign).
I've been making a small game for a homework assignment using Pygame with Livewires. I've been trying to debug it, but to no success; I can't get a look at the variables I want to look at before the main loop is executed. Though I can press F10 to skip over the main loop, that just stops the Autos and Watches window from working; apparently they can only records vars when the game is paused by the debugger.
Is there a way for me to use the debugger to look at the vars in runtime? Because no matter what I do while the debugger is paused, I can't look at the data inside the game objects I want to take a look in
Thanks to Jack Zhai's suggestion, I figured it out.
When the breakpoint is hit, unpause the debugger (shortcut: F5)
Play to the point in the game you want to debug.
Repause the debugger using Break All (shortcut: Ctrl-Alt-Break)
Press F10 a few times; this makes it go a few more steps in the main livewires loop.
In the autos window, there is an entry that contains the a list of the game's objects. Look through it, until you find the one(s) you're looking for. In my case, the list of the objects was self._objects. The object I was looking for was the second, which in other words, was self._objects[1].
The dropdown arrows of the object(s) you're looking for show the object's members. If you want to look at the object's properties in a less cumbersome way, use the Interactive Debugger to assign that object to a variable. That way, you can look at its values through typing objectNameHere.objectValueHere into the interactive debug console.
In my case, I had to type in player = self._objects[1] to get it assigned, then could see the player's x position by then entering player.x into the debug console.
--
I know this answer might only work for my specific problem, so if anyone else has a better one, please post it for others' sake.
My question may seem bizarre.
I am debugging a program in python with portable python, pyscripter.
Is it possible to ask pyscripter to do this? :
"Run the program until my_variable 's value become equal to 10, then stop at that point, and let me debug it line by line (using F7 or F8)"
I want some sort of breakpoint that activates when a variable gets some specific value.
Yes, PyScripter can do this. It is called conditional breakpoint.
Put a breakpoint, open Breakpoints tab, right-click on your breakpoint to bring context menu where you can add a condition (python expression)