• Home
  • Raw
  • Download

Lines Matching +full:diff +full:- +full:sequences

1 :mod:`difflib` --- Helpers for computing deltas
18 --------------
20 This module provides classes and functions for comparing sequences. It
29 This is a flexible class for comparing pairs of sequences of any type, so long
37 idea is then applied recursively to the pieces of the sequences to the left and
39 sequences, but does tend to yield matches that "look right" to people.
41 **Timing:** The basic Ratcliff-Obershelp algorithm is cubic time in the worst
43 quadratic time for the worst case and has expected-case behavior dependent in a
44 complicated way on how many elements the sequences have in common; best case
61 This is a class for comparing sequences of lines of text, and producing
62 human-readable differences or deltas. Differ uses :class:`SequenceMatcher`
63 both to compare sequences of lines, and to compare sequences of characters
64 within similar (near-matching) lines.
66 Each line of a :class:`Differ` delta begins with a two-letter code:
68 +----------+-------------------------------------------+
71 | ``'- '`` | line unique to sequence 1 |
72 +----------+-------------------------------------------+
74 +----------+-------------------------------------------+
75 | ``' '`` | line common to both sequences |
76 +----------+-------------------------------------------+
78 +----------+-------------------------------------------+
82 the sequences contain tab characters.
89 with inter-line and intra-line change highlights. The table can be generated in
112 numlines=5, *, charset='utf-8')
116 inter-line and intra-line changes highlighted.
136 *charset* keyword-only argument was added. The default charset of
137 HTML document changed from ``'ISO-8859-1'`` to ``'utf-8'``.
142 is a complete HTML table showing line by line differences with inter-line and
143 intra-line changes highlighted.
148 :file:`Tools/scripts/diff.py` is a command-line front-end to this class and
155 generating the delta lines) in context diff format.
161 By default, the diff control lines (those with ``***`` or ``---``) are created
170 The context diff format normally has a header for filenames and modification
180 --- after.py
187 --- 1,4 ----
193 See :ref:`difflib-interface` for a more detailed example.
200 sequences against which to match *word* (typically a list of strings).
224 Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style
232 is also a module-level function :func:`IS_LINE_JUNK`, which filters out lines
234 -- however the underlying :class:`SequenceMatcher` class does a dynamic
239 returns if the character is junk, or false if not. The default is module-level
243 :file:`Tools/scripts/ndiff.py` is a command-line front-end to this function.
245 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
247 >>> print(''.join(diff), end="")
248 - one
252 - two
253 - three
254 ? -
261 Return one of the two sequences that generated a delta.
269 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(keepends=True),
271 >>> diff = list(diff) # materialize the generated delta into a list
272 >>> print(''.join(restore(diff, 1)), end="")
276 >>> print(''.join(restore(diff, 2)), end="")
285 generating the delta lines) in unified diff format.
292 By default, the diff control lines (those with ``---``, ``+++``, or ``@@``) are
301 The context diff format normally has a header for filenames and modification
311 --- before.py
313 @@ -1,4 +1,4 @@
314 -bacon
315 -eggs
316 -ham
322 See :ref:`difflib-interface` for a more detailed example.
356 …g: The Gestalt Approach <https://www.drdobbs.com/database/pattern-matching-the-gestalt-approach/18…
361 .. _sequence-matcher:
364 -----------------------
371 Optional argument *isjunk* must be ``None`` (the default) or a one-argument
379 if you're comparing lines as sequences of characters, and don't want to synch up
382 The optional arguments *a* and *b* are sequences to be compared; both default to
383 empty strings. The elements of both sequences must be :term:`hashable`.
393 non-junk elements considered popular by the heuristic (if it is not
405 Set the two sequences to be compared.
409 sequences, use :meth:`set_seq2` to set the commonly used sequence once and
410 call :meth:`set_seq1` repeatedly, once for each of the other sequences.
468 Return list of triples describing non-overlapping matching subsequences.
477 triples always describe non-adjacent equal blocks.
490 Return list of 5-tuples describing how to turn *a* into *b*. Each tuple is
497 +---------------+---------------------------------------------+
502 +---------------+---------------------------------------------+
505 +---------------+---------------------------------------------+
509 +---------------+---------------------------------------------+
510 | ``'equal'`` | ``a[i1:i2] == b[j1:j2]`` (the sub-sequences |
512 +---------------+---------------------------------------------+
520 ... print('{:7} a[{}:{}] --> b[{}:{}] {!r:>8} --> {!r}'.format(
522 delete a[0:1] --> b[0:0] 'q' --> ''
523 equal a[1:3] --> b[0:2] 'ab' --> 'ab'
524 replace a[3:4] --> b[2:3] 'x' --> 'y'
525 equal a[4:6] --> b[3:5] 'cd' --> 'cd'
526 insert a[6:6] --> b[5:6] '' --> 'f'
542 Return a measure of the sequences' similarity as a float in the range [0,
545 Where T is the total number of elements in both sequences, and M is the
547 sequences are identical, and ``0.0`` if they have nothing in common.
589 .. _sequencematcher-examples:
592 ------------------------
601 sequences. As a rule of thumb, a :meth:`ratio` value over 0.6 means the
602 sequences are close matches:
607 If you're only interested in where the sequences match,
640 .. _differ-objects:
643 --------------
645 Note that :class:`Differ`\ -generated deltas make no claim to be **minimal**
646 diffs. To the contrary, minimal diffs are often counter-intuitive, because they
649 locality, at the occasional cost of producing a longer diff.
668 These junk-filtering functions speed up matching to find
679 Compare two sequences of lines, and generate the delta (a sequence of lines).
681 Each sequence must contain individual single-line strings ending with
682 newlines. Such sequences can be obtained from the
683 :meth:`~io.IOBase.readlines` method of file-like objects. The delta
684 generated also consists of newline-terminated strings, ready to be
685 printed as-is via the :meth:`~io.IOBase.writelines` method of a
686 file-like object.
689 .. _differ-examples:
692 --------------
694 This example compares two texts. First we set up the texts, sequences of
695 individual single-line strings ending with newlines (such sequences can also be
696 obtained from the :meth:`~io.BaseIO.readlines` method of file-like objects):
705 >>> text1[0][-1]
725 ``result`` is a list of strings, so let's pretty-print it:
730 '- 2. Explicit is better than implicit.\n',
731 '- 3. Simple is better than complex.\n',
734 '- 4. Complex is better than complicated.\n',
735 '? ^ ---- ^\n',
740 As a single multi-line string it looks like this:
745 - 2. Explicit is better than implicit.
746 - 3. Simple is better than complex.
749 - 4. Complex is better than complicated.
750 ? ^ ---- ^
756 .. _difflib-interface:
758 A command-line interface to difflib
759 -----------------------------------
761 This example shows how to use difflib to create a ``diff``-like utility.
763 :file:`Tools/scripts/diff.py`.
765 .. literalinclude:: ../../Tools/scripts/diff.py