MCPcopy Index your code
hub / github.com/RustPython/RustPython / compare

Method compare

Lib/difflib.py:833–872  ·  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

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