[PATCH] merge3 fails with python2.3

Martin Pool mbp at sourcefrog.net
Wed Jul 6 02:25:04 BST 2005


On  5 Jul 2005, John A Meinel <john at arbash-meinel.com> wrote:

> I think use generators wherever you can, but since you *already* have a
> list, just use it. Is it possible for get_matching_blocks to return and
> empty list? []

According to the API, no, it always returns at least one.  I at first
thought this might be different in Python2.3 but it seems not.  So in
fact it wouldn't be necessary to guard the first two .next() calls.

> alst = SequenceMatcher(None, self.base, self.a).get_matching_blocks()
> blst = SequenceMatcher(None, self.base, self.b).get_matching_blocks()
> 
> if alst:
>    abase, amatch, alen = alst.pop(0)
> if blst:
>    bbase, bmatch, blen = blst.pop(0)
> 
> while alst and blst:
>    ...
>    if (abase + alen) < (bbase + blen):
>        abase, amatch, alen = alst.pop(0)
>    else:
>        bbase, bmatch, blen = blst.pop(0)
> 
> Then you don't need to keep a counter, because an empty list
> automatically is counted as False.

The Python list() type is really an array, so using a index is more
efficient than shuffling everything down to the front (though arguably
less clear).  In this case the lists are probably so small that the
difference would not be noticeable.

> You could also just use 2 stop iterations traps:
> try:
>    abase, amatch, alen = aiter.next()
>    bbase, bmatch, blen = biter.next()
> except StopIteration:
>    aiter = False
>    biter = False
> 
> while aiter and biter:
>    ...
>    try:
>        if (abase + alen) < (bbase + blen):
>            abase, amatch, alen = aiter.next()
>        else:
>            bbase, bmatch, blen = biter.next()
>    except StopIteration:
>       break
> 
> That way you don't have a giant try/catch block.
> 
> I think the internal iteration is great. It makes everything lazy and
> easy to work with at the top level. Just don't go around creating iter()
> objects that you don't need, since their implementation changed between
> 2.3 and 2.4

Right, and don't assume the boolean value of an iterator is useful.

-- 
Martin




More information about the bazaar mailing list