Lines Matching +full:diff +full:- +full:sequences
1 :mod:`difflib` --- Helpers for computing deltas
17 This module provides classes and functions for comparing sequences. It
24 This is a flexible class for comparing pairs of sequences of any type, so long
30 idea is then applied recursively to the pieces of the sequences to the left and
32 sequences, but does tend to yield matches that "look right" to people.
34 **Timing:** The basic Ratcliff-Obershelp algorithm is cubic time in the worst
36 quadratic time for the worst case and has expected-case behavior dependent in a
37 complicated way on how many elements the sequences have in common; best case
53 This is a class for comparing sequences of lines of text, and producing
54 human-readable differences or deltas. Differ uses :class:`SequenceMatcher`
55 both to compare sequences of lines, and to compare sequences of characters
56 within similar (near-matching) lines.
58 Each line of a :class:`Differ` delta begins with a two-letter code:
60 +----------+-------------------------------------------+
63 | ``'- '`` | line unique to sequence 1 |
64 +----------+-------------------------------------------+
66 +----------+-------------------------------------------+
67 | ``' '`` | line common to both sequences |
68 +----------+-------------------------------------------+
70 +----------+-------------------------------------------+
74 the sequences contain tab characters.
81 with inter-line and intra-line change highlights. The table can be generated in
108 inter-line and intra-line changes highlighted.
127 is a complete HTML table showing line by line differences with inter-line and
128 intra-line changes highlighted.
133 :file:`Tools/scripts/diff.py` is a command-line front-end to this class and
142 generating the delta lines) in context diff format.
148 By default, the diff control lines (those with ``***`` or ``---``) are created
157 The context diff format normally has a header for filenames and modification
168 --- after.py
175 --- 1,4 ----
181 See :ref:`difflib-interface` for a more detailed example.
190 sequences against which to match *word* (typically a list of strings).
214 Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style
222 Python 2.3. Before then, the default was the module-level function
226 frequent as to constitute noise, and this usually works better than the pre-2.3
230 returns if the character is junk, or false if not. The default is module-level
234 :file:`Tools/scripts/ndiff.py` is a command-line front-end to this function.
236 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
238 >>> print ''.join(diff),
239 - one
243 - two
244 - three
245 ? -
252 Return one of the two sequences that generated a delta.
260 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
262 >>> diff = list(diff) # materialize the generated delta into a list
263 >>> print ''.join(restore(diff, 1)),
267 >>> print ''.join(restore(diff, 2)),
276 generating the delta lines) in unified diff format.
283 By default, the diff control lines (those with ``---``, ``+++``, or ``@@``) are
292 The context diff format normally has a header for filenames and modification
302 --- before.py
304 @@ -1,4 +1,4 @@
305 -bacon
306 -eggs
307 -ham
313 See :ref:`difflib-interface` for a more detailed example.
334 …ng: The Gestalt Approach <http://www.drdobbs.com/database/pattern-matching-the-gestalt-approach/18…
339 .. _sequence-matcher:
342 -----------------------
349 Optional argument *isjunk* must be ``None`` (the default) or a one-argument
357 if you're comparing lines as sequences of characters, and don't want to synch up
360 The optional arguments *a* and *b* are sequences to be compared; both default to
361 empty strings. The elements of both sequences must be :term:`hashable`.
373 Set the two sequences to be compared.
377 sequences, use :meth:`set_seq2` to set the commonly used sequence once and
378 call :meth:`set_seq1` repeatedly, once for each of the other sequences.
442 triples always describe non-adjacent equal blocks.
447 The guarantee that adjacent triples always describe non-adjacent blocks
459 Return list of 5-tuples describing how to turn *a* into *b*. Each tuple is
466 +---------------+---------------------------------------------+
471 +---------------+---------------------------------------------+
474 +---------------+---------------------------------------------+
478 +---------------+---------------------------------------------+
479 | ``'equal'`` | ``a[i1:i2] == b[j1:j2]`` (the sub-sequences |
481 +---------------+---------------------------------------------+
513 Return a measure of the sequences' similarity as a float in the range [0,
516 Where T is the total number of elements in both sequences, and M is the
518 sequences are identical, and ``0.0`` if they have nothing in common.
550 .. _sequencematcher-examples:
553 ------------------------
562 sequences. As a rule of thumb, a :meth:`ratio` value over 0.6 means the
563 sequences are close matches:
568 If you're only interested in where the sequences match,
601 .. _differ-objects:
604 --------------
606 Note that :class:`Differ`\ -generated deltas make no claim to be **minimal**
607 diffs. To the contrary, minimal diffs are often counter-intuitive, because they
610 locality, at the occasional cost of producing a longer diff.
633 Compare two sequences of lines, and generate the delta (a sequence of lines).
635 Each sequence must contain individual single-line strings ending with
636 newlines. Such sequences can be obtained from the
637 :meth:`~file.readlines` method of file-like objects. The delta
638 generated also consists of newline-terminated strings, ready to be
639 printed as-is via the :meth:`~file.writelines` method of a
640 file-like object.
643 .. _differ-examples:
646 --------------
648 This example compares two texts. First we set up the texts, sequences of
649 individual single-line strings ending with newlines (such sequences can also be
650 obtained from the :meth:`~file.readlines` method of file-like objects):
659 >>> text1[0][-1]
679 ``result`` is a list of strings, so let's pretty-print it:
684 '- 2. Explicit is better than implicit.\n',
685 '- 3. Simple is better than complex.\n',
688 '- 4. Complex is better than complicated.\n',
689 '? ^ ---- ^\n',
694 As a single multi-line string it looks like this:
699 - 2. Explicit is better than implicit.
700 - 3. Simple is better than complex.
703 - 4. Complex is better than complicated.
704 ? ^ ---- ^
710 .. _difflib-interface:
712 A command-line interface to difflib
713 -----------------------------------
715 This example shows how to use difflib to create a ``diff``-like utility.
717 :file:`Tools/scripts/diff.py`.
736 parser.add_option("-c", action="store_true", default=False,
737 help='Produce a context format diff (default)')
738 parser.add_option("-u", action="store_true", default=False,
739 help='Produce a unified format diff')
740 hlp = 'Produce HTML side by side diff (can use -c and -l in conjunction)'
741 parser.add_option("-m", action="store_true", default=False, help=hlp)
742 parser.add_option("-n", action="store_true", default=False,
743 help='Produce a ndiff format diff')
744 parser.add_option("-l", "--lines", type="int", default=3,
757 # we're passing these as arguments to the diff function
766 diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
769 diff = difflib.ndiff(fromlines, tolines)
771 diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile,
775 diff = difflib.context_diff(fromlines, tolines, fromfile, tofile,
778 # we're using writelines because diff is a generator
779 sys.stdout.writelines(diff)