blob: 4bc5226aab8b0d1dae2d1e9c3349d7ee40aff377 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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>
Georg Brandlb19be572007-12-29 10:57:00 +00008.. Markup by Fred L. Drake, Jr. <fdrake@acm.org>
Georg Brandl8ec7f652007-08-15 14:28:01 +00009
Georg Brandle8f1b002008-03-22 22:04:10 +000010.. testsetup::
Georg Brandl8ec7f652007-08-15 14:28:01 +000011
Georg Brandle8f1b002008-03-22 22:04:10 +000012 import sys
13 from difflib import *
Georg Brandl8ec7f652007-08-15 14:28:01 +000014
15.. versionadded:: 2.1
16
Mark Summerfield0752d202007-10-19 12:48:17 +000017This module provides classes and functions for comparing sequences. It
18can be used for example, for comparing files, and can produce difference
19information in various formats, including HTML and context and unified
20diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
22.. class:: SequenceMatcher
23
24 This is a flexible class for comparing pairs of sequences of any type, so long
Georg Brandl7c3e79f2007-11-02 20:06:17 +000025 as the sequence elements are :term:`hashable`. The basic algorithm predates, and is a
Georg Brandl8ec7f652007-08-15 14:28:01 +000026 little fancier than, an algorithm published in the late 1980's by Ratcliff and
27 Obershelp under the hyperbolic name "gestalt pattern matching." The idea is to
28 find the longest contiguous matching subsequence that contains no "junk"
29 elements (the Ratcliff and Obershelp algorithm doesn't address junk). The same
30 idea is then applied recursively to the pieces of the sequences to the left and
31 to the right of the matching subsequence. This does not yield minimal edit
32 sequences, but does tend to yield matches that "look right" to people.
33
34 **Timing:** The basic Ratcliff-Obershelp algorithm is cubic time in the worst
35 case and quadratic time in the expected case. :class:`SequenceMatcher` is
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
38 time is linear.
39
40
41.. class:: Differ
42
43 This is a class for comparing sequences of lines of text, and producing
44 human-readable differences or deltas. Differ uses :class:`SequenceMatcher`
45 both to compare sequences of lines, and to compare sequences of characters
46 within similar (near-matching) lines.
47
48 Each line of a :class:`Differ` delta begins with a two-letter code:
49
50 +----------+-------------------------------------------+
51 | Code | Meaning |
52 +==========+===========================================+
53 | ``'- '`` | line unique to sequence 1 |
54 +----------+-------------------------------------------+
55 | ``'+ '`` | line unique to sequence 2 |
56 +----------+-------------------------------------------+
57 | ``' '`` | line common to both sequences |
58 +----------+-------------------------------------------+
59 | ``'? '`` | line not present in either input sequence |
60 +----------+-------------------------------------------+
61
62 Lines beginning with '``?``' attempt to guide the eye to intraline differences,
63 and were not present in either input sequence. These lines can be confusing if
64 the sequences contain tab characters.
65
66
67.. class:: HtmlDiff
68
69 This class can be used to create an HTML table (or a complete HTML file
70 containing the table) showing a side by side, line by line comparison of text
71 with inter-line and intra-line change highlights. The table can be generated in
72 either full or contextual difference mode.
73
74 The constructor for this class is:
75
76
77 .. function:: __init__([tabsize][, wrapcolumn][, linejunk][, charjunk])
78
79 Initializes instance of :class:`HtmlDiff`.
80
81 *tabsize* is an optional keyword argument to specify tab stop spacing and
82 defaults to ``8``.
83
84 *wrapcolumn* is an optional keyword to specify column number where lines are
85 broken and wrapped, defaults to ``None`` where lines are not wrapped.
86
87 *linejunk* and *charjunk* are optional keyword arguments passed into ``ndiff()``
88 (used by :class:`HtmlDiff` to generate the side by side HTML differences). See
89 ``ndiff()`` documentation for argument default values and descriptions.
90
91 The following methods are public:
92
93
94 .. function:: make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
95
96 Compares *fromlines* and *tolines* (lists of strings) and returns a string which
97 is a complete HTML file containing a table showing line by line differences with
98 inter-line and intra-line changes highlighted.
99
100 *fromdesc* and *todesc* are optional keyword arguments to specify from/to file
101 column header strings (both default to an empty string).
102
103 *context* and *numlines* are both optional keyword arguments. Set *context* to
104 ``True`` when contextual differences are to be shown, else the default is
105 ``False`` to show the full files. *numlines* defaults to ``5``. When *context*
106 is ``True`` *numlines* controls the number of context lines which surround the
107 difference highlights. When *context* is ``False`` *numlines* controls the
108 number of lines which are shown before a difference highlight when using the
109 "next" hyperlinks (setting to zero would cause the "next" hyperlinks to place
110 the next difference highlight at the top of the browser without any leading
111 context).
112
113
114 .. function:: make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
115
116 Compares *fromlines* and *tolines* (lists of strings) and returns a string which
117 is a complete HTML table showing line by line differences with inter-line and
118 intra-line changes highlighted.
119
120 The arguments for this method are the same as those for the :meth:`make_file`
121 method.
122
123 :file:`Tools/scripts/diff.py` is a command-line front-end to this class and
124 contains a good example of its use.
125
126 .. versionadded:: 2.4
127
128
129.. function:: context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
130
Georg Brandlcf3fb252007-10-21 10:52:38 +0000131 Compare *a* and *b* (lists of strings); return a delta (a :term:`generator`
132 generating the delta lines) in context diff format.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000133
134 Context diffs are a compact way of showing just the lines that have changed plus
135 a few lines of context. The changes are shown in a before/after style. The
136 number of context lines is set by *n* which defaults to three.
137
138 By default, the diff control lines (those with ``***`` or ``---``) are created
139 with a trailing newline. This is helpful so that inputs created from
140 :func:`file.readlines` result in diffs that are suitable for use with
141 :func:`file.writelines` since both the inputs and outputs have trailing
142 newlines.
143
144 For inputs that do not have trailing newlines, set the *lineterm* argument to
145 ``""`` so that the output will be uniformly newline free.
146
147 The context diff format normally has a header for filenames and modification
148 times. Any or all of these may be specified using strings for *fromfile*,
149 *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
150 expressed in the format returned by :func:`time.ctime`. If not specified, the
151 strings default to blanks.
152
Georg Brandl080b0942008-02-23 15:19:54 +0000153 >>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
154 >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
155 >>> for line in context_diff(s1, s2, fromfile='before.py', tofile='after.py'):
Georg Brandle8f1b002008-03-22 22:04:10 +0000156 ... sys.stdout.write(line) # doctest: +NORMALIZE_WHITESPACE
Georg Brandl080b0942008-02-23 15:19:54 +0000157 *** before.py
158 --- after.py
159 ***************
160 *** 1,4 ****
161 ! bacon
162 ! eggs
163 ! ham
164 guido
165 --- 1,4 ----
166 ! python
167 ! eggy
168 ! hamster
169 guido
170
171 See :ref:`difflib-interface` for a more detailed example.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172
173 .. versionadded:: 2.3
174
175
176.. function:: get_close_matches(word, possibilities[, n][, cutoff])
177
178 Return a list of the best "good enough" matches. *word* is a sequence for which
179 close matches are desired (typically a string), and *possibilities* is a list of
180 sequences against which to match *word* (typically a list of strings).
181
182 Optional argument *n* (default ``3``) is the maximum number of close matches to
183 return; *n* must be greater than ``0``.
184
185 Optional argument *cutoff* (default ``0.6``) is a float in the range [0, 1].
186 Possibilities that don't score at least that similar to *word* are ignored.
187
188 The best (no more than *n*) matches among the possibilities are returned in a
Georg Brandle8f1b002008-03-22 22:04:10 +0000189 list, sorted by similarity score, most similar first.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000190
191 >>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
192 ['apple', 'ape']
193 >>> import keyword
194 >>> get_close_matches('wheel', keyword.kwlist)
195 ['while']
196 >>> get_close_matches('apple', keyword.kwlist)
197 []
198 >>> get_close_matches('accept', keyword.kwlist)
199 ['except']
200
201
202.. function:: ndiff(a, b[, linejunk][, charjunk])
203
Georg Brandlcf3fb252007-10-21 10:52:38 +0000204 Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style
205 delta (a :term:`generator` generating the delta lines).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000206
207 Optional keyword parameters *linejunk* and *charjunk* are for filter functions
208 (or ``None``):
209
210 *linejunk*: A function that accepts a single string argument, and returns true
211 if the string is junk, or false if not. The default is (``None``), starting with
212 Python 2.3. Before then, the default was the module-level function
213 :func:`IS_LINE_JUNK`, which filters out lines without visible characters, except
214 for at most one pound character (``'#'``). As of Python 2.3, the underlying
215 :class:`SequenceMatcher` class does a dynamic analysis of which lines are so
216 frequent as to constitute noise, and this usually works better than the pre-2.3
217 default.
218
219 *charjunk*: A function that accepts a character (a string of length 1), and
220 returns if the character is junk, or false if not. The default is module-level
221 function :func:`IS_CHARACTER_JUNK`, which filters out whitespace characters (a
222 blank or tab; note: bad idea to include newline in this!).
223
Georg Brandle8f1b002008-03-22 22:04:10 +0000224 :file:`Tools/scripts/ndiff.py` is a command-line front-end to this function.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000225
226 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
227 ... 'ore\ntree\nemu\n'.splitlines(1))
228 >>> print ''.join(diff),
229 - one
230 ? ^
231 + ore
232 ? ^
233 - two
234 - three
235 ? -
236 + tree
237 + emu
238
239
240.. function:: restore(sequence, which)
241
242 Return one of the two sequences that generated a delta.
243
244 Given a *sequence* produced by :meth:`Differ.compare` or :func:`ndiff`, extract
245 lines originating from file 1 or 2 (parameter *which*), stripping off line
246 prefixes.
247
Georg Brandle8f1b002008-03-22 22:04:10 +0000248 Example:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000249
250 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
251 ... 'ore\ntree\nemu\n'.splitlines(1))
252 >>> diff = list(diff) # materialize the generated delta into a list
253 >>> print ''.join(restore(diff, 1)),
254 one
255 two
256 three
257 >>> print ''.join(restore(diff, 2)),
258 ore
259 tree
260 emu
261
262
263.. function:: unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
264
Georg Brandlcf3fb252007-10-21 10:52:38 +0000265 Compare *a* and *b* (lists of strings); return a delta (a :term:`generator`
266 generating the delta lines) in unified diff format.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000267
268 Unified diffs are a compact way of showing just the lines that have changed plus
269 a few lines of context. The changes are shown in a inline style (instead of
270 separate before/after blocks). The number of context lines is set by *n* which
271 defaults to three.
272
273 By default, the diff control lines (those with ``---``, ``+++``, or ``@@``) are
274 created with a trailing newline. This is helpful so that inputs created from
275 :func:`file.readlines` result in diffs that are suitable for use with
276 :func:`file.writelines` since both the inputs and outputs have trailing
277 newlines.
278
279 For inputs that do not have trailing newlines, set the *lineterm* argument to
280 ``""`` so that the output will be uniformly newline free.
281
282 The context diff format normally has a header for filenames and modification
283 times. Any or all of these may be specified using strings for *fromfile*,
284 *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
285 expressed in the format returned by :func:`time.ctime`. If not specified, the
286 strings default to blanks.
287
Georg Brandl080b0942008-02-23 15:19:54 +0000288 >>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
289 >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
290 >>> for line in unified_diff(s1, s2, fromfile='before.py', tofile='after.py'):
Georg Brandle8f1b002008-03-22 22:04:10 +0000291 ... sys.stdout.write(line) # doctest: +NORMALIZE_WHITESPACE
Georg Brandl080b0942008-02-23 15:19:54 +0000292 --- before.py
293 +++ after.py
294 @@ -1,4 +1,4 @@
295 -bacon
296 -eggs
297 -ham
298 +python
299 +eggy
300 +hamster
301 guido
302
303 See :ref:`difflib-interface` for a more detailed example.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000304
305 .. versionadded:: 2.3
306
307
308.. function:: IS_LINE_JUNK(line)
309
310 Return true for ignorable lines. The line *line* is ignorable if *line* is
311 blank or contains a single ``'#'``, otherwise it is not ignorable. Used as a
312 default for parameter *linejunk* in :func:`ndiff` before Python 2.3.
313
314
315.. function:: IS_CHARACTER_JUNK(ch)
316
317 Return true for ignorable characters. The character *ch* is ignorable if *ch*
318 is a space or tab, otherwise it is not ignorable. Used as a default for
319 parameter *charjunk* in :func:`ndiff`.
320
321
322.. seealso::
323
324 `Pattern Matching: The Gestalt Approach <http://www.ddj.com/184407970?pgno=5>`_
325 Discussion of a similar algorithm by John W. Ratcliff and D. E. Metzener. This
326 was published in `Dr. Dobb's Journal <http://www.ddj.com/>`_ in July, 1988.
327
328
329.. _sequence-matcher:
330
331SequenceMatcher Objects
332-----------------------
333
334The :class:`SequenceMatcher` class has this constructor:
335
336
337.. class:: SequenceMatcher([isjunk[, a[, b]]])
338
339 Optional argument *isjunk* must be ``None`` (the default) or a one-argument
340 function that takes a sequence element and returns true if and only if the
341 element is "junk" and should be ignored. Passing ``None`` for *isjunk* is
342 equivalent to passing ``lambda x: 0``; in other words, no elements are ignored.
343 For example, pass::
344
345 lambda x: x in " \t"
346
347 if you're comparing lines as sequences of characters, and don't want to synch up
348 on blanks or hard tabs.
349
350 The optional arguments *a* and *b* are sequences to be compared; both default to
Georg Brandl7c3e79f2007-11-02 20:06:17 +0000351 empty strings. The elements of both sequences must be :term:`hashable`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000352
353:class:`SequenceMatcher` objects have the following methods:
354
355
356.. method:: SequenceMatcher.set_seqs(a, b)
357
358 Set the two sequences to be compared.
359
360:class:`SequenceMatcher` computes and caches detailed information about the
361second sequence, so if you want to compare one sequence against many sequences,
362use :meth:`set_seq2` to set the commonly used sequence once and call
363:meth:`set_seq1` repeatedly, once for each of the other sequences.
364
365
366.. method:: SequenceMatcher.set_seq1(a)
367
368 Set the first sequence to be compared. The second sequence to be compared is
369 not changed.
370
371
372.. method:: SequenceMatcher.set_seq2(b)
373
374 Set the second sequence to be compared. The first sequence to be compared is
375 not changed.
376
377
378.. method:: SequenceMatcher.find_longest_match(alo, ahi, blo, bhi)
379
380 Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``.
381
Georg Brandle3c3db52008-01-11 09:55:53 +0000382 If *isjunk* was omitted or ``None``, :meth:`find_longest_match` returns ``(i, j,
Georg Brandl8ec7f652007-08-15 14:28:01 +0000383 k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo <= i <= i+k <=
384 ahi`` and ``blo <= j <= j+k <= bhi``. For all ``(i', j', k')`` meeting those
385 conditions, the additional conditions ``k >= k'``, ``i <= i'``, and if ``i ==
386 i'``, ``j <= j'`` are also met. In other words, of all maximal matching blocks,
387 return one that starts earliest in *a*, and of all those maximal matching blocks
Georg Brandle8f1b002008-03-22 22:04:10 +0000388 that start earliest in *a*, return the one that starts earliest in *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000389
390 >>> s = SequenceMatcher(None, " abcd", "abcd abcd")
391 >>> s.find_longest_match(0, 5, 0, 9)
Georg Brandle8f1b002008-03-22 22:04:10 +0000392 Match(a=0, b=4, size=5)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000393
394 If *isjunk* was provided, first the longest matching block is determined as
395 above, but with the additional restriction that no junk element appears in the
396 block. Then that block is extended as far as possible by matching (only) junk
397 elements on both sides. So the resulting block never matches on junk except as
398 identical junk happens to be adjacent to an interesting match.
399
400 Here's the same example as before, but considering blanks to be junk. That
401 prevents ``' abcd'`` from matching the ``' abcd'`` at the tail end of the second
402 sequence directly. Instead only the ``'abcd'`` can match, and matches the
Georg Brandle8f1b002008-03-22 22:04:10 +0000403 leftmost ``'abcd'`` in the second sequence:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000404
405 >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
406 >>> s.find_longest_match(0, 5, 0, 9)
Georg Brandle8f1b002008-03-22 22:04:10 +0000407 Match(a=1, b=0, size=4)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000408
409 If no blocks match, this returns ``(alo, blo, 0)``.
410
Georg Brandle3c3db52008-01-11 09:55:53 +0000411 .. versionchanged:: 2.6
412 This method returns a :term:`named tuple` ``Match(a, b, size)``.
413
Georg Brandl8ec7f652007-08-15 14:28:01 +0000414
415.. method:: SequenceMatcher.get_matching_blocks()
416
417 Return list of triples describing matching subsequences. Each triple is of the
418 form ``(i, j, n)``, and means that ``a[i:i+n] == b[j:j+n]``. The triples are
419 monotonically increasing in *i* and *j*.
420
421 The last triple is a dummy, and has the value ``(len(a), len(b), 0)``. It is
422 the only triple with ``n == 0``. If ``(i, j, n)`` and ``(i', j', n')`` are
423 adjacent triples in the list, and the second is not the last triple in the list,
424 then ``i+n != i'`` or ``j+n != j'``; in other words, adjacent triples always
425 describe non-adjacent equal blocks.
426
Georg Brandlb19be572007-12-29 10:57:00 +0000427 .. XXX Explain why a dummy is used!
Georg Brandl8ec7f652007-08-15 14:28:01 +0000428
429 .. versionchanged:: 2.5
430 The guarantee that adjacent triples always describe non-adjacent blocks was
431 implemented.
432
Georg Brandle8f1b002008-03-22 22:04:10 +0000433 .. doctest::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000434
435 >>> s = SequenceMatcher(None, "abxcd", "abcd")
436 >>> s.get_matching_blocks()
Georg Brandle8f1b002008-03-22 22:04:10 +0000437 [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000438
439
440.. method:: SequenceMatcher.get_opcodes()
441
442 Return list of 5-tuples describing how to turn *a* into *b*. Each tuple is of
443 the form ``(tag, i1, i2, j1, j2)``. The first tuple has ``i1 == j1 == 0``, and
444 remaining tuples have *i1* equal to the *i2* from the preceding tuple, and,
445 likewise, *j1* equal to the previous *j2*.
446
447 The *tag* values are strings, with these meanings:
448
449 +---------------+---------------------------------------------+
450 | Value | Meaning |
451 +===============+=============================================+
452 | ``'replace'`` | ``a[i1:i2]`` should be replaced by |
453 | | ``b[j1:j2]``. |
454 +---------------+---------------------------------------------+
455 | ``'delete'`` | ``a[i1:i2]`` should be deleted. Note that |
456 | | ``j1 == j2`` in this case. |
457 +---------------+---------------------------------------------+
458 | ``'insert'`` | ``b[j1:j2]`` should be inserted at |
459 | | ``a[i1:i1]``. Note that ``i1 == i2`` in |
460 | | this case. |
461 +---------------+---------------------------------------------+
462 | ``'equal'`` | ``a[i1:i2] == b[j1:j2]`` (the sub-sequences |
463 | | are equal). |
464 +---------------+---------------------------------------------+
465
Georg Brandle8f1b002008-03-22 22:04:10 +0000466 For example:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000467
468 >>> a = "qabxcd"
469 >>> b = "abycdf"
470 >>> s = SequenceMatcher(None, a, b)
471 >>> for tag, i1, i2, j1, j2 in s.get_opcodes():
472 ... print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
473 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))
474 delete a[0:1] (q) b[0:0] ()
475 equal a[1:3] (ab) b[0:2] (ab)
476 replace a[3:4] (x) b[2:3] (y)
477 equal a[4:6] (cd) b[3:5] (cd)
478 insert a[6:6] () b[5:6] (f)
479
480
481.. method:: SequenceMatcher.get_grouped_opcodes([n])
482
Georg Brandlcf3fb252007-10-21 10:52:38 +0000483 Return a :term:`generator` of groups with up to *n* lines of context.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000484
485 Starting with the groups returned by :meth:`get_opcodes`, this method splits out
486 smaller change clusters and eliminates intervening ranges which have no changes.
487
488 The groups are returned in the same format as :meth:`get_opcodes`.
489
490 .. versionadded:: 2.3
491
492
493.. method:: SequenceMatcher.ratio()
494
495 Return a measure of the sequences' similarity as a float in the range [0, 1].
496
497 Where T is the total number of elements in both sequences, and M is the number
498 of matches, this is 2.0\*M / T. Note that this is ``1.0`` if the sequences are
499 identical, and ``0.0`` if they have nothing in common.
500
501 This is expensive to compute if :meth:`get_matching_blocks` or
502 :meth:`get_opcodes` hasn't already been called, in which case you may want to
503 try :meth:`quick_ratio` or :meth:`real_quick_ratio` first to get an upper bound.
504
505
506.. method:: SequenceMatcher.quick_ratio()
507
508 Return an upper bound on :meth:`ratio` relatively quickly.
509
510 This isn't defined beyond that it is an upper bound on :meth:`ratio`, and is
511 faster to compute.
512
513
514.. method:: SequenceMatcher.real_quick_ratio()
515
516 Return an upper bound on :meth:`ratio` very quickly.
517
518 This isn't defined beyond that it is an upper bound on :meth:`ratio`, and is
519 faster to compute than either :meth:`ratio` or :meth:`quick_ratio`.
520
521The three methods that return the ratio of matching to total characters can give
522different results due to differing levels of approximation, although
523:meth:`quick_ratio` and :meth:`real_quick_ratio` are always at least as large as
Georg Brandle8f1b002008-03-22 22:04:10 +0000524:meth:`ratio`:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000525
526 >>> s = SequenceMatcher(None, "abcd", "bcde")
527 >>> s.ratio()
528 0.75
529 >>> s.quick_ratio()
530 0.75
531 >>> s.real_quick_ratio()
532 1.0
533
534
535.. _sequencematcher-examples:
536
537SequenceMatcher Examples
538------------------------
539
Georg Brandle8f1b002008-03-22 22:04:10 +0000540This example compares two strings, considering blanks to be "junk:"
Georg Brandl8ec7f652007-08-15 14:28:01 +0000541
542 >>> s = SequenceMatcher(lambda x: x == " ",
543 ... "private Thread currentThread;",
544 ... "private volatile Thread currentThread;")
545
546:meth:`ratio` returns a float in [0, 1], measuring the similarity of the
547sequences. As a rule of thumb, a :meth:`ratio` value over 0.6 means the
Georg Brandle8f1b002008-03-22 22:04:10 +0000548sequences are close matches:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000549
550 >>> print round(s.ratio(), 3)
551 0.866
552
553If you're only interested in where the sequences match,
Georg Brandle8f1b002008-03-22 22:04:10 +0000554:meth:`get_matching_blocks` is handy:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000555
556 >>> for block in s.get_matching_blocks():
557 ... print "a[%d] and b[%d] match for %d elements" % block
558 a[0] and b[0] match for 8 elements
Georg Brandle8f1b002008-03-22 22:04:10 +0000559 a[8] and b[17] match for 21 elements
Georg Brandl8ec7f652007-08-15 14:28:01 +0000560 a[29] and b[38] match for 0 elements
561
562Note that the last tuple returned by :meth:`get_matching_blocks` is always a
563dummy, ``(len(a), len(b), 0)``, and this is the only case in which the last
564tuple element (number of elements matched) is ``0``.
565
566If you want to know how to change the first sequence into the second, use
Georg Brandle8f1b002008-03-22 22:04:10 +0000567:meth:`get_opcodes`:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000568
569 >>> for opcode in s.get_opcodes():
570 ... print "%6s a[%d:%d] b[%d:%d]" % opcode
571 equal a[0:8] b[0:8]
572 insert a[8:8] b[8:17]
Georg Brandle8f1b002008-03-22 22:04:10 +0000573 equal a[8:29] b[17:38]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000574
575See also the function :func:`get_close_matches` in this module, which shows how
576simple code building on :class:`SequenceMatcher` can be used to do useful work.
577
578
579.. _differ-objects:
580
581Differ Objects
582--------------
583
584Note that :class:`Differ`\ -generated deltas make no claim to be **minimal**
585diffs. To the contrary, minimal diffs are often counter-intuitive, because they
586synch up anywhere possible, sometimes accidental matches 100 pages apart.
587Restricting synch points to contiguous matches preserves some notion of
588locality, at the occasional cost of producing a longer diff.
589
590The :class:`Differ` class has this constructor:
591
592
593.. class:: Differ([linejunk[, charjunk]])
594
595 Optional keyword parameters *linejunk* and *charjunk* are for filter functions
596 (or ``None``):
597
598 *linejunk*: A function that accepts a single string argument, and returns true
599 if the string is junk. The default is ``None``, meaning that no line is
600 considered junk.
601
602 *charjunk*: A function that accepts a single character argument (a string of
603 length 1), and returns true if the character is junk. The default is ``None``,
604 meaning that no character is considered junk.
605
606:class:`Differ` objects are used (deltas generated) via a single method:
607
608
609.. method:: Differ.compare(a, b)
610
611 Compare two sequences of lines, and generate the delta (a sequence of lines).
612
613 Each sequence must contain individual single-line strings ending with newlines.
614 Such sequences can be obtained from the :meth:`readlines` method of file-like
615 objects. The delta generated also consists of newline-terminated strings, ready
616 to be printed as-is via the :meth:`writelines` method of a file-like object.
617
618
619.. _differ-examples:
620
621Differ Example
622--------------
623
624This example compares two texts. First we set up the texts, sequences of
625individual single-line strings ending with newlines (such sequences can also be
Georg Brandle8f1b002008-03-22 22:04:10 +0000626obtained from the :meth:`readlines` method of file-like objects):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000627
628 >>> text1 = ''' 1. Beautiful is better than ugly.
629 ... 2. Explicit is better than implicit.
630 ... 3. Simple is better than complex.
631 ... 4. Complex is better than complicated.
632 ... '''.splitlines(1)
633 >>> len(text1)
634 4
635 >>> text1[0][-1]
636 '\n'
637 >>> text2 = ''' 1. Beautiful is better than ugly.
638 ... 3. Simple is better than complex.
639 ... 4. Complicated is better than complex.
640 ... 5. Flat is better than nested.
641 ... '''.splitlines(1)
642
Georg Brandle8f1b002008-03-22 22:04:10 +0000643Next we instantiate a Differ object:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000644
645 >>> d = Differ()
646
647Note that when instantiating a :class:`Differ` object we may pass functions to
648filter out line and character "junk." See the :meth:`Differ` constructor for
649details.
650
Georg Brandle8f1b002008-03-22 22:04:10 +0000651Finally, we compare the two:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000652
653 >>> result = list(d.compare(text1, text2))
654
Georg Brandle8f1b002008-03-22 22:04:10 +0000655``result`` is a list of strings, so let's pretty-print it:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000656
657 >>> from pprint import pprint
658 >>> pprint(result)
659 [' 1. Beautiful is better than ugly.\n',
660 '- 2. Explicit is better than implicit.\n',
661 '- 3. Simple is better than complex.\n',
662 '+ 3. Simple is better than complex.\n',
Georg Brandle8f1b002008-03-22 22:04:10 +0000663 '? ++\n',
Georg Brandl8ec7f652007-08-15 14:28:01 +0000664 '- 4. Complex is better than complicated.\n',
Georg Brandle8f1b002008-03-22 22:04:10 +0000665 '? ^ ---- ^\n',
Georg Brandl8ec7f652007-08-15 14:28:01 +0000666 '+ 4. Complicated is better than complex.\n',
Georg Brandle8f1b002008-03-22 22:04:10 +0000667 '? ++++ ^ ^\n',
Georg Brandl8ec7f652007-08-15 14:28:01 +0000668 '+ 5. Flat is better than nested.\n']
669
Georg Brandle8f1b002008-03-22 22:04:10 +0000670As a single multi-line string it looks like this:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000671
672 >>> import sys
673 >>> sys.stdout.writelines(result)
674 1. Beautiful is better than ugly.
675 - 2. Explicit is better than implicit.
676 - 3. Simple is better than complex.
677 + 3. Simple is better than complex.
678 ? ++
679 - 4. Complex is better than complicated.
680 ? ^ ---- ^
681 + 4. Complicated is better than complex.
682 ? ++++ ^ ^
683 + 5. Flat is better than nested.
684
Georg Brandl080b0942008-02-23 15:19:54 +0000685
686.. _difflib-interface:
687
688A command-line interface to difflib
689-----------------------------------
690
691This example shows how to use difflib to create a ``diff``-like utility.
692It is also contained in the Python source distribution, as
693:file:`Tools/scripts/diff.py`.
694
Georg Brandle8f1b002008-03-22 22:04:10 +0000695.. testcode::
Georg Brandl080b0942008-02-23 15:19:54 +0000696
697 """ Command line interface to difflib.py providing diffs in four formats:
698
699 * ndiff: lists every line and highlights interline changes.
700 * context: highlights clusters of changes in a before/after format.
701 * unified: highlights clusters of changes in an inline format.
702 * html: generates side by side comparison with change highlights.
703
704 """
705
706 import sys, os, time, difflib, optparse
707
708 def main():
709 # Configure the option parser
710 usage = "usage: %prog [options] fromfile tofile"
711 parser = optparse.OptionParser(usage)
712 parser.add_option("-c", action="store_true", default=False,
713 help='Produce a context format diff (default)')
714 parser.add_option("-u", action="store_true", default=False,
715 help='Produce a unified format diff')
716 hlp = 'Produce HTML side by side diff (can use -c and -l in conjunction)'
717 parser.add_option("-m", action="store_true", default=False, help=hlp)
718 parser.add_option("-n", action="store_true", default=False,
719 help='Produce a ndiff format diff')
720 parser.add_option("-l", "--lines", type="int", default=3,
721 help='Set number of context lines (default 3)')
722 (options, args) = parser.parse_args()
723
724 if len(args) == 0:
725 parser.print_help()
726 sys.exit(1)
727 if len(args) != 2:
728 parser.error("need to specify both a fromfile and tofile")
729
730 n = options.lines
731 fromfile, tofile = args # as specified in the usage string
732
733 # we're passing these as arguments to the diff function
734 fromdate = time.ctime(os.stat(fromfile).st_mtime)
735 todate = time.ctime(os.stat(tofile).st_mtime)
736 fromlines = open(fromfile, 'U').readlines()
737 tolines = open(tofile, 'U').readlines()
738
739 if options.u:
740 diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
741 fromdate, todate, n=n)
742 elif options.n:
743 diff = difflib.ndiff(fromlines, tolines)
744 elif options.m:
745 diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile,
746 tofile, context=options.c,
747 numlines=n)
748 else:
749 diff = difflib.context_diff(fromlines, tolines, fromfile, tofile,
750 fromdate, todate, n=n)
751
752 # we're using writelines because diff is a generator
753 sys.stdout.writelines(diff)
754
755 if __name__ == '__main__':
756 main()