The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Iterators and generators.
Discuss Iterators and generators. in the Python Programming forum on Dev Shed. Iterators and generators. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 8th, 2013, 12:01 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation Power: 1
|
|
|
Iterators and generators.
I have this generator:
PHP Code:
def takeOnly(it,pred,f):
count = 0
for i in it:
if not count == f:
if pred(i):
count = 0
yield i
else:
count += 1
else:
break
it receives an iterator, a predicate and a limit "f". The generator will yield all the items that meet the requirement of the predicate. As soon as "f" consecutive items in the iterator don't meet the requirement, the operation is terminated.
the generator works fine but I noticed that it has a little problem handling "infinite" iterators (the generator in this case doesn't stop, the memory is filled and the python shell crashes). How can I take care of this issue?
|

January 8th, 2013, 01:04 PM
|
 |
Contributing User
|
|
|
|
please show failure, works fine in python3 for several test cases.
Code:
>>> def takeOnly(it,pred,f):
count = 0
for i in it:
if not count == f:
if pred(i):
count = 0
yield i
else:
count += 1
else:
break
def odd(a):
return a & 1
... ... ... ... ... ... ... ... ... ... ... >>> ... ... >>>
>>> for i in takeOnly([1,2,3,4,5,6,6,6,6,6],odd,3):
... print(i)
...
1
3
5
>>> for i in takeOnly([1,2,2,2,2,3,4,5,6,6,6,6,6],odd,3):
... print(i)
...
1
>>> for i in takeOnly([1,2,3],odd,8):
... print(i)
...
1
3
>>> for i in takeOnly(itertools.repeat(1),odd,8):
... print(i)
...
1 (many 1's removed)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in takeOnly
KeyboardInterrupt
>>>
__________________
[code] Code tags[/code] are essential for python code!
|

January 8th, 2013, 01:22 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 32
Time spent in forums: 8 h 42 m 41 sec
Reputation Power: 1
|
|
Quote: | Originally Posted by b49P23TIvg please show failure, works fine in python3 for several test cases.
Code:
>>> def takeOnly(it,pred,f):
count = 0
for i in it:
if not count == f:
if pred(i):
count = 0
yield i
else:
count += 1
else:
break
def odd(a):
return a & 1
... ... ... ... ... ... ... ... ... ... ... >>> ... ... >>>
>>> for i in takeOnly([1,2,3,4,5,6,6,6,6,6],odd,3):
... print(i)
...
1
3
5
>>> for i in takeOnly([1,2,2,2,2,3,4,5,6,6,6,6,6],odd,3):
... print(i)
...
1
>>> for i in takeOnly([1,2,3],odd,8):
... print(i)
...
1
3
>>> for i in takeOnly(itertools.repeat(1),odd,8):
... print(i)
...
1 (many 1's removed)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in takeOnly
KeyboardInterrupt
>>>
|
Yes, take the last test you tried for example. That's exactly where my program gets stuck, I need to find an appropriate halting condition that blocks it from entering in an infinite loop. As I noticed, you too had to stop it with a keyboardInterrupt order. Rather, I would like to raise a StopIteration to exit the loop. How could this be done so it can work with "any" infinite iterator?
|

January 8th, 2013, 01:36 PM
|
 |
Contributing User
|
|
|
|
|
use another argument
Code:
def takeOnly(it,pred,f,at_most=8888888888):
count = 0
yielded = 0
for i in it:
if (count < f) and (yielded < at_most):
if pred(i):
count = 0
yielded += 1
yield i
else:
count += 1
else:
break
def odd(a):
return a & 1
import itertools
for i in takeOnly(itertools.repeat(1),odd,8,at_most=20):
print(i)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|