Skip to content

Instantly share code, notes, and snippets.

@mrshu
Last active December 23, 2020 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrshu/c8bb4d99edcd470cdb6a7525f7146af0 to your computer and use it in GitHub Desktop.
Save mrshu/c8bb4d99edcd470cdb6a7525f7146af0 to your computer and use it in GitHub Desktop.
import difflib
def visualize_diff(a, b):
seqm = difflib.SequenceMatcher(None, a, b)
output= []
for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
if opcode == 'equal':
output.append(seqm.a[a0:a1])
elif opcode == 'insert':
output.append(green)
output.append(seqm.b[b0:b1])
output.append(default)
elif opcode == 'delete':
output.append(red)
output.append(seqm.a[a0:a1])
output.append(default)
elif opcode == 'replace':
output.append(b_green)
output.append(seqm.b[a0:a1])
output.append(b_red)
output.append(seqm.a[a0:a1])
output.append(default)
else:
raise RuntimeError
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment