def compress(a, b):
    """Performs the *compressed* diff of `a` and `b` such that the original
    contents of the :func:`difflib.ndiff` call can be reconstructed using
    :func:`~acorn.logging.diff.restore`.

    Args:
        a (str or list): *original* string or list of strings to diff.
        b (str or list): *edited* string or list of strings to diff.
    """
    from difflib import ndiff
    left = a.splitlines(1) if isinstance(a, string_types) else a
    right = b.splitlines(1) if isinstance(b, string_types) else b
    ldiff = list(ndiff(left, right))
    
    result = {}
    latest = None   
    combo = None
    icombo = 0
    iorig = 0
    
    for i, line in enumerate(ldiff):
        cs = [l[0] for l in ldiff[i:min((i+4, len(ldiff)))]]
        if cs[0] != ' ':
            #Initialize a new entry in the diff list.
            if latest is None:
                latest = iorig
                result[latest] = []
                
            #We have to be careful. At a minimum, there may be a '-' or a '+' when the lines are 
            #completely added or deleted. When they are *altered*, then we also expect one or
            #more '?' lines showing the differences.
            if combo is None:
                if cs[0] == '-':
                    #Check whether the next lines have one of these combinations:
                    if (len(cs) >=3 and cs[1] == '+' and cs[2] == '?'):
                        combo = 3
                    elif (len(cs) >= 4 and cs[1] == '?' and cs[2] == '+'
                          and cs[3] == '?'):
                        combo = 4
                    else:
                        #This is a stand-alone deletion.
                        combo = 1
                elif cs[0] == '+':
                    #This is for the stand-alone addition.
                    combo = 1
                
            if icombo < combo:
                result[latest].append(line)
                icombo += 1
            
            if icombo == combo:
                if combo > 1:
                    latest = None
                combo = None
                icombo = 0
                if cs[0] != '+':
                    iorig += 1
        else:
            latest = None
            iorig += 1

    return result
def compress ( a , b ) : from difflib import ndiff left = a . splitlines ( 1 ) if isinstance ( a , string_types ) else a right = b . splitlines ( 1 ) if isinstance ( b , string_types ) else b ldiff = list ( ndiff ( left , right ) ) result = { } latest = None combo = None icombo = 0 iorig = 0 for i , line in enumerate ( ldiff ) : cs = [ l [ 0 ] for l in ldiff [ i : min ( ( i + 4 , len ( ldiff ) ) ) ] ] if cs [ 0 ] != ' ' : if latest is None : latest = iorig result [ latest ] = [ ] if combo is None : if cs [ 0 ] == '-' : if ( len ( cs ) >= 3 and cs [ 1 ] == '+' and cs [ 2 ] == '?' ) : combo = 3 elif ( len ( cs ) >= 4 and cs [ 1 ] == '?' and cs [ 2 ] == '+' and cs [ 3 ] == '?' ) : combo = 4 else : combo = 1 elif cs [ 0 ] == '+' : combo = 1 if icombo < combo : result [ latest ] . append ( line ) icombo += 1 if icombo == combo : if combo > 1 : latest = None combo = None icombo = 0 if cs [ 0 ] != '+' : iorig += 1 else : latest = None iorig += 1 return result
Performs the *compressed* diff of `a` and `b` such that the original
    contents of the :func:`difflib.ndiff` call can be reconstructed using
    :func:`~acorn.logging.diff.restore`.

    Args:
        a (str or list): *original* string or list of strings to diff.
        b (str or list): *edited* string or list of strings to diff.