Python 2.2 alpha - generators test

Steve Horne sh at ttsoftware.co.uk
Fri Jul 20 06:51:05 EDT 2001


I've been playing with the Python 2.2 alpha, found it to be pretty
reliable so far, and I've fallen in love with the iterators and
generators. However, I did find what I think is a problem in the
test_generators.py (sorry if the filenames slightly wrong) file.

I was looking at the 2**i * 3**j * 5**k problem, and decided to try my
own approach. I reached a point where I needed essentially a
duplicate-removing priority queue (if such a thing exists) but I
couldn't be bothered revising my heap theory so temporarily I decided
to just merge the new items into a simple ordered list.

I remembered a generator called merge or xmerge or something, and
decided to borrow that to get the initial version running quickly.

Trouble is, it didn't work at all.

Looking back at the merge function, I realised that when either one of
the input generators is exhausted the generator terminates immediately
- it does not pass on the remaining items from the other generator. In
fact, if one of the input generators ends immediately (empty list), no
items will be output at all irrespective of the number of items the
other generator could generate.

I'm not sure of the implications in the algorithm it was used in
(which I didn't fully understand), but it is certainly incorrect for
merging two ordered sequences - which is what it appeared to be - and
is certainly what caused my problems.

Is this a real bug, or just a case of me jumping to daft conclusions
about what the generator was meant to do?



BTW, sorry about the vague names - I only have that Python version
installed at home.

-- 
Steve Horne
Home : steve at lurking.demon.co.uk
Work : sh at ttsoftware.co.uk



More information about the Python-list mailing list