Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`difflib` --- Helpers for computing deltas |
| 3 | =============================================== |
| 4 | |
| 5 | .. module:: difflib |
| 6 | :synopsis: Helpers for computing differences between objects. |
| 7 | .. moduleauthor:: Tim Peters <tim_one@users.sourceforge.net> |
| 8 | .. sectionauthor:: Tim Peters <tim_one@users.sourceforge.net> |
| 9 | |
| 10 | |
| 11 | .. % LaTeXification by Fred L. Drake, Jr. <fdrake@acm.org>. |
| 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | .. class:: SequenceMatcher |
| 14 | |
| 15 | This is a flexible class for comparing pairs of sequences of any type, so long |
| 16 | as the sequence elements are hashable. The basic algorithm predates, and is a |
| 17 | little fancier than, an algorithm published in the late 1980's by Ratcliff and |
| 18 | Obershelp under the hyperbolic name "gestalt pattern matching." The idea is to |
| 19 | find the longest contiguous matching subsequence that contains no "junk" |
| 20 | elements (the Ratcliff and Obershelp algorithm doesn't address junk). The same |
| 21 | idea is then applied recursively to the pieces of the sequences to the left and |
| 22 | to the right of the matching subsequence. This does not yield minimal edit |
| 23 | sequences, but does tend to yield matches that "look right" to people. |
| 24 | |
| 25 | **Timing:** The basic Ratcliff-Obershelp algorithm is cubic time in the worst |
| 26 | case and quadratic time in the expected case. :class:`SequenceMatcher` is |
| 27 | quadratic time for the worst case and has expected-case behavior dependent in a |
| 28 | complicated way on how many elements the sequences have in common; best case |
| 29 | time is linear. |
| 30 | |
| 31 | |
| 32 | .. class:: Differ |
| 33 | |
| 34 | This is a class for comparing sequences of lines of text, and producing |
| 35 | human-readable differences or deltas. Differ uses :class:`SequenceMatcher` |
| 36 | both to compare sequences of lines, and to compare sequences of characters |
| 37 | within similar (near-matching) lines. |
| 38 | |
| 39 | Each line of a :class:`Differ` delta begins with a two-letter code: |
| 40 | |
| 41 | +----------+-------------------------------------------+ |
| 42 | | Code | Meaning | |
| 43 | +==========+===========================================+ |
| 44 | | ``'- '`` | line unique to sequence 1 | |
| 45 | +----------+-------------------------------------------+ |
| 46 | | ``'+ '`` | line unique to sequence 2 | |
| 47 | +----------+-------------------------------------------+ |
| 48 | | ``' '`` | line common to both sequences | |
| 49 | +----------+-------------------------------------------+ |
| 50 | | ``'? '`` | line not present in either input sequence | |
| 51 | +----------+-------------------------------------------+ |
| 52 | |
| 53 | Lines beginning with '``?``' attempt to guide the eye to intraline differences, |
| 54 | and were not present in either input sequence. These lines can be confusing if |
| 55 | the sequences contain tab characters. |
| 56 | |
| 57 | |
| 58 | .. class:: HtmlDiff |
| 59 | |
| 60 | This class can be used to create an HTML table (or a complete HTML file |
| 61 | containing the table) showing a side by side, line by line comparison of text |
| 62 | with inter-line and intra-line change highlights. The table can be generated in |
| 63 | either full or contextual difference mode. |
| 64 | |
| 65 | The constructor for this class is: |
| 66 | |
| 67 | |
| 68 | .. function:: __init__([tabsize][, wrapcolumn][, linejunk][, charjunk]) |
| 69 | |
| 70 | Initializes instance of :class:`HtmlDiff`. |
| 71 | |
| 72 | *tabsize* is an optional keyword argument to specify tab stop spacing and |
| 73 | defaults to ``8``. |
| 74 | |
| 75 | *wrapcolumn* is an optional keyword to specify column number where lines are |
| 76 | broken and wrapped, defaults to ``None`` where lines are not wrapped. |
| 77 | |
| 78 | *linejunk* and *charjunk* are optional keyword arguments passed into ``ndiff()`` |
| 79 | (used by :class:`HtmlDiff` to generate the side by side HTML differences). See |
| 80 | ``ndiff()`` documentation for argument default values and descriptions. |
| 81 | |
| 82 | The following methods are public: |
| 83 | |
| 84 | |
| 85 | .. function:: make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines]) |
| 86 | |
| 87 | Compares *fromlines* and *tolines* (lists of strings) and returns a string which |
| 88 | is a complete HTML file containing a table showing line by line differences with |
| 89 | inter-line and intra-line changes highlighted. |
| 90 | |
| 91 | *fromdesc* and *todesc* are optional keyword arguments to specify from/to file |
| 92 | column header strings (both default to an empty string). |
| 93 | |
| 94 | *context* and *numlines* are both optional keyword arguments. Set *context* to |
| 95 | ``True`` when contextual differences are to be shown, else the default is |
| 96 | ``False`` to show the full files. *numlines* defaults to ``5``. When *context* |
| 97 | is ``True`` *numlines* controls the number of context lines which surround the |
| 98 | difference highlights. When *context* is ``False`` *numlines* controls the |
| 99 | number of lines which are shown before a difference highlight when using the |
| 100 | "next" hyperlinks (setting to zero would cause the "next" hyperlinks to place |
| 101 | the next difference highlight at the top of the browser without any leading |
| 102 | context). |
| 103 | |
| 104 | |
| 105 | .. function:: make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines]) |
| 106 | |
| 107 | Compares *fromlines* and *tolines* (lists of strings) and returns a string which |
| 108 | is a complete HTML table showing line by line differences with inter-line and |
| 109 | intra-line changes highlighted. |
| 110 | |
| 111 | The arguments for this method are the same as those for the :meth:`make_file` |
| 112 | method. |
| 113 | |
| 114 | :file:`Tools/scripts/diff.py` is a command-line front-end to this class and |
| 115 | contains a good example of its use. |
| 116 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | |
| 118 | .. function:: context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]) |
| 119 | |
| 120 | Compare *a* and *b* (lists of strings); return a delta (a generator generating |
| 121 | the delta lines) in context diff format. |
| 122 | |
| 123 | Context diffs are a compact way of showing just the lines that have changed plus |
| 124 | a few lines of context. The changes are shown in a before/after style. The |
| 125 | number of context lines is set by *n* which defaults to three. |
| 126 | |
| 127 | By default, the diff control lines (those with ``***`` or ``---``) are created |
| 128 | with a trailing newline. This is helpful so that inputs created from |
| 129 | :func:`file.readlines` result in diffs that are suitable for use with |
| 130 | :func:`file.writelines` since both the inputs and outputs have trailing |
| 131 | newlines. |
| 132 | |
| 133 | For inputs that do not have trailing newlines, set the *lineterm* argument to |
| 134 | ``""`` so that the output will be uniformly newline free. |
| 135 | |
| 136 | The context diff format normally has a header for filenames and modification |
| 137 | times. Any or all of these may be specified using strings for *fromfile*, |
| 138 | *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally |
| 139 | expressed in the format returned by :func:`time.ctime`. If not specified, the |
| 140 | strings default to blanks. |
| 141 | |
| 142 | :file:`Tools/scripts/diff.py` is a command-line front-end for this function. |
| 143 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
| 145 | .. function:: get_close_matches(word, possibilities[, n][, cutoff]) |
| 146 | |
| 147 | Return a list of the best "good enough" matches. *word* is a sequence for which |
| 148 | close matches are desired (typically a string), and *possibilities* is a list of |
| 149 | sequences against which to match *word* (typically a list of strings). |
| 150 | |
| 151 | Optional argument *n* (default ``3``) is the maximum number of close matches to |
| 152 | return; *n* must be greater than ``0``. |
| 153 | |
| 154 | Optional argument *cutoff* (default ``0.6``) is a float in the range [0, 1]. |
| 155 | Possibilities that don't score at least that similar to *word* are ignored. |
| 156 | |
| 157 | The best (no more than *n*) matches among the possibilities are returned in a |
| 158 | list, sorted by similarity score, most similar first. :: |
| 159 | |
| 160 | >>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) |
| 161 | ['apple', 'ape'] |
| 162 | >>> import keyword |
| 163 | >>> get_close_matches('wheel', keyword.kwlist) |
| 164 | ['while'] |
| 165 | >>> get_close_matches('apple', keyword.kwlist) |
| 166 | [] |
| 167 | >>> get_close_matches('accept', keyword.kwlist) |
| 168 | ['except'] |
| 169 | |
| 170 | |
| 171 | .. function:: ndiff(a, b[, linejunk][, charjunk]) |
| 172 | |
| 173 | Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style delta |
| 174 | (a generator generating the delta lines). |
| 175 | |
| 176 | Optional keyword parameters *linejunk* and *charjunk* are for filter functions |
| 177 | (or ``None``): |
| 178 | |
| 179 | *linejunk*: A function that accepts a single string argument, and returns true |
| 180 | if the string is junk, or false if not. The default is (``None``), starting with |
| 181 | Python 2.3. Before then, the default was the module-level function |
| 182 | :func:`IS_LINE_JUNK`, which filters out lines without visible characters, except |
| 183 | for at most one pound character (``'#'``). As of Python 2.3, the underlying |
| 184 | :class:`SequenceMatcher` class does a dynamic analysis of which lines are so |
| 185 | frequent as to constitute noise, and this usually works better than the pre-2.3 |
| 186 | default. |
| 187 | |
| 188 | *charjunk*: A function that accepts a character (a string of length 1), and |
| 189 | returns if the character is junk, or false if not. The default is module-level |
| 190 | function :func:`IS_CHARACTER_JUNK`, which filters out whitespace characters (a |
| 191 | blank or tab; note: bad idea to include newline in this!). |
| 192 | |
| 193 | :file:`Tools/scripts/ndiff.py` is a command-line front-end to this function. :: |
| 194 | |
| 195 | >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), |
| 196 | ... 'ore\ntree\nemu\n'.splitlines(1)) |
| 197 | >>> print ''.join(diff), |
| 198 | - one |
| 199 | ? ^ |
| 200 | + ore |
| 201 | ? ^ |
| 202 | - two |
| 203 | - three |
| 204 | ? - |
| 205 | + tree |
| 206 | + emu |
| 207 | |
| 208 | |
| 209 | .. function:: restore(sequence, which) |
| 210 | |
| 211 | Return one of the two sequences that generated a delta. |
| 212 | |
| 213 | Given a *sequence* produced by :meth:`Differ.compare` or :func:`ndiff`, extract |
| 214 | lines originating from file 1 or 2 (parameter *which*), stripping off line |
| 215 | prefixes. |
| 216 | |
| 217 | Example:: |
| 218 | |
| 219 | >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), |
| 220 | ... 'ore\ntree\nemu\n'.splitlines(1)) |
| 221 | >>> diff = list(diff) # materialize the generated delta into a list |
| 222 | >>> print ''.join(restore(diff, 1)), |
| 223 | one |
| 224 | two |
| 225 | three |
| 226 | >>> print ''.join(restore(diff, 2)), |
| 227 | ore |
| 228 | tree |
| 229 | emu |
| 230 | |
| 231 | |
| 232 | .. function:: unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]) |
| 233 | |
| 234 | Compare *a* and *b* (lists of strings); return a delta (a generator generating |
| 235 | the delta lines) in unified diff format. |
| 236 | |
| 237 | Unified diffs are a compact way of showing just the lines that have changed plus |
| 238 | a few lines of context. The changes are shown in a inline style (instead of |
| 239 | separate before/after blocks). The number of context lines is set by *n* which |
| 240 | defaults to three. |
| 241 | |
| 242 | By default, the diff control lines (those with ``---``, ``+++``, or ``@@``) are |
| 243 | created with a trailing newline. This is helpful so that inputs created from |
| 244 | :func:`file.readlines` result in diffs that are suitable for use with |
| 245 | :func:`file.writelines` since both the inputs and outputs have trailing |
| 246 | newlines. |
| 247 | |
| 248 | For inputs that do not have trailing newlines, set the *lineterm* argument to |
| 249 | ``""`` so that the output will be uniformly newline free. |
| 250 | |
| 251 | The context diff format normally has a header for filenames and modification |
| 252 | times. Any or all of these may be specified using strings for *fromfile*, |
| 253 | *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally |
| 254 | expressed in the format returned by :func:`time.ctime`. If not specified, the |
| 255 | strings default to blanks. |
| 256 | |
| 257 | :file:`Tools/scripts/diff.py` is a command-line front-end for this function. |
| 258 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 259 | |
| 260 | .. function:: IS_LINE_JUNK(line) |
| 261 | |
| 262 | Return true for ignorable lines. The line *line* is ignorable if *line* is |
| 263 | blank or contains a single ``'#'``, otherwise it is not ignorable. Used as a |
| 264 | default for parameter *linejunk* in :func:`ndiff` before Python 2.3. |
| 265 | |
| 266 | |
| 267 | .. function:: IS_CHARACTER_JUNK(ch) |
| 268 | |
| 269 | Return true for ignorable characters. The character *ch* is ignorable if *ch* |
| 270 | is a space or tab, otherwise it is not ignorable. Used as a default for |
| 271 | parameter *charjunk* in :func:`ndiff`. |
| 272 | |
| 273 | |
| 274 | .. seealso:: |
| 275 | |
| 276 | `Pattern Matching: The Gestalt Approach <http://www.ddj.com/184407970?pgno=5>`_ |
| 277 | Discussion of a similar algorithm by John W. Ratcliff and D. E. Metzener. This |
| 278 | was published in `Dr. Dobb's Journal <http://www.ddj.com/>`_ in July, 1988. |
| 279 | |
| 280 | |
| 281 | .. _sequence-matcher: |
| 282 | |
| 283 | SequenceMatcher Objects |
| 284 | ----------------------- |
| 285 | |
| 286 | The :class:`SequenceMatcher` class has this constructor: |
| 287 | |
| 288 | |
| 289 | .. class:: SequenceMatcher([isjunk[, a[, b]]]) |
| 290 | |
| 291 | Optional argument *isjunk* must be ``None`` (the default) or a one-argument |
| 292 | function that takes a sequence element and returns true if and only if the |
| 293 | element is "junk" and should be ignored. Passing ``None`` for *isjunk* is |
| 294 | equivalent to passing ``lambda x: 0``; in other words, no elements are ignored. |
| 295 | For example, pass:: |
| 296 | |
| 297 | lambda x: x in " \t" |
| 298 | |
| 299 | if you're comparing lines as sequences of characters, and don't want to synch up |
| 300 | on blanks or hard tabs. |
| 301 | |
| 302 | The optional arguments *a* and *b* are sequences to be compared; both default to |
| 303 | empty strings. The elements of both sequences must be hashable. |
| 304 | |
| 305 | :class:`SequenceMatcher` objects have the following methods: |
| 306 | |
| 307 | |
| 308 | .. method:: SequenceMatcher.set_seqs(a, b) |
| 309 | |
| 310 | Set the two sequences to be compared. |
| 311 | |
| 312 | :class:`SequenceMatcher` computes and caches detailed information about the |
| 313 | second sequence, so if you want to compare one sequence against many sequences, |
| 314 | use :meth:`set_seq2` to set the commonly used sequence once and call |
| 315 | :meth:`set_seq1` repeatedly, once for each of the other sequences. |
| 316 | |
| 317 | |
| 318 | .. method:: SequenceMatcher.set_seq1(a) |
| 319 | |
| 320 | Set the first sequence to be compared. The second sequence to be compared is |
| 321 | not changed. |
| 322 | |
| 323 | |
| 324 | .. method:: SequenceMatcher.set_seq2(b) |
| 325 | |
| 326 | Set the second sequence to be compared. The first sequence to be compared is |
| 327 | not changed. |
| 328 | |
| 329 | |
| 330 | .. method:: SequenceMatcher.find_longest_match(alo, ahi, blo, bhi) |
| 331 | |
| 332 | Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``. |
| 333 | |
| 334 | If *isjunk* was omitted or ``None``, :meth:`get_longest_match` returns ``(i, j, |
| 335 | k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo <= i <= i+k <= |
| 336 | ahi`` and ``blo <= j <= j+k <= bhi``. For all ``(i', j', k')`` meeting those |
| 337 | conditions, the additional conditions ``k >= k'``, ``i <= i'``, and if ``i == |
| 338 | i'``, ``j <= j'`` are also met. In other words, of all maximal matching blocks, |
| 339 | return one that starts earliest in *a*, and of all those maximal matching blocks |
| 340 | that start earliest in *a*, return the one that starts earliest in *b*. :: |
| 341 | |
| 342 | >>> s = SequenceMatcher(None, " abcd", "abcd abcd") |
| 343 | >>> s.find_longest_match(0, 5, 0, 9) |
| 344 | (0, 4, 5) |
| 345 | |
| 346 | If *isjunk* was provided, first the longest matching block is determined as |
| 347 | above, but with the additional restriction that no junk element appears in the |
| 348 | block. Then that block is extended as far as possible by matching (only) junk |
| 349 | elements on both sides. So the resulting block never matches on junk except as |
| 350 | identical junk happens to be adjacent to an interesting match. |
| 351 | |
| 352 | Here's the same example as before, but considering blanks to be junk. That |
| 353 | prevents ``' abcd'`` from matching the ``' abcd'`` at the tail end of the second |
| 354 | sequence directly. Instead only the ``'abcd'`` can match, and matches the |
| 355 | leftmost ``'abcd'`` in the second sequence:: |
| 356 | |
| 357 | >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") |
| 358 | >>> s.find_longest_match(0, 5, 0, 9) |
| 359 | (1, 0, 4) |
| 360 | |
| 361 | If no blocks match, this returns ``(alo, blo, 0)``. |
| 362 | |
| 363 | |
| 364 | .. method:: SequenceMatcher.get_matching_blocks() |
| 365 | |
| 366 | Return list of triples describing matching subsequences. Each triple is of the |
| 367 | form ``(i, j, n)``, and means that ``a[i:i+n] == b[j:j+n]``. The triples are |
| 368 | monotonically increasing in *i* and *j*. |
| 369 | |
| 370 | The last triple is a dummy, and has the value ``(len(a), len(b), 0)``. It is |
| 371 | the only triple with ``n == 0``. If ``(i, j, n)`` and ``(i', j', n')`` are |
| 372 | adjacent triples in the list, and the second is not the last triple in the list, |
| 373 | then ``i+n != i'`` or ``j+n != j'``; in other words, adjacent triples always |
| 374 | describe non-adjacent equal blocks. |
| 375 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 376 | :: |
| 377 | |
| 378 | >>> s = SequenceMatcher(None, "abxcd", "abcd") |
| 379 | >>> s.get_matching_blocks() |
| 380 | [(0, 0, 2), (3, 2, 2), (5, 4, 0)] |
| 381 | |
| 382 | |
| 383 | .. method:: SequenceMatcher.get_opcodes() |
| 384 | |
| 385 | Return list of 5-tuples describing how to turn *a* into *b*. Each tuple is of |
| 386 | the form ``(tag, i1, i2, j1, j2)``. The first tuple has ``i1 == j1 == 0``, and |
| 387 | remaining tuples have *i1* equal to the *i2* from the preceding tuple, and, |
| 388 | likewise, *j1* equal to the previous *j2*. |
| 389 | |
| 390 | The *tag* values are strings, with these meanings: |
| 391 | |
| 392 | +---------------+---------------------------------------------+ |
| 393 | | Value | Meaning | |
| 394 | +===============+=============================================+ |
| 395 | | ``'replace'`` | ``a[i1:i2]`` should be replaced by | |
| 396 | | | ``b[j1:j2]``. | |
| 397 | +---------------+---------------------------------------------+ |
| 398 | | ``'delete'`` | ``a[i1:i2]`` should be deleted. Note that | |
| 399 | | | ``j1 == j2`` in this case. | |
| 400 | +---------------+---------------------------------------------+ |
| 401 | | ``'insert'`` | ``b[j1:j2]`` should be inserted at | |
| 402 | | | ``a[i1:i1]``. Note that ``i1 == i2`` in | |
| 403 | | | this case. | |
| 404 | +---------------+---------------------------------------------+ |
| 405 | | ``'equal'`` | ``a[i1:i2] == b[j1:j2]`` (the sub-sequences | |
| 406 | | | are equal). | |
| 407 | +---------------+---------------------------------------------+ |
| 408 | |
| 409 | For example:: |
| 410 | |
| 411 | >>> a = "qabxcd" |
| 412 | >>> b = "abycdf" |
| 413 | >>> s = SequenceMatcher(None, a, b) |
| 414 | >>> for tag, i1, i2, j1, j2 in s.get_opcodes(): |
| 415 | ... print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % |
| 416 | ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])) |
| 417 | delete a[0:1] (q) b[0:0] () |
| 418 | equal a[1:3] (ab) b[0:2] (ab) |
| 419 | replace a[3:4] (x) b[2:3] (y) |
| 420 | equal a[4:6] (cd) b[3:5] (cd) |
| 421 | insert a[6:6] () b[5:6] (f) |
| 422 | |
| 423 | |
| 424 | .. method:: SequenceMatcher.get_grouped_opcodes([n]) |
| 425 | |
| 426 | Return a generator of groups with up to *n* lines of context. |
| 427 | |
| 428 | Starting with the groups returned by :meth:`get_opcodes`, this method splits out |
| 429 | smaller change clusters and eliminates intervening ranges which have no changes. |
| 430 | |
| 431 | The groups are returned in the same format as :meth:`get_opcodes`. |
| 432 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | |
| 434 | .. method:: SequenceMatcher.ratio() |
| 435 | |
| 436 | Return a measure of the sequences' similarity as a float in the range [0, 1]. |
| 437 | |
| 438 | Where T is the total number of elements in both sequences, and M is the number |
| 439 | of matches, this is 2.0\*M / T. Note that this is ``1.0`` if the sequences are |
| 440 | identical, and ``0.0`` if they have nothing in common. |
| 441 | |
| 442 | This is expensive to compute if :meth:`get_matching_blocks` or |
| 443 | :meth:`get_opcodes` hasn't already been called, in which case you may want to |
| 444 | try :meth:`quick_ratio` or :meth:`real_quick_ratio` first to get an upper bound. |
| 445 | |
| 446 | |
| 447 | .. method:: SequenceMatcher.quick_ratio() |
| 448 | |
| 449 | Return an upper bound on :meth:`ratio` relatively quickly. |
| 450 | |
| 451 | This isn't defined beyond that it is an upper bound on :meth:`ratio`, and is |
| 452 | faster to compute. |
| 453 | |
| 454 | |
| 455 | .. method:: SequenceMatcher.real_quick_ratio() |
| 456 | |
| 457 | Return an upper bound on :meth:`ratio` very quickly. |
| 458 | |
| 459 | This isn't defined beyond that it is an upper bound on :meth:`ratio`, and is |
| 460 | faster to compute than either :meth:`ratio` or :meth:`quick_ratio`. |
| 461 | |
| 462 | The three methods that return the ratio of matching to total characters can give |
| 463 | different results due to differing levels of approximation, although |
| 464 | :meth:`quick_ratio` and :meth:`real_quick_ratio` are always at least as large as |
| 465 | :meth:`ratio`:: |
| 466 | |
| 467 | >>> s = SequenceMatcher(None, "abcd", "bcde") |
| 468 | >>> s.ratio() |
| 469 | 0.75 |
| 470 | >>> s.quick_ratio() |
| 471 | 0.75 |
| 472 | >>> s.real_quick_ratio() |
| 473 | 1.0 |
| 474 | |
| 475 | |
| 476 | .. _sequencematcher-examples: |
| 477 | |
| 478 | SequenceMatcher Examples |
| 479 | ------------------------ |
| 480 | |
| 481 | This example compares two strings, considering blanks to be "junk:" :: |
| 482 | |
| 483 | >>> s = SequenceMatcher(lambda x: x == " ", |
| 484 | ... "private Thread currentThread;", |
| 485 | ... "private volatile Thread currentThread;") |
| 486 | |
| 487 | :meth:`ratio` returns a float in [0, 1], measuring the similarity of the |
| 488 | sequences. As a rule of thumb, a :meth:`ratio` value over 0.6 means the |
| 489 | sequences are close matches:: |
| 490 | |
| 491 | >>> print round(s.ratio(), 3) |
| 492 | 0.866 |
| 493 | |
| 494 | If you're only interested in where the sequences match, |
| 495 | :meth:`get_matching_blocks` is handy:: |
| 496 | |
| 497 | >>> for block in s.get_matching_blocks(): |
| 498 | ... print "a[%d] and b[%d] match for %d elements" % block |
| 499 | a[0] and b[0] match for 8 elements |
| 500 | a[8] and b[17] match for 6 elements |
| 501 | a[14] and b[23] match for 15 elements |
| 502 | a[29] and b[38] match for 0 elements |
| 503 | |
| 504 | Note that the last tuple returned by :meth:`get_matching_blocks` is always a |
| 505 | dummy, ``(len(a), len(b), 0)``, and this is the only case in which the last |
| 506 | tuple element (number of elements matched) is ``0``. |
| 507 | |
| 508 | If you want to know how to change the first sequence into the second, use |
| 509 | :meth:`get_opcodes`:: |
| 510 | |
| 511 | >>> for opcode in s.get_opcodes(): |
| 512 | ... print "%6s a[%d:%d] b[%d:%d]" % opcode |
| 513 | equal a[0:8] b[0:8] |
| 514 | insert a[8:8] b[8:17] |
| 515 | equal a[8:14] b[17:23] |
| 516 | equal a[14:29] b[23:38] |
| 517 | |
| 518 | See also the function :func:`get_close_matches` in this module, which shows how |
| 519 | simple code building on :class:`SequenceMatcher` can be used to do useful work. |
| 520 | |
| 521 | |
| 522 | .. _differ-objects: |
| 523 | |
| 524 | Differ Objects |
| 525 | -------------- |
| 526 | |
| 527 | Note that :class:`Differ`\ -generated deltas make no claim to be **minimal** |
| 528 | diffs. To the contrary, minimal diffs are often counter-intuitive, because they |
| 529 | synch up anywhere possible, sometimes accidental matches 100 pages apart. |
| 530 | Restricting synch points to contiguous matches preserves some notion of |
| 531 | locality, at the occasional cost of producing a longer diff. |
| 532 | |
| 533 | The :class:`Differ` class has this constructor: |
| 534 | |
| 535 | |
| 536 | .. class:: Differ([linejunk[, charjunk]]) |
| 537 | |
| 538 | Optional keyword parameters *linejunk* and *charjunk* are for filter functions |
| 539 | (or ``None``): |
| 540 | |
| 541 | *linejunk*: A function that accepts a single string argument, and returns true |
| 542 | if the string is junk. The default is ``None``, meaning that no line is |
| 543 | considered junk. |
| 544 | |
| 545 | *charjunk*: A function that accepts a single character argument (a string of |
| 546 | length 1), and returns true if the character is junk. The default is ``None``, |
| 547 | meaning that no character is considered junk. |
| 548 | |
| 549 | :class:`Differ` objects are used (deltas generated) via a single method: |
| 550 | |
| 551 | |
| 552 | .. method:: Differ.compare(a, b) |
| 553 | |
| 554 | Compare two sequences of lines, and generate the delta (a sequence of lines). |
| 555 | |
| 556 | Each sequence must contain individual single-line strings ending with newlines. |
| 557 | Such sequences can be obtained from the :meth:`readlines` method of file-like |
| 558 | objects. The delta generated also consists of newline-terminated strings, ready |
| 559 | to be printed as-is via the :meth:`writelines` method of a file-like object. |
| 560 | |
| 561 | |
| 562 | .. _differ-examples: |
| 563 | |
| 564 | Differ Example |
| 565 | -------------- |
| 566 | |
| 567 | This example compares two texts. First we set up the texts, sequences of |
| 568 | individual single-line strings ending with newlines (such sequences can also be |
| 569 | obtained from the :meth:`readlines` method of file-like objects):: |
| 570 | |
| 571 | >>> text1 = ''' 1. Beautiful is better than ugly. |
| 572 | ... 2. Explicit is better than implicit. |
| 573 | ... 3. Simple is better than complex. |
| 574 | ... 4. Complex is better than complicated. |
| 575 | ... '''.splitlines(1) |
| 576 | >>> len(text1) |
| 577 | 4 |
| 578 | >>> text1[0][-1] |
| 579 | '\n' |
| 580 | >>> text2 = ''' 1. Beautiful is better than ugly. |
| 581 | ... 3. Simple is better than complex. |
| 582 | ... 4. Complicated is better than complex. |
| 583 | ... 5. Flat is better than nested. |
| 584 | ... '''.splitlines(1) |
| 585 | |
| 586 | Next we instantiate a Differ object:: |
| 587 | |
| 588 | >>> d = Differ() |
| 589 | |
| 590 | Note that when instantiating a :class:`Differ` object we may pass functions to |
| 591 | filter out line and character "junk." See the :meth:`Differ` constructor for |
| 592 | details. |
| 593 | |
| 594 | Finally, we compare the two:: |
| 595 | |
| 596 | >>> result = list(d.compare(text1, text2)) |
| 597 | |
| 598 | ``result`` is a list of strings, so let's pretty-print it:: |
| 599 | |
| 600 | >>> from pprint import pprint |
| 601 | >>> pprint(result) |
| 602 | [' 1. Beautiful is better than ugly.\n', |
| 603 | '- 2. Explicit is better than implicit.\n', |
| 604 | '- 3. Simple is better than complex.\n', |
| 605 | '+ 3. Simple is better than complex.\n', |
| 606 | '? ++ \n', |
| 607 | '- 4. Complex is better than complicated.\n', |
| 608 | '? ^ ---- ^ \n', |
| 609 | '+ 4. Complicated is better than complex.\n', |
| 610 | '? ++++ ^ ^ \n', |
| 611 | '+ 5. Flat is better than nested.\n'] |
| 612 | |
| 613 | As a single multi-line string it looks like this:: |
| 614 | |
| 615 | >>> import sys |
| 616 | >>> sys.stdout.writelines(result) |
| 617 | 1. Beautiful is better than ugly. |
| 618 | - 2. Explicit is better than implicit. |
| 619 | - 3. Simple is better than complex. |
| 620 | + 3. Simple is better than complex. |
| 621 | ? ++ |
| 622 | - 4. Complex is better than complicated. |
| 623 | ? ^ ---- ^ |
| 624 | + 4. Complicated is better than complex. |
| 625 | ? ++++ ^ ^ |
| 626 | + 5. Flat is better than nested. |
| 627 | |