def merge_lz_operations(lzops):
    """Merge consecutive LZ operations into single ops if possible."""
    lzops = iter(lzops)
    try:
        prev = lzops.next()
        while True:
            cur = lzops.next()
            if isinstance(cur, LZLiteral):
                if isinstance(prev, LZLiteral):
                    prev.data += cur.data
                else:
                    yield prev; prev = cur
            else:
                if isinstance(prev, LZLiteral):
                    yield prev; prev = cur
                else:
                    if prev.distance == cur.distance:
                        prev.length += cur.length
                    else:
                        yield prev; prev = cur
    except StopIteration:
        pass
Merge consecutive LZ operations into single ops if possible.
tools/compress_memory_initializer.py