Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 8th, 2013, 12:01 PM
Nightmareix35 Nightmareix35 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 32 Nightmareix35 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old January 8th, 2013, 01:04 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,348 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 35 m 46 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old January 8th, 2013, 01:22 PM
Nightmareix35 Nightmareix35 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 32 Nightmareix35 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old January 8th, 2013, 01:36 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,348 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 35 m 46 sec
Reputation Power: 383
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)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Iterators and generators.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap