0

Goal : While being in pdb, I have a "for" loop which runs 1000 ("x" which is not constant) times. I want to skip past 565 iterations of this loop, but look at the 566th ("y" which is not constant) iteration of this loop alone.

The main issue is "y" : the iteration I want to look at is not constant, it can be anything based on previous part of the code, therefore I cannot have an "if" statement inside "for" loop to check which iteration it is and use pdb.set_trace() inside the "if" loop.

code to give some perspective:

def process_queries(input_tuple):
    queries, n = input_tuple
    result = []
    phoneBook = phone_book(n*2)
    for cur_query in queries:
        if cur_query.type == 'add':
            phoneBook.add_number(phoneBook.current_linked_list, cur_query.number, cur_query.name)
        elif cur_query.type == 'del':
            phoneBook.del_number(phoneBook.current_linked_list, cur_query.number)
        else:
            response = phoneBook.find_number(phoneBook.current_linked_list, cur_query.number)
            result.append(response)
    return result

queries is a list which is of length x. process_queries function is called by another function "function1". Based on the logic in "function1", the iteration of "for" loop in for cur_query in queries I might want to stop is different each time and decided on the fly when I'm already in pdb. Therefore, I can't use this:

for index, cur_query in enumerate(queries):
    if index == 566:
        pdb.set_trace()
Zizou
  • 67
  • 5
  • What have you tried - did you try not skipping the y-1 times? What’s the problem with not skipping? – DisappointedByUnaccountableMod Apr 12 '20 at 08:28
  • 1
    @barny how do you skip y-1 iterations of a for loop, that is the question. Since "y" is huge, I don't want to go through y-1 iterations (which is "not skipping") like you suggested. The problem with not skipping is that it takes way more time to debug. I want to shorten the time it takes to debug by "skipping y iterations". – Zizou Apr 12 '20 at 17:55
  • 1
    The linked duplicate says you can't directly do this, and Martijn knows his stuff. Still, I was wondering if you could somehow use [jump](https://stackoverflow.com/questions/27741387/python-pdb-skip-code-as-in-not-execute) to skip some of your code interactively...I'm not sure. – Andras Deak -- Слава Україні Apr 12 '20 at 18:39
  • @AndrasDeak yes, in my case `jump` will be of some help because it can help me to jump upto end of each iteration atleast instead of linearly walking through each line of code inside the for loop. But I would still have to iterate y-1 times before I come to "y"th iteration :( – Zizou Apr 12 '20 at 20:24
  • 1
    I figured you could have a `continue` at the top of your loop, and set a conditional breakpoint that lets you jump over it in the specific iteration you're looking for. You still have to edit the code, so you might as well take Martijn's suggestion and step the iterator n times yourself. – Andras Deak -- Слава Україні Apr 12 '20 at 21:07

0 Answers0