import difflib


def comp_strs_diff_similarity(str1, str2):
    """
    Compare strings: Diff similarity:
    Return a diff sequence similarity metric [0-1] for two strings.

    Similarity = 2 * matches / total elements.
    Uses difflib.SequenceMatcher.ratio.

    NOTES:
    -  The ratio calculation is influenced by difflib's junk and autojunk settings, which by default filters whitespace and supresses excessive duplicates.
    -  Calculating ratios is very slow for unsplit paragraph text. It performs much better for line splits or word splits.
    """
    str1 = str1.split()  ## difflib struggles with paragraphs -- works better on lines
    str2 = str2.split()
    seqcomp = difflib.SequenceMatcher(lambda x: x == " ", str1, str2)  ## run on partial files for greatly increased speed e.g. str1[0:512], str2[0:512] -- this will impact ratio for boilerplate-heavy openings.
    seqratio = round(seqcomp.quick_ratio(), 2)  ## .quick_ratio() and .real_quick_ratio() are **much** faster than .ratio(), but unusably imprecise on paragraph text, e.g. 0.08 ratio = 0.98 quick_ratio similarity. Much better with line splits.
    return seqratio