Re: empty lists vs empty generators

2005-05-04 Thread Jeremy Bowers
On Wed, 04 May 2005 20:33:31 +, Leif K-Brooks wrote: > With the EmptyGeneratorDetector class as you defined it, lists will fail: > > >>> EmptyGeneratorDetector([]) > Traceback (most recent call last): >File "", line 1, in ? >File "", line 15, in __init__ > AttributeError: 'list' objec

Re: empty lists vs empty generators

2005-05-04 Thread Leif K-Brooks
Jeremy Bowers wrote: > On Wed, 04 May 2005 13:45:00 +, Leif K-Brooks wrote: > > >>Jeremy Bowers wrote: >> >>>def __init__(self, generator): >>>self.generator = generator >> >>You'll want to use iter(generator) there in order to handle reiterables. > > > Can you expand that expla

Re: empty lists vs empty generators

2005-05-04 Thread Jeremy Bowers
On Wed, 04 May 2005 13:45:00 +, Leif K-Brooks wrote: > Jeremy Bowers wrote: >> def __init__(self, generator): >> self.generator = generator > > You'll want to use iter(generator) there in order to handle reiterables. Can you expand that explanation a bit? I'm not certain what you

Re: empty lists vs empty generators

2005-05-04 Thread Leif K-Brooks
Jeremy Bowers wrote: > def __init__(self, generator): > self.generator = generator You'll want to use iter(generator) there in order to handle reiterables. -- http://mail.python.org/mailman/listinfo/python-list

Re: empty lists vs empty generators

2005-05-03 Thread Michele Simionato
Andrea Griffini: > Are you sure this is going to do the right thing ? Argh! I missed these two lines from the documentation: """Note, once tee() has made a split, the original iterable should not be used anywhere else; otherwise, the iterable could get advanced without the tee objects being info

Re: empty lists vs empty generators

2005-05-03 Thread Andrea Griffini
On 2 May 2005 21:49:33 -0700, "Michele Simionato" <[EMAIL PROTECTED]> wrote: >Starting from Python 2.4 we have tee in the itertools >module, so you can define the following: > >from itertools import tee > >def is_empty(it): >it_copy = tee(it)[1] >try: >it_copy.next() >except St

Re: empty lists vs empty generators

2005-05-02 Thread Terry Reedy
"Brian Roberts" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I'm using using generators and iterators more and more intead of > passing lists around, and prefer them. However, I'm not clear on the > best way to detect an empty generator (one that will return no items) > when som

Re: empty lists vs empty generators

2005-05-02 Thread Michele Simionato
Starting from Python 2.4 we have tee in the itertools module, so you can define the following: from itertools import tee def is_empty(it): it_copy = tee(it)[1] try: it_copy.next() except StopIteration: return True else: return False It works with generic i

Re: empty lists vs empty generators

2005-05-02 Thread Bengt Richter
On 2 May 2005 16:14:57 -0700, [EMAIL PROTECTED] (Brian Roberts) wrote: >I'm using using generators and iterators more and more intead of >passing lists around, and prefer them. However, I'm not clear on the >best way to detect an empty generator (one that will return no items) >when some sort of

Re: empty lists vs empty generators

2005-05-02 Thread Jeremy Bowers
On Mon, 02 May 2005 16:14:57 -0700, Brian Roberts wrote: > Q1: Is there a better or alternate way to handle this? Q2: Is there a way > that handles both lists and generators, so I don't have to worry about > which one I've got? Are you in control of your generators? You could put a method on them

Re: empty lists vs empty generators

2005-05-02 Thread Roy Smith
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Brian Roberts) wrote: > I'm using using generators and iterators more and more intead of > passing lists around, and prefer them. However, I'm not clear on the > best way to detect an empty generator (one that will return no items) > when some s

Re: empty lists vs empty generators

2005-05-02 Thread jfj
Brian Roberts wrote: > I'm using using generators and iterators more and more intead of > passing lists around, and prefer them. However, I'm not clear on the > best way to detect an empty generator (one that will return no items) > when some sort of special case handling is required. > Usually

empty lists vs empty generators

2005-05-02 Thread Brian Roberts
I'm using using generators and iterators more and more intead of passing lists around, and prefer them. However, I'm not clear on the best way to detect an empty generator (one that will return no items) when some sort of special case handling is required. Typical code for handling an empty list: