Terminate for loop in console - Printable Version +- Gimp-Forum.net (https://www.gimp-forum.net) +-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP) +--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP) +---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions) +---- Thread: Terminate for loop in console (/Thread-Terminate-for-loop-in-console) |
Terminate for loop in console - pwiecek56 - 07-04-2023 I'm trying to learn python scripting. I'm working in the Python-fu console How do I terminate a loop? example: ############################# list = gimp.image_list() for x in list: pdb.gimp_image_delete(x) list ############################# No matter what I try, the console tries to include the next line in the loop RE: Terminate for loop in console - PixLab - 07-05-2023 you might need a break statement I'm not a python guy, nor a programmer, but I found those: https://www.w3schools.com/python/python_for_loops.asp https://www.programiz.com/python-programming/break-continue RE: Terminate for loop in console - pwiecek56 - 07-05-2023 Thanks, I'll try that. I already tried 'pass' but that didn't do it Can someone confirm that if I save it as a .py file and run it that the loop will work? RE: Terminate for loop in console - Ofnuts - 07-05-2023 (07-04-2023, 03:55 PM)pwiecek56 Wrote: I'm trying to learn python scripting. I'm working in the Python-fu console You just strike [enter] until you are back to the ➤➤➤ prompt: [attachment=10022]
But you can't delete an image that hasn't been created by the script, [attachment=10023]
This is because such image has a view and you cannot delete an image that has views. And you can't normally obtain a reference to an existing image view to close it (but if you create the view with a script then you have a reference to the view, so you can close it in the script).
There is an Images dockable dialog from which you can manually delete images that have no attached "view".
[attachment=10024]
RE: Terminate for loop in console - pwiecek56 - 07-05-2023 Thank you for both answers RE: Terminate for loop in console - Ofnuts - 07-05-2023 (07-05-2023, 06:59 AM)pwiecek56 Wrote: Can someone confirm that if I save it as a .py file and run it that the loop will work? You can save it to a side file, with a few extras empty lines at the bottom, and copy-paste to the console when needed. But writing a real Python plugin is not that complicated, you just need to add some boilerplate to have it "register" and be included in the menus. RE: Terminate for loop in console - Dogarithm - 12-16-2023 IIRC, "pass" is used to replace a piece of code that is usually syntactically required, but for some reason or another, there is no instruction to give in practice. So "pass" is just telling the interpreter to run the next line of code instead. Like playing a game and choosing to skip your move. Break might be what you want instead to stop a loop. But in general, this is a recursion problem. For loops are a recursion, but they're really nice in that they often have terminal conditions built in, unlike while loops. ie, Code: for i in range(10): This has a terminal case, the result of that code will print the integer "9". The range function in the for loop gives an exact number of times the recursion is to be called. I don't what the pdb.gimp_image_delete(x) method does, but if it modifies the list in any way per iteration, that might be something to keep an eye on. You might be getting a recursion without a well-defined terminal condition, where the computer knows it needs to stop upon reaching some condition. If that list doesn't reach a defined terminal condition, it won't terminate. No promises, but I hope that helps RE: Terminate for loop in console - Ofnuts - 12-16-2023 (12-16-2023, 01:05 AM)Dogarithm Wrote: IIRC, "pass" is used to replace a piece of code that is usually syntactically required, but for some reason or another, there is no instruction to give in practice. So "pass" is just telling the interpreter to run the next line of code instead. Like playing a game and choosing to skip your move. Break might be what you want instead to stop a loop. But in general, this is a recursion problem. For loops are a recursion, but they're really nice in that they often have terminal conditions built in, unlike while loops. ie, There is no "recursion" here, just an iteration... In programming, recursion in when some piece of code calls itself. For instance you can define the even-ness of a positive integer as being the opposite of the even-ness of its predecessor, in other words, isEven(i)=not isEven(i-1), so if you know the value for a number (for instance, 0), you can write the function like this: def isEven(i): return True if i==0 else not isEven(i-1) (of course three are much more efficient and non-recursive ways to write the function). (12-16-2023, 01:05 AM)Dogarithm Wrote: This has a terminal case, the result of that code will print the integer "9". The result of your loop is 10, and this is proof that it is very dangerous and/or misleading in such a for loop to alter the loop counter (it turns out that in this particular form Python resets i to the next item from the range()). (12-16-2023, 01:05 AM)Dogarithm Wrote: The range function in the for loop gives an exact number of times the recursion is to be called Technically the range() gives you a list of items(*) and you execute the loop once for each item. It just happens that range() produces an output of known size, with nicely ordered numbers. (12-16-2023, 01:05 AM)Dogarithm Wrote: I don't what the pdb.gimp_image_delete(x) method does, but if it modifies the list in any way per iteration, that might be something to keep an eye on. Good remark, but it depends on what you iterate (i.e., the thing after the in in the for ... in ... instruction). In this case it is a list that was obtained once for all (gimp.image_list()). (*) In Python v2. In Python v3, range() is a "generator" that creates the required item only when needed so you don't create a large list just to iterate it. In Python v2, you can use xrange() for this. |