blob: 585fa74bfd1a8fc72ba1f4f960878e966d42d019 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`difflib` --- Helpers for computing deltas
2===============================================
3
4.. module:: difflib
5 :synopsis: Helpers for computing differences between objects.
6.. moduleauthor:: Tim Peters <tim_one@users.sourceforge.net>
7.. sectionauthor:: Tim Peters <tim_one@users.sourceforge.net>
Christian Heimes5b5e81c2007-12-31 16:14:33 +00008.. Markup by Fred L. Drake, Jr. <fdrake@acm.org>
Georg Brandl116aa622007-08-15 14:28:22 +00009
Christian Heimesfe337bf2008-03-23 21:54:12 +000010.. testsetup::
Georg Brandl116aa622007-08-15 14:28:22 +000011
Christian Heimesfe337bf2008-03-23 21:54:12 +000012 import sys
13 from difflib import *
Georg Brandl116aa622007-08-15 14:28:22 +000014
Georg Brandl9afde1c2007-11-01 20:32:30 +000015This module provides classes and functions for comparing sequences. It
16can be used for example, for comparing files, and can produce difference
17information in various formats, including HTML and context and unified
18diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
19
Georg Brandl116aa622007-08-15 14:28:22 +000020.. class:: SequenceMatcher
21
22 This is a flexible class for comparing pairs of sequences of any type, so long
Guido van Rossum2cc30da2007-11-02 23:46:40 +000023 as the sequence elements are :term:`hashable`. The basic algorithm predates, and is a
Georg Brandl116aa622007-08-15 14:28:22 +000024 little fancier than, an algorithm published in the late 1980's by Ratcliff and
25 Obershelp under the hyperbolic name "gestalt pattern matching." The idea is to
26 find the longest contiguous matching subsequence that contains no "junk"
27 elements (the Ratcliff and Obershelp algorithm doesn't address junk). The same
28 idea is then applied recursively to the pieces of the sequences to the left and
29 to the right of the matching subsequence. This does not yield minimal edit
30 sequences, but does tend to yield matches that "look right" to people.
31
32 **Timing:** The basic Ratcliff-Obershelp algorithm is cubic time in the worst
33 case and quadratic time in the expected case. :class:`SequenceMatcher` is
34 quadratic time for the worst case and has expected-case behavior dependent in a
35 complicated way on how many elements the sequences have in common; best case
36 time is linear.
37
38
39.. class:: Differ
40
41 This is a class for comparing sequences of lines of text, and producing
42 human-readable differences or deltas. Differ uses :class:`SequenceMatcher`
43 both to compare sequences of lines, and to compare sequences of characters
44 within similar (near-matching) lines.
45
46 Each line of a :class:`Differ` delta begins with a two-letter code:
47
48 +----------+-------------------------------------------+
49 | Code | Meaning |
50 +==========+===========================================+
51 | ``'- '`` | line unique to sequence 1 |
52 +----------+-------------------------------------------+
53 | ``'+ '`` | line unique to sequence 2 |
54 +----------+-------------------------------------------+
55 | ``' '`` | line common to both sequences |
56 +----------+-------------------------------------------+
57 | ``'? '`` | line not present in either input sequence |
58 +----------+-------------------------------------------+
59
60 Lines beginning with '``?``' attempt to guide the eye to intraline differences,
61 and were not present in either input sequence. These lines can be confusing if
62 the sequences contain tab characters.
63
64
65.. class:: HtmlDiff
66
67 This class can be used to create an HTML table (or a complete HTML file
68 containing the table) showing a side by side, line by line comparison of text
69 with inter-line and intra-line change highlights. The table can be generated in
70 either full or contextual difference mode.
71
72 The constructor for this class is:
73
74
75 .. function:: __init__([tabsize][, wrapcolumn][, linejunk][, charjunk])
76
77 Initializes instance of :class:`HtmlDiff`.
78
79 *tabsize* is an optional keyword argument to specify tab stop spacing and
80 defaults to ``8``.
81
82 *wrapcolumn* is an optional keyword to specify column number where lines are
83 broken and wrapped, defaults to ``None`` where lines are not wrapped.
84
85 *linejunk* and *charjunk* are optional keyword arguments passed into ``ndiff()``
86 (used by :class:`HtmlDiff` to generate the side by side HTML differences). See
87 ``ndiff()`` documentation for argument default values and descriptions.
88
89 The following methods are public:
90
91
92 .. function:: make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
93
94 Compares *fromlines* and *tolines* (lists of strings) and returns a string which
95 is a complete HTML file containing a table showing line by line differences with
96 inter-line and intra-line changes highlighted.
97
98 *fromdesc* and *todesc* are optional keyword arguments to specify from/to file
99 column header strings (both default to an empty string).
100
101 *context* and *numlines* are both optional keyword arguments. Set *context* to
102 ``True`` when contextual differences are to be shown, else the default is
103 ``False`` to show the full files. *numlines* defaults to ``5``. When *context*
104 is ``True`` *numlines* controls the number of context lines which surround the
105 difference highlights. When *context* is ``False`` *numlines* controls the
106 number of lines which are shown before a difference highlight when using the
107 "next" hyperlinks (setting to zero would cause the "next" hyperlinks to place
108 the next difference highlight at the top of the browser without any leading
109 context).
110
111
112 .. function:: make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
113
114 Compares *fromlines* and *tolines* (lists of strings) and returns a string which
115 is a complete HTML table showing line by line differences with inter-line and
116 intra-line changes highlighted.
117
118 The arguments for this method are the same as those for the :meth:`make_file`
119 method.
120
121 :file:`Tools/scripts/diff.py` is a command-line front-end to this class and
122 contains a good example of its use.
123
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125.. function:: context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
126
Georg Brandl9afde1c2007-11-01 20:32:30 +0000127 Compare *a* and *b* (lists of strings); return a delta (a :term:`generator`
128 generating the delta lines) in context diff format.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130 Context diffs are a compact way of showing just the lines that have changed plus
131 a few lines of context. The changes are shown in a before/after style. The
132 number of context lines is set by *n* which defaults to three.
133
134 By default, the diff control lines (those with ``***`` or ``---``) are created
135 with a trailing newline. This is helpful so that inputs created from
136 :func:`file.readlines` result in diffs that are suitable for use with
137 :func:`file.writelines` since both the inputs and outputs have trailing
138 newlines.
139
140 For inputs that do not have trailing newlines, set the *lineterm* argument to
141 ``""`` so that the output will be uniformly newline free.
142
143 The context diff format normally has a header for filenames and modification
144 times. Any or all of these may be specified using strings for *fromfile*,
145 *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
146 expressed in the format returned by :func:`time.ctime`. If not specified, the
147 strings default to blanks.
148
Christian Heimes8640e742008-02-23 16:23:06 +0000149 >>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
150 >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
151 >>> for line in context_diff(s1, s2, fromfile='before.py', tofile='after.py'):
Christian Heimesfe337bf2008-03-23 21:54:12 +0000152 ... sys.stdout.write(line) # doctest: +NORMALIZE_WHITESPACE
Christian Heimes8640e742008-02-23 16:23:06 +0000153 *** before.py
154 --- after.py
155 ***************
156 *** 1,4 ****
157 ! bacon
158 ! eggs
159 ! ham
160 guido
161 --- 1,4 ----
162 ! python
163 ! eggy
164 ! hamster
165 guido
166
167 See :ref:`difflib-interface` for a more detailed example.
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170.. function:: get_close_matches(word, possibilities[, n][, cutoff])
171
172 Return a list of the best "good enough" matches. *word* is a sequence for which
173 close matches are desired (typically a string), and *possibilities* is a list of
174 sequences against which to match *word* (typically a list of strings).
175
176 Optional argument *n* (default ``3``) is the maximum number of close matches to
177 return; *n* must be greater than ``0``.
178
179 Optional argument *cutoff* (default ``0.6``) is a float in the range [0, 1].
180 Possibilities that don't score at least that similar to *word* are ignored.
181
182 The best (no more than *n*) matches among the possibilities are returned in a
Christian Heimesfe337bf2008-03-23 21:54:12 +0000183 list, sorted by similarity score, most similar first.
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185 >>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
186 ['apple', 'ape']
187 >>> import keyword
188 >>> get_close_matches('wheel', keyword.kwlist)
189 ['while']
190 >>> get_close_matches('apple', keyword.kwlist)
191 []
192 >>> get_close_matches('accept', keyword.kwlist)
193 ['except']
194
195
196.. function:: ndiff(a, b[, linejunk][, charjunk])
197
Georg Brandl9afde1c2007-11-01 20:32:30 +0000198 Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style
199 delta (a :term:`generator` generating the delta lines).
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201 Optional keyword parameters *linejunk* and *charjunk* are for filter functions
202 (or ``None``):
203
204 *linejunk*: A function that accepts a single string argument, and returns true
205 if the string is junk, or false if not. The default is (``None``), starting with
206 Python 2.3. Before then, the default was the module-level function
207 :func:`IS_LINE_JUNK`, which filters out lines without visible characters, except
208 for at most one pound character (``'#'``). As of Python 2.3, the underlying
209 :class:`SequenceMatcher` class does a dynamic analysis of which lines are so
210 frequent as to constitute noise, and this usually works better than the pre-2.3
211 default.
212
213 *charjunk*: A function that accepts a character (a string of length 1), and
214 returns if the character is junk, or false if not. The default is module-level
215 function :func:`IS_CHARACTER_JUNK`, which filters out whitespace characters (a
216 blank or tab; note: bad idea to include newline in this!).
217
Christian Heimesfe337bf2008-03-23 21:54:12 +0000218 :file:`Tools/scripts/ndiff.py` is a command-line front-end to this function.
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
221 ... 'ore\ntree\nemu\n'.splitlines(1))
Georg Brandl6911e3c2007-09-04 07:15:32 +0000222 >>> print(''.join(diff), end="")
Georg Brandl116aa622007-08-15 14:28:22 +0000223 - one
224 ? ^
225 + ore
226 ? ^
227 - two
228 - three
229 ? -
230 + tree
231 + emu
232
233
234.. function:: restore(sequence, which)
235
236 Return one of the two sequences that generated a delta.
237
238 Given a *sequence* produced by :meth:`Differ.compare` or :func:`ndiff`, extract
239 lines originating from file 1 or 2 (parameter *which*), stripping off line
240 prefixes.
241
Christian Heimesfe337bf2008-03-23 21:54:12 +0000242 Example:
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
245 ... 'ore\ntree\nemu\n'.splitlines(1))
246 >>> diff = list(diff) # materialize the generated delta into a list
Georg Brandl6911e3c2007-09-04 07:15:32 +0000247 >>> print(''.join(restore(diff, 1)), end="")
Georg Brandl116aa622007-08-15 14:28:22 +0000248 one
249 two
250 three
Georg Brandl6911e3c2007-09-04 07:15:32 +0000251 >>> print(''.join(restore(diff, 2)), end="")
Georg Brandl116aa622007-08-15 14:28:22 +0000252 ore
253 tree
254 emu
255
256
257.. function:: unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
258
Georg Brandl9afde1c2007-11-01 20:32:30 +0000259 Compare *a* and *b* (lists of strings); return a delta (a :term:`generator`
260 generating the delta lines) in unified diff format.
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262 Unified diffs are a compact way of showing just the lines that have changed plus
263 a few lines of context. The changes are shown in a inline style (instead of
264 separate before/after blocks). The number of context lines is set by *n* which
265 defaults to three.
266
267 By default, the diff control lines (those with ``---``, ``+++``, or ``@@``) are
268 created with a trailing newline. This is helpful so that inputs created from
269 :func:`file.readlines` result in diffs that are suitable for use with
270 :func:`file.writelines` since both the inputs and outputs have trailing
271 newlines.
272
273 For inputs that do not have trailing newlines, set the *lineterm* argument to
274 ``""`` so that the output will be uniformly newline free.
275
276 The context diff format normally has a header for filenames and modification
277 times. Any or all of these may be specified using strings for *fromfile*,
278 *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
279 expressed in the format returned by :func:`time.ctime`. If not specified, the
280 strings default to blanks.
281
Christian Heimes8640e742008-02-23 16:23:06 +0000282
283 >>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
284 >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
285 >>> for line in unified_diff(s1, s2, fromfile='before.py', tofile='after.py'):
Christian Heimesfe337bf2008-03-23 21:54:12 +0000286 ... sys.stdout.write(line) # doctest: +NORMALIZE_WHITESPACE
Christian Heimes8640e742008-02-23 16:23:06 +0000287 --- before.py
288 +++ after.py
289 @@ -1,4 +1,4 @@
290 -bacon
291 -eggs
292 -ham
293 +python
294 +eggy
295 +hamster
296 guido
297
298 See :ref:`difflib-interface` for a more detailed example.
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Georg Brandl116aa622007-08-15 14:28:22 +0000300
301.. function:: IS_LINE_JUNK(line)
302
303 Return true for ignorable lines. The line *line* is ignorable if *line* is
304 blank or contains a single ``'#'``, otherwise it is not ignorable. Used as a
305 default for parameter *linejunk* in :func:`ndiff` before Python 2.3.
306
307
308.. function:: IS_CHARACTER_JUNK(ch)
309
310 Return true for ignorable characters. The character *ch* is ignorable if *ch*
311 is a space or tab, otherwise it is not ignorable. Used as a default for
312 parameter *charjunk* in :func:`ndiff`.
313
314
315.. seealso::
316
317 `Pattern Matching: The Gestalt Approach <http://www.ddj.com/184407970?pgno=5>`_
318 Discussion of a similar algorithm by John W. Ratcliff and D. E. Metzener. This
319 was published in `Dr. Dobb's Journal <http://www.ddj.com/>`_ in July, 1988.
320
321
322.. _sequence-matcher:
323
324SequenceMatcher Objects
325-----------------------
326
327The :class:`SequenceMatcher` class has this constructor:
328
329
330.. class:: SequenceMatcher([isjunk[, a[, b]]])
331
332 Optional argument *isjunk* must be ``None`` (the default) or a one-argument
333 function that takes a sequence element and returns true if and only if the
334 element is "junk" and should be ignored. Passing ``None`` for *isjunk* is
335 equivalent to passing ``lambda x: 0``; in other words, no elements are ignored.
336 For example, pass::
337
338 lambda x: x in " \t"
339
340 if you're comparing lines as sequences of characters, and don't want to synch up
341 on blanks or hard tabs.
342
343 The optional arguments *a* and *b* are sequences to be compared; both default to
Guido van Rossum2cc30da2007-11-02 23:46:40 +0000344 empty strings. The elements of both sequences must be :term:`hashable`.
Georg Brandl116aa622007-08-15 14:28:22 +0000345
346:class:`SequenceMatcher` objects have the following methods:
347
348
349.. method:: SequenceMatcher.set_seqs(a, b)
350
351 Set the two sequences to be compared.
352
353:class:`SequenceMatcher` computes and caches detailed information about the
354second sequence, so if you want to compare one sequence against many sequences,
355use :meth:`set_seq2` to set the commonly used sequence once and call
356:meth:`set_seq1` repeatedly, once for each of the other sequences.
357
358
359.. method:: SequenceMatcher.set_seq1(a)
360
361 Set the first sequence to be compared. The second sequence to be compared is
362 not changed.
363
364
365.. method:: SequenceMatcher.set_seq2(b)
366
367 Set the second sequence to be compared. The first sequence to be compared is
368 not changed.
369
370
371.. method:: SequenceMatcher.find_longest_match(alo, ahi, blo, bhi)
372
373 Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``.
374
Christian Heimes25bb7832008-01-11 16:17:00 +0000375 If *isjunk* was omitted or ``None``, :meth:`find_longest_match` returns ``(i, j,
Georg Brandl116aa622007-08-15 14:28:22 +0000376 k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo <= i <= i+k <=
377 ahi`` and ``blo <= j <= j+k <= bhi``. For all ``(i', j', k')`` meeting those
378 conditions, the additional conditions ``k >= k'``, ``i <= i'``, and if ``i ==
379 i'``, ``j <= j'`` are also met. In other words, of all maximal matching blocks,
380 return one that starts earliest in *a*, and of all those maximal matching blocks
Christian Heimesfe337bf2008-03-23 21:54:12 +0000381 that start earliest in *a*, return the one that starts earliest in *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000382
383 >>> s = SequenceMatcher(None, " abcd", "abcd abcd")
384 >>> s.find_longest_match(0, 5, 0, 9)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000385 Match(a=0, b=4, size=5)
Georg Brandl116aa622007-08-15 14:28:22 +0000386
387 If *isjunk* was provided, first the longest matching block is determined as
388 above, but with the additional restriction that no junk element appears in the
389 block. Then that block is extended as far as possible by matching (only) junk
390 elements on both sides. So the resulting block never matches on junk except as
391 identical junk happens to be adjacent to an interesting match.
392
393 Here's the same example as before, but considering blanks to be junk. That
394 prevents ``' abcd'`` from matching the ``' abcd'`` at the tail end of the second
395 sequence directly. Instead only the ``'abcd'`` can match, and matches the
Christian Heimesfe337bf2008-03-23 21:54:12 +0000396 leftmost ``'abcd'`` in the second sequence:
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398 >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
399 >>> s.find_longest_match(0, 5, 0, 9)
Christian Heimesfe337bf2008-03-23 21:54:12 +0000400 Match(a=1, b=0, size=4)
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402 If no blocks match, this returns ``(alo, blo, 0)``.
403
Benjamin Peterson35e8c462008-04-24 02:34:53 +0000404 This method returns a :term:`named tuple` ``Match(a, b, size)``.
Christian Heimes25bb7832008-01-11 16:17:00 +0000405
Georg Brandl116aa622007-08-15 14:28:22 +0000406
407.. method:: SequenceMatcher.get_matching_blocks()
408
409 Return list of triples describing matching subsequences. Each triple is of the
410 form ``(i, j, n)``, and means that ``a[i:i+n] == b[j:j+n]``. The triples are
411 monotonically increasing in *i* and *j*.
412
413 The last triple is a dummy, and has the value ``(len(a), len(b), 0)``. It is
414 the only triple with ``n == 0``. If ``(i, j, n)`` and ``(i', j', n')`` are
415 adjacent triples in the list, and the second is not the last triple in the list,
416 then ``i+n != i'`` or ``j+n != j'``; in other words, adjacent triples always
417 describe non-adjacent equal blocks.
418
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000419 .. XXX Explain why a dummy is used!
420
Christian Heimesfe337bf2008-03-23 21:54:12 +0000421 .. doctest::
Georg Brandl116aa622007-08-15 14:28:22 +0000422
423 >>> s = SequenceMatcher(None, "abxcd", "abcd")
424 >>> s.get_matching_blocks()
Christian Heimesfe337bf2008-03-23 21:54:12 +0000425 [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]
Georg Brandl116aa622007-08-15 14:28:22 +0000426
427
428.. method:: SequenceMatcher.get_opcodes()
429
430 Return list of 5-tuples describing how to turn *a* into *b*. Each tuple is of
431 the form ``(tag, i1, i2, j1, j2)``. The first tuple has ``i1 == j1 == 0``, and
432 remaining tuples have *i1* equal to the *i2* from the preceding tuple, and,
433 likewise, *j1* equal to the previous *j2*.
434
435 The *tag* values are strings, with these meanings:
436
437 +---------------+---------------------------------------------+
438 | Value | Meaning |
439 +===============+=============================================+
440 | ``'replace'`` | ``a[i1:i2]`` should be replaced by |
441 | | ``b[j1:j2]``. |
442 +---------------+---------------------------------------------+
443 | ``'delete'`` | ``a[i1:i2]`` should be deleted. Note that |
444 | | ``j1 == j2`` in this case. |
445 +---------------+---------------------------------------------+
446 | ``'insert'`` | ``b[j1:j2]`` should be inserted at |
447 | | ``a[i1:i1]``. Note that ``i1 == i2`` in |
448 | | this case. |
449 +---------------+---------------------------------------------+
450 | ``'equal'`` | ``a[i1:i2] == b[j1:j2]`` (the sub-sequences |
451 | | are equal). |
452 +---------------+---------------------------------------------+
453
Christian Heimesfe337bf2008-03-23 21:54:12 +0000454 For example:
Georg Brandl116aa622007-08-15 14:28:22 +0000455
456 >>> a = "qabxcd"
457 >>> b = "abycdf"
458 >>> s = SequenceMatcher(None, a, b)
459 >>> for tag, i1, i2, j1, j2 in s.get_opcodes():
Georg Brandl6911e3c2007-09-04 07:15:32 +0000460 ... print(("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
461 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])))
Georg Brandl116aa622007-08-15 14:28:22 +0000462 delete a[0:1] (q) b[0:0] ()
463 equal a[1:3] (ab) b[0:2] (ab)
464 replace a[3:4] (x) b[2:3] (y)
465 equal a[4:6] (cd) b[3:5] (cd)
466 insert a[6:6] () b[5:6] (f)
467
468
469.. method:: SequenceMatcher.get_grouped_opcodes([n])
470
Georg Brandl9afde1c2007-11-01 20:32:30 +0000471 Return a :term:`generator` of groups with up to *n* lines of context.
Georg Brandl116aa622007-08-15 14:28:22 +0000472
473 Starting with the groups returned by :meth:`get_opcodes`, this method splits out
474 smaller change clusters and eliminates intervening ranges which have no changes.
475
476 The groups are returned in the same format as :meth:`get_opcodes`.
477
Georg Brandl116aa622007-08-15 14:28:22 +0000478
479.. method:: SequenceMatcher.ratio()
480
481 Return a measure of the sequences' similarity as a float in the range [0, 1].
482
483 Where T is the total number of elements in both sequences, and M is the number
484 of matches, this is 2.0\*M / T. Note that this is ``1.0`` if the sequences are
485 identical, and ``0.0`` if they have nothing in common.
486
487 This is expensive to compute if :meth:`get_matching_blocks` or
488 :meth:`get_opcodes` hasn't already been called, in which case you may want to
489 try :meth:`quick_ratio` or :meth:`real_quick_ratio` first to get an upper bound.
490
491
492.. method:: SequenceMatcher.quick_ratio()
493
494 Return an upper bound on :meth:`ratio` relatively quickly.
495
496 This isn't defined beyond that it is an upper bound on :meth:`ratio`, and is
497 faster to compute.
498
499
500.. method:: SequenceMatcher.real_quick_ratio()
501
502 Return an upper bound on :meth:`ratio` very quickly.
503
504 This isn't defined beyond that it is an upper bound on :meth:`ratio`, and is
505 faster to compute than either :meth:`ratio` or :meth:`quick_ratio`.
506
507The three methods that return the ratio of matching to total characters can give
508different results due to differing levels of approximation, although
509:meth:`quick_ratio` and :meth:`real_quick_ratio` are always at least as large as
Christian Heimesfe337bf2008-03-23 21:54:12 +0000510:meth:`ratio`:
Georg Brandl116aa622007-08-15 14:28:22 +0000511
512 >>> s = SequenceMatcher(None, "abcd", "bcde")
513 >>> s.ratio()
514 0.75
515 >>> s.quick_ratio()
516 0.75
517 >>> s.real_quick_ratio()
518 1.0
519
520
521.. _sequencematcher-examples:
522
523SequenceMatcher Examples
524------------------------
525
Christian Heimesfe337bf2008-03-23 21:54:12 +0000526This example compares two strings, considering blanks to be "junk:"
Georg Brandl116aa622007-08-15 14:28:22 +0000527
528 >>> s = SequenceMatcher(lambda x: x == " ",
529 ... "private Thread currentThread;",
530 ... "private volatile Thread currentThread;")
531
532:meth:`ratio` returns a float in [0, 1], measuring the similarity of the
533sequences. As a rule of thumb, a :meth:`ratio` value over 0.6 means the
Christian Heimesfe337bf2008-03-23 21:54:12 +0000534sequences are close matches:
Georg Brandl116aa622007-08-15 14:28:22 +0000535
Georg Brandl6911e3c2007-09-04 07:15:32 +0000536 >>> print(round(s.ratio(), 3))
Georg Brandl116aa622007-08-15 14:28:22 +0000537 0.866
538
539If you're only interested in where the sequences match,
Christian Heimesfe337bf2008-03-23 21:54:12 +0000540:meth:`get_matching_blocks` is handy:
Georg Brandl116aa622007-08-15 14:28:22 +0000541
542 >>> for block in s.get_matching_blocks():
Georg Brandl6911e3c2007-09-04 07:15:32 +0000543 ... print("a[%d] and b[%d] match for %d elements" % block)
Georg Brandl116aa622007-08-15 14:28:22 +0000544 a[0] and b[0] match for 8 elements
Christian Heimesfe337bf2008-03-23 21:54:12 +0000545 a[8] and b[17] match for 21 elements
Georg Brandl116aa622007-08-15 14:28:22 +0000546 a[29] and b[38] match for 0 elements
547
548Note that the last tuple returned by :meth:`get_matching_blocks` is always a
549dummy, ``(len(a), len(b), 0)``, and this is the only case in which the last
550tuple element (number of elements matched) is ``0``.
551
552If you want to know how to change the first sequence into the second, use
Christian Heimesfe337bf2008-03-23 21:54:12 +0000553:meth:`get_opcodes`:
Georg Brandl116aa622007-08-15 14:28:22 +0000554
555 >>> for opcode in s.get_opcodes():
Georg Brandl6911e3c2007-09-04 07:15:32 +0000556 ... print("%6s a[%d:%d] b[%d:%d]" % opcode)
Georg Brandl116aa622007-08-15 14:28:22 +0000557 equal a[0:8] b[0:8]
558 insert a[8:8] b[8:17]
Christian Heimesfe337bf2008-03-23 21:54:12 +0000559 equal a[8:29] b[17:38]
Georg Brandl116aa622007-08-15 14:28:22 +0000560
561See also the function :func:`get_close_matches` in this module, which shows how
562simple code building on :class:`SequenceMatcher` can be used to do useful work.
563
564
565.. _differ-objects:
566
567Differ Objects
568--------------
569
570Note that :class:`Differ`\ -generated deltas make no claim to be **minimal**
571diffs. To the contrary, minimal diffs are often counter-intuitive, because they
572synch up anywhere possible, sometimes accidental matches 100 pages apart.
573Restricting synch points to contiguous matches preserves some notion of
574locality, at the occasional cost of producing a longer diff.
575
576The :class:`Differ` class has this constructor:
577
578
579.. class:: Differ([linejunk[, charjunk]])
580
581 Optional keyword parameters *linejunk* and *charjunk* are for filter functions
582 (or ``None``):
583
584 *linejunk*: A function that accepts a single string argument, and returns true
585 if the string is junk. The default is ``None``, meaning that no line is
586 considered junk.
587
588 *charjunk*: A function that accepts a single character argument (a string of
589 length 1), and returns true if the character is junk. The default is ``None``,
590 meaning that no character is considered junk.
591
592:class:`Differ` objects are used (deltas generated) via a single method:
593
594
595.. method:: Differ.compare(a, b)
596
597 Compare two sequences of lines, and generate the delta (a sequence of lines).
598
599 Each sequence must contain individual single-line strings ending with newlines.
600 Such sequences can be obtained from the :meth:`readlines` method of file-like
601 objects. The delta generated also consists of newline-terminated strings, ready
602 to be printed as-is via the :meth:`writelines` method of a file-like object.
603
604
605.. _differ-examples:
606
607Differ Example
608--------------
609
610This example compares two texts. First we set up the texts, sequences of
611individual single-line strings ending with newlines (such sequences can also be
Christian Heimesfe337bf2008-03-23 21:54:12 +0000612obtained from the :meth:`readlines` method of file-like objects):
Georg Brandl116aa622007-08-15 14:28:22 +0000613
614 >>> text1 = ''' 1. Beautiful is better than ugly.
615 ... 2. Explicit is better than implicit.
616 ... 3. Simple is better than complex.
617 ... 4. Complex is better than complicated.
618 ... '''.splitlines(1)
619 >>> len(text1)
620 4
621 >>> text1[0][-1]
622 '\n'
623 >>> text2 = ''' 1. Beautiful is better than ugly.
624 ... 3. Simple is better than complex.
625 ... 4. Complicated is better than complex.
626 ... 5. Flat is better than nested.
627 ... '''.splitlines(1)
628
Christian Heimesfe337bf2008-03-23 21:54:12 +0000629Next we instantiate a Differ object:
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631 >>> d = Differ()
632
633Note that when instantiating a :class:`Differ` object we may pass functions to
634filter out line and character "junk." See the :meth:`Differ` constructor for
635details.
636
Christian Heimesfe337bf2008-03-23 21:54:12 +0000637Finally, we compare the two:
Georg Brandl116aa622007-08-15 14:28:22 +0000638
639 >>> result = list(d.compare(text1, text2))
640
Christian Heimesfe337bf2008-03-23 21:54:12 +0000641``result`` is a list of strings, so let's pretty-print it:
Georg Brandl116aa622007-08-15 14:28:22 +0000642
643 >>> from pprint import pprint
644 >>> pprint(result)
645 [' 1. Beautiful is better than ugly.\n',
646 '- 2. Explicit is better than implicit.\n',
647 '- 3. Simple is better than complex.\n',
648 '+ 3. Simple is better than complex.\n',
Christian Heimesfe337bf2008-03-23 21:54:12 +0000649 '? ++\n',
Georg Brandl116aa622007-08-15 14:28:22 +0000650 '- 4. Complex is better than complicated.\n',
Christian Heimesfe337bf2008-03-23 21:54:12 +0000651 '? ^ ---- ^\n',
Georg Brandl116aa622007-08-15 14:28:22 +0000652 '+ 4. Complicated is better than complex.\n',
Christian Heimesfe337bf2008-03-23 21:54:12 +0000653 '? ++++ ^ ^\n',
Georg Brandl116aa622007-08-15 14:28:22 +0000654 '+ 5. Flat is better than nested.\n']
655
Christian Heimesfe337bf2008-03-23 21:54:12 +0000656As a single multi-line string it looks like this:
Georg Brandl116aa622007-08-15 14:28:22 +0000657
658 >>> import sys
659 >>> sys.stdout.writelines(result)
660 1. Beautiful is better than ugly.
661 - 2. Explicit is better than implicit.
662 - 3. Simple is better than complex.
663 + 3. Simple is better than complex.
664 ? ++
665 - 4. Complex is better than complicated.
666 ? ^ ---- ^
667 + 4. Complicated is better than complex.
668 ? ++++ ^ ^
669 + 5. Flat is better than nested.
670
Christian Heimes8640e742008-02-23 16:23:06 +0000671
672.. _difflib-interface:
673
674A command-line interface to difflib
675-----------------------------------
676
677This example shows how to use difflib to create a ``diff``-like utility.
678It is also contained in the Python source distribution, as
679:file:`Tools/scripts/diff.py`.
680
Christian Heimesfe337bf2008-03-23 21:54:12 +0000681.. testcode::
Christian Heimes8640e742008-02-23 16:23:06 +0000682
683 """ Command line interface to difflib.py providing diffs in four formats:
684
685 * ndiff: lists every line and highlights interline changes.
686 * context: highlights clusters of changes in a before/after format.
687 * unified: highlights clusters of changes in an inline format.
688 * html: generates side by side comparison with change highlights.
689
690 """
691
692 import sys, os, time, difflib, optparse
693
694 def main():
695 # Configure the option parser
696 usage = "usage: %prog [options] fromfile tofile"
697 parser = optparse.OptionParser(usage)
698 parser.add_option("-c", action="store_true", default=False,
699 help='Produce a context format diff (default)')
700 parser.add_option("-u", action="store_true", default=False,
701 help='Produce a unified format diff')
702 hlp = 'Produce HTML side by side diff (can use -c and -l in conjunction)'
703 parser.add_option("-m", action="store_true", default=False, help=hlp)
704 parser.add_option("-n", action="store_true", default=False,
705 help='Produce a ndiff format diff')
706 parser.add_option("-l", "--lines", type="int", default=3,
707 help='Set number of context lines (default 3)')
708 (options, args) = parser.parse_args()
709
710 if len(args) == 0:
711 parser.print_help()
712 sys.exit(1)
713 if len(args) != 2:
714 parser.error("need to specify both a fromfile and tofile")
715
716 n = options.lines
717 fromfile, tofile = args # as specified in the usage string
718
719 # we're passing these as arguments to the diff function
720 fromdate = time.ctime(os.stat(fromfile).st_mtime)
721 todate = time.ctime(os.stat(tofile).st_mtime)
722 fromlines = open(fromfile, 'U').readlines()
723 tolines = open(tofile, 'U').readlines()
724
725 if options.u:
726 diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
727 fromdate, todate, n=n)
728 elif options.n:
729 diff = difflib.ndiff(fromlines, tolines)
730 elif options.m:
731 diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile,
732 tofile, context=options.c,
733 numlines=n)
734 else:
735 diff = difflib.context_diff(fromlines, tolines, fromfile, tofile,
736 fromdate, todate, n=n)
737
738 # we're using writelines because diff is a generator
739 sys.stdout.writelines(diff)
740
741 if __name__ == '__main__':
742 main()