MCPcopy Index your code
hub / github.com/python/cpython / compare

Method compare

Lib/difflib.py:836–875  ·  view source on GitHub ↗

r""" Compare two sequences of lines; generate the resulting delta. Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the `readlines()` method of file-like objects. The delta generated also consists of

(self, a, b)

Source from the content-addressed store, hash-verified

834 self.charjunk = charjunk
835
836 def compare(self, a, b):
837 r"""
838 Compare two sequences of lines; generate the resulting delta.
839
840 Each sequence must contain individual single-line strings ending with
841 newlines. Such sequences can be obtained from the `readlines()` method
842 of file-like objects. The delta generated also consists of newline-
843 terminated strings, ready to be printed as-is via the writelines()
844 method of a file-like object.
845
846 Example:
847
848 >>> print(''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(True),
849 ... 'ore\ntree\nemu\n'.splitlines(True))),
850 ... end="")
851 - one
852 ? ^
853 + ore
854 ? ^
855 - two
856 - three
857 ? -
858 + tree
859 + emu
860 """
861
862 cruncher = SequenceMatcher(self.linejunk, a, b)
863 for tag, alo, ahi, blo, bhi in cruncher.get_opcodes():
864 if tag == 'replace':
865 g = self._fancy_replace(a, alo, ahi, b, blo, bhi)
866 elif tag == 'delete':
867 g = self._dump('-', a, alo, ahi)
868 elif tag == 'insert':
869 g = self._dump('+', b, blo, bhi)
870 elif tag == 'equal':
871 g = self._dump(' ', a, alo, ahi)
872 else:
873 raise ValueError('unknown tag %r' % (tag,))
874
875 yield from g
876
877 def _dump(self, tag, x, lo, hi):
878 """Generate comparison results for a same-tagged range."""

Callers 2

output_differenceMethod · 0.95
ndiffFunction · 0.45

Calls 4

get_opcodesMethod · 0.95
_fancy_replaceMethod · 0.95
_dumpMethod · 0.95
SequenceMatcherClass · 0.85

Tested by 1

output_differenceMethod · 0.76