<html>
Hi people,<br><br>
Is there a way for me to check if an iterator has "run out". I
was hoping bool (iterator) would help but it always returns
true.<br><br>
example:<br><br>
<tt>iterObj = iter (obj)<br>
while 1:<br>
for i in iterObj:<br>
process i<br>
break based on some condition<br>
# continue until we've exhausted the iterator<br>
if bool (iterObj)==0: break <br><br>
<br>
</tt>Below is a place in my project where i would have liked it. Read it
if you have the time.<br><br>
Here, I'm generating a dialog box dynamically from a tree
structure.<br><br>
<tt>case (1): without an iterator<br><br>
def AddRecursive (parentSizer, obj):<br>
if obj.children: # obj.children = list of children<br>
leaves = []<br>
for i in obj.children:<br>
if i.children:<br>
leaves.append (i)<br>
else:<br>
<font face="Andale Mono" color="#0000FF">
if leaves:<br>
sizer = XXX # create a new sizer and add to parentSizer<br>
for j in leaves: AddRecursive (sizer, j)<br>
# add all the leaves to the new sizer<br>
leaves = []<br>
</font>
AddRecursive (parentSizer, i)<br>
<font face="Andale Mono" color="#0000FF">
if leaves:<br>
sizer = XXX # create a
new sizer and add to parentSizer<br>
for j in leaves:
AddRecursive (sizer, j)<br>
# add all the leaves to
the new sizer<br>
leaves = []<br>
</font> else:<br>
# handle the other
case
<br>
<br><br>
</tt>Here, the lines in blue (if its plain text, then the code for
"if leaves:" is repeated twice because I have to process the
leaves either (1) when i dont find a leaf in the traversal, or (2) when
the traversal is complete.<br><br>
A better way would be <br><br>
<tt>case (2): with an iterator<br><br>
def AddRecursive (parentSizer, obj):<br>
leaves = []<br>
if obj.children: # obj.children = list of children<br>
<font face="Andale Mono" color="#0000FF">iterChildren = iter
(obj.children)<br>
</font> while 1:<br>
<font face="Andale Mono" color="#0000FF">for i in iterChildren:<br>
</font>
if i.children: leaves.append (i)<br>
else:
break<br>
if leaves:<br>
sizer
= XXX # create a new sizer and add to parentSizer<br>
for j
in leaves: AddRecursive (sizer, j)<br>
# add
all the leaves to the new sizer<br>
leaves
= []<br>
AddRecursive
(parentSizer, i)<br>
<font face="Andale Mono" color="#0000FF">if not(bool(iterChildren)):
break<br>
</font> else:<br>
#handle the other case<br><br>
</tt>the problem is that to terminate the while loop, i need to know if
the iterator has "finished". bool(iterChildren) always returns
true.<br><br>
If anyone has a better solution, please do tell me. the line "if
iterChildren==obj.children[-1]: break" does a nice job for me rite
now (but only because obj.Children is never empty)<br><br>
The reason I pasted all this code is to show that it can be helpful to
have a nice value for a bool (iterator) (always returning 1 seems quite
useless.) Hopefully some future version of python can have this
additional change. (it should be trivial i think.....when StopIteration
is thrown, a flag can probably be set in the iterator
object???)<br><br>
Chirayu.<br>
</html>