<jupyter_start><jupyter_text>⊕ [6.3. difflib — Helpers for computing deltas — Python 3.6.8 documentation](https://docs.python.org/3.6/library/difflib.html)<jupyter_code>from difflib import SequenceMatcher, context_diff
s = SequenceMatcher(None, "abcd", "bcde")
print(s.ratio(), s.quick_ratio(), s.real_quick_ratio())
import sys
s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
sys.stdout.writelines(context_diff(s1, s2, fromfile='before.py', tofile='after.py'))
from difflib import *
get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
>>> import keyword
>>> get_close_matches('wheel', keyword.kwlist)
>>> get_close_matches('accept', keyword.kwlist)
diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
             'ore\ntree\nemu\n'.splitlines(keepends=True))
print(''.join(diff), end="")
>>> s = SequenceMatcher(None, "abxcd", "abcd")
>>> s.get_matching_blocks()
a = "qabxcd"
b = "abycdf"
s = SequenceMatcher(None, a, b)
for tag, i1, i2, j1, j2 in s.get_opcodes():
    print('{:7}   a[{}:{}] --> b[{}:{}] {!r:>8} --> {!r}'.format(
        tag, i1, i2, j1, j2, a[i1:i2], b[j1:j2]))
s = SequenceMatcher(lambda x: x == " ",
                    "private Thread currentThread;",
                    "private volatile Thread currentThread;")
print(round(s.ratio(), 3))
text1 = '''  1. Beautiful is better than ugly.
  2. Explicit is better than implicit.
  3. Simple is better than complex.
  4. Complex is better than complicated.
'''.splitlines(keepends=True)
text2 = '''  1. Beautiful is better than ugly.
  3.   Simple is better than complex.
  4. Complicated is better than complex.
  5. Flat is better than nested.
'''.splitlines(keepends=True)

d = Differ()
result = list(d.compare(text1, text2))

from pprint import pprint
pprint(result)
>>> import sys
>>> sys.stdout.writelines(result)<jupyter_output>1. Beautiful is better than ugly.
-   2. Explicit is better than implicit.
-   3. Simple is better than complex.
+   3.   Simple is better than complex.
?     ++
-   4. Complex is better than complicated.
?            ^                     ---- ^
+   4. Complicated is better than complex.
?           ++++ ^                      ^
+   5. Flat is better than nested.
{'fraction_non_alphanumeric': 0.17224453369031684, 'fraction_numerical': 0.020080321285140562, 'mean_word_length': 3.4133858267716537, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '363734', 'license': 'Apache-2.0', 'n_tokens_mistral': 808, 'n_tokens_neox': 787, 'n_words': 210, 'path': 'notebook/procs-diff.ipynb', 'repo_name': 'samlet/stack', 'size': '2241'}