09 Aug 2015 by dryobates
I often see programmers, even experienced, that tediously press "n" and "s" few hundreds of times, one after another, only to skip some for loop or get into state where loop is executed n-th time in pdb. Here is a little tip how to do it smarter.
What I often see is debugging some loop with code like below:
I wrote earlier how to cope with above problem and get debugger started only once. But often we want to stop execution when some condition is met. It would be a lot of typing if you would like to stop execution when "i == 98". Better solution is to put a breakpoint with condition:
With above code execution will be stopped only when "i == 98".
Sometimes execution path is quite complex and putting set_trace just before loop isn't good choice. If you know filename and line number you can forget about set_trace and editing any file. Just run program with pdb and place breakpoint in file and line you want it:
Sometimes condition isn't easy to express with simple math. There are times when you have to print some more complex variable and decide yourself if you should skip another loop or not. In such cases you often put simple breakpoint inside a loop and type:
And so one. Print, continue, print, continue... If you end up with such repetitive task it's time for aliases:
Alias definition has simple, yet powerful syntax. You can pass parameters to aliases or define them ahead in ".pdbrc" file. All of that you can find in documentation  What you won't find in documentation is ";;" trick. Placing double semicolon while defining alias allows to stream multiple commands as seen above.
More elastic alias that could be placed in ".pdbrc" could be:
|||pdb's Documentation https://docs.python.org/2/library/pdb.html|