Mercurial > cpython-lru_cache-weakref
annotate Lib/difflib.py @ 24042:63fbbed10714 legacy-trunk
Simplify HTTPSConnection constructor.
See discussion in SF bug 458463.
| author | Jeremy Hylton <jeremy@alum.mit.edu> |
|---|---|
| date | Fri, 28 Jun 2002 23:38:14 +0000 |
| parents | a1de0bab5475 |
| children | 18edf23bf499 |
| rev | line source |
|---|---|
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4 Module difflib -- helpers for computing deltas between objects. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
5 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
6 Function get_close_matches(word, possibilities, n=3, cutoff=0.6): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
7 Use SequenceMatcher to return list of the best "good enough" matches. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
8 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
9 Function ndiff(a, b): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
10 Return a delta: the difference between `a` and `b` (lists of strings). |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
11 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
12 Function restore(delta, which): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
13 Return one of the two sequences that generated an ndiff delta. |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
14 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
15 Class SequenceMatcher: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
16 A flexible class for comparing pairs of sequences of any type. |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
17 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
18 Class Differ: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
19 For producing human-readable deltas from sequences of lines of text. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
20 """ |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
21 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
22 __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
23 'Differ'] |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
24 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
25 class SequenceMatcher: |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
26 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
27 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
28 SequenceMatcher is a flexible class for comparing pairs of sequences of |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
29 any type, so long as the sequence elements are hashable. The basic |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
30 algorithm predates, and is a little fancier than, an algorithm |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
31 published in the late 1980's by Ratcliff and Obershelp under the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
32 hyperbolic name "gestalt pattern matching". The basic idea is to find |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
33 the longest contiguous matching subsequence that contains no "junk" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
34 elements (R-O doesn't address junk). The same idea is then applied |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
35 recursively to the pieces of the sequences to the left and to the right |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
36 of the matching subsequence. This does not yield minimal edit |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
37 sequences, but does tend to yield matches that "look right" to people. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
38 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
39 SequenceMatcher tries to compute a "human-friendly diff" between two |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
40 sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
41 longest *contiguous* & junk-free matching subsequence. That's what |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
42 catches peoples' eyes. The Windows(tm) windiff has another interesting |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
43 notion, pairing up elements that appear uniquely in each sequence. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
44 That, and the method here, appear to yield more intuitive difference |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
45 reports than does diff. This method appears to be the least vulnerable |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
46 to synching up on blocks of "junk lines", though (like blank lines in |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
47 ordinary text files, or maybe "<P>" lines in HTML files). That may be |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
48 because this is the only method of the 3 that has a *concept* of |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
49 "junk" <wink>. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
50 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
51 Example, comparing two strings, and considering blanks to be "junk": |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
52 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
53 >>> s = SequenceMatcher(lambda x: x == " ", |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
54 ... "private Thread currentThread;", |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
55 ... "private volatile Thread currentThread;") |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
56 >>> |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
57 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
58 .ratio() returns a float in [0, 1], measuring the "similarity" of the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
59 sequences. As a rule of thumb, a .ratio() value over 0.6 means the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
60 sequences are close matches: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
61 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
62 >>> print round(s.ratio(), 3) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
63 0.866 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
64 >>> |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
65 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
66 If you're only interested in where the sequences match, |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
67 .get_matching_blocks() is handy: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
68 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
69 >>> for block in s.get_matching_blocks(): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
70 ... print "a[%d] and b[%d] match for %d elements" % block |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
71 a[0] and b[0] match for 8 elements |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
72 a[8] and b[17] match for 6 elements |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
73 a[14] and b[23] match for 15 elements |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
74 a[29] and b[38] match for 0 elements |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
75 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
76 Note that the last tuple returned by .get_matching_blocks() is always a |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
77 dummy, (len(a), len(b), 0), and this is the only case in which the last |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
78 tuple element (number of elements matched) is 0. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
79 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
80 If you want to know how to change the first sequence into the second, |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
81 use .get_opcodes(): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
82 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
83 >>> for opcode in s.get_opcodes(): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
84 ... print "%6s a[%d:%d] b[%d:%d]" % opcode |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
85 equal a[0:8] b[0:8] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
86 insert a[8:8] b[8:17] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
87 equal a[8:14] b[17:23] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
88 equal a[14:29] b[23:38] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
89 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
90 See the Differ class for a fancy human-friendly file differencer, which |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
91 uses SequenceMatcher both to compare sequences of lines, and to compare |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
92 sequences of characters within similar (near-matching) lines. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
93 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
94 See also function get_close_matches() in this module, which shows how |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
95 simple code building on SequenceMatcher can be used to do useful work. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
96 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
97 Timing: Basic R-O is cubic time worst case and quadratic time expected |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
98 case. SequenceMatcher is quadratic time for the worst case and has |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
99 expected-case behavior dependent in a complicated way on how many |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
100 elements the sequences have in common; best case time is linear. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
101 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
102 Methods: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
103 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
104 __init__(isjunk=None, a='', b='') |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
105 Construct a SequenceMatcher. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
106 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
107 set_seqs(a, b) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
108 Set the two sequences to be compared. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
109 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
110 set_seq1(a) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
111 Set the first sequence to be compared. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
112 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
113 set_seq2(b) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
114 Set the second sequence to be compared. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
115 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
116 find_longest_match(alo, ahi, blo, bhi) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
117 Find longest matching block in a[alo:ahi] and b[blo:bhi]. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
118 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
119 get_matching_blocks() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
120 Return list of triples describing matching subsequences. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
121 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
122 get_opcodes() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
123 Return list of 5-tuples describing how to turn a into b. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
124 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
125 ratio() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
126 Return a measure of the sequences' similarity (float in [0,1]). |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
127 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
128 quick_ratio() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
129 Return an upper bound on .ratio() relatively quickly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
130 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
131 real_quick_ratio() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
132 Return an upper bound on ratio() very quickly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
133 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
134 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
135 def __init__(self, isjunk=None, a='', b=''): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
136 """Construct a SequenceMatcher. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
137 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
138 Optional arg isjunk is None (the default), or a one-argument |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
139 function that takes a sequence element and returns true iff the |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
140 element is junk. None is equivalent to passing "lambda x: 0", i.e. |
|
16695
e14c5f34fa05
Fix a few small typos in the docstrings.
Fred Drake <fdrake@acm.org>
parents:
16551
diff
changeset
|
141 no elements are considered to be junk. For example, pass |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
142 lambda x: x in " \\t" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
143 if you're comparing lines as sequences of characters, and don't |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
144 want to synch up on blanks or hard tabs. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
145 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
146 Optional arg a is the first of two sequences to be compared. By |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
147 default, an empty string. The elements of a must be hashable. See |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
148 also .set_seqs() and .set_seq1(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
149 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
150 Optional arg b is the second of two sequences to be compared. By |
|
16695
e14c5f34fa05
Fix a few small typos in the docstrings.
Fred Drake <fdrake@acm.org>
parents:
16551
diff
changeset
|
151 default, an empty string. The elements of b must be hashable. See |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
152 also .set_seqs() and .set_seq2(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
153 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
154 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
155 # Members: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
156 # a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
157 # first sequence |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
158 # b |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
159 # second sequence; differences are computed as "what do |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
160 # we need to do to 'a' to change it into 'b'?" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
161 # b2j |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
162 # for x in b, b2j[x] is a list of the indices (into b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
163 # at which x appears; junk elements do not appear |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
164 # fullbcount |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
165 # for x in b, fullbcount[x] == the number of times x |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
166 # appears in b; only materialized if really needed (used |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
167 # only for computing quick_ratio()) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
168 # matching_blocks |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
169 # a list of (i, j, k) triples, where a[i:i+k] == b[j:j+k]; |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
170 # ascending & non-overlapping in i and in j; terminated by |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
171 # a dummy (len(a), len(b), 0) sentinel |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
172 # opcodes |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
173 # a list of (tag, i1, i2, j1, j2) tuples, where tag is |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
174 # one of |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
175 # 'replace' a[i1:i2] should be replaced by b[j1:j2] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
176 # 'delete' a[i1:i2] should be deleted |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
177 # 'insert' b[j1:j2] should be inserted |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
178 # 'equal' a[i1:i2] == b[j1:j2] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
179 # isjunk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
180 # a user-supplied function taking a sequence element and |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
181 # returning true iff the element is "junk" -- this has |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
182 # subtle but helpful effects on the algorithm, which I'll |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
183 # get around to writing up someday <0.9 wink>. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
184 # DON'T USE! Only __chain_b uses this. Use isbjunk. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
185 # isbjunk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
186 # for x in b, isbjunk(x) == isjunk(x) but much faster; |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
187 # it's really the has_key method of a hidden dict. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
188 # DOES NOT WORK for x in a! |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
189 # isbpopular |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
190 # for x in b, isbpopular(x) is true iff b is reasonably long |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
191 # (at least 200 elements) and x accounts for more than 1% of |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
192 # its elements. DOES NOT WORK for x in a! |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
193 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
194 self.isjunk = isjunk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
195 self.a = self.b = None |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
196 self.set_seqs(a, b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
197 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
198 def set_seqs(self, a, b): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
199 """Set the two sequences to be compared. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
200 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
201 >>> s = SequenceMatcher() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
202 >>> s.set_seqs("abcd", "bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
203 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
204 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
205 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
206 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
207 self.set_seq1(a) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
208 self.set_seq2(b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
209 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
210 def set_seq1(self, a): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
211 """Set the first sequence to be compared. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
212 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
213 The second sequence to be compared is not changed. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
214 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
215 >>> s = SequenceMatcher(None, "abcd", "bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
216 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
217 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
218 >>> s.set_seq1("bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
219 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
220 1.0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
221 >>> |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
222 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
223 SequenceMatcher computes and caches detailed information about the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
224 second sequence, so if you want to compare one sequence S against |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
225 many sequences, use .set_seq2(S) once and call .set_seq1(x) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
226 repeatedly for each of the other sequences. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
227 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
228 See also set_seqs() and set_seq2(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
229 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
230 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
231 if a is self.a: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
232 return |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
233 self.a = a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
234 self.matching_blocks = self.opcodes = None |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
235 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
236 def set_seq2(self, b): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
237 """Set the second sequence to be compared. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
238 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
239 The first sequence to be compared is not changed. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
240 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
241 >>> s = SequenceMatcher(None, "abcd", "bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
242 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
243 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
244 >>> s.set_seq2("abcd") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
245 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
246 1.0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
247 >>> |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
248 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
249 SequenceMatcher computes and caches detailed information about the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
250 second sequence, so if you want to compare one sequence S against |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
251 many sequences, use .set_seq2(S) once and call .set_seq1(x) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
252 repeatedly for each of the other sequences. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
253 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
254 See also set_seqs() and set_seq1(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
255 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
256 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
257 if b is self.b: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
258 return |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
259 self.b = b |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
260 self.matching_blocks = self.opcodes = None |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
261 self.fullbcount = None |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
262 self.__chain_b() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
263 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
264 # For each element x in b, set b2j[x] to a list of the indices in |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
265 # b where x appears; the indices are in increasing order; note that |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
266 # the number of times x appears in b is len(b2j[x]) ... |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
267 # when self.isjunk is defined, junk elements don't show up in this |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
268 # map at all, which stops the central find_longest_match method |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
269 # from starting any matching block at a junk element ... |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
270 # also creates the fast isbjunk function ... |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
271 # b2j also does not contain entries for "popular" elements, meaning |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
272 # elements that account for more than 1% of the total elements, and |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
273 # when the sequence is reasonably large (>= 200 elements); this can |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
274 # be viewed as an adaptive notion of semi-junk, and yields an enormous |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
275 # speedup when, e.g., comparing program files with hundreds of |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
276 # instances of "return NULL;" ... |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
277 # note that this is only called when b changes; so for cross-product |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
278 # kinds of matches, it's best to call set_seq2 once, then set_seq1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
279 # repeatedly |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
280 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
281 def __chain_b(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
282 # Because isjunk is a user-defined (not C) function, and we test |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
283 # for junk a LOT, it's important to minimize the number of calls. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
284 # Before the tricks described here, __chain_b was by far the most |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
285 # time-consuming routine in the whole module! If anyone sees |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
286 # Jim Roskind, thank him again for profile.py -- I never would |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
287 # have guessed that. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
288 # The first trick is to build b2j ignoring the possibility |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
289 # of junk. I.e., we don't call isjunk at all yet. Throwing |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
290 # out the junk later is much cheaper than building b2j "right" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
291 # from the start. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
292 b = self.b |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
293 n = len(b) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
294 self.b2j = b2j = {} |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
295 populardict = {} |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
296 for i, elt in enumerate(b): |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
297 if elt in b2j: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
298 indices = b2j[elt] |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
299 if n >= 200 and len(indices) * 100 > n: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
300 populardict[elt] = 1 |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
301 del indices[:] |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
302 else: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
303 indices.append(i) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
304 else: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
305 b2j[elt] = [i] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
306 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
307 # Purge leftover indices for popular elements. |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
308 for elt in populardict: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
309 del b2j[elt] |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
310 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
311 # Now b2j.keys() contains elements uniquely, and especially when |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
312 # the sequence is a string, that's usually a good deal smaller |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
313 # than len(string). The difference is the number of isjunk calls |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
314 # saved. |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
315 isjunk = self.isjunk |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
316 junkdict = {} |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
317 if isjunk: |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
318 for d in populardict, b2j: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
319 for elt in d.keys(): |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
320 if isjunk(elt): |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
321 junkdict[elt] = 1 |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
322 del d[elt] |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
323 |
|
23620
a1de0bab5475
SF 563203. Replaced 'has_key()' with 'in'.
Raymond Hettinger <python@rcn.com>
parents:
23300
diff
changeset
|
324 # Now for x in b, isjunk(x) == x in junkdict, but the |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
325 # latter is much faster. Note too that while there may be a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
326 # lot of junk in the sequence, the number of *unique* junk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
327 # elements is probably small. So the memory burden of keeping |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
328 # this dict alive is likely trivial compared to the size of b2j. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
329 self.isbjunk = junkdict.has_key |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
330 self.isbpopular = populardict.has_key |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
331 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
332 def find_longest_match(self, alo, ahi, blo, bhi): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
333 """Find longest matching block in a[alo:ahi] and b[blo:bhi]. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
334 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
335 If isjunk is not defined: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
336 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
337 Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
338 alo <= i <= i+k <= ahi |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
339 blo <= j <= j+k <= bhi |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
340 and for all (i',j',k') meeting those conditions, |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
341 k >= k' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
342 i <= i' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
343 and if i == i', j <= j' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
344 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
345 In other words, of all maximal matching blocks, return one that |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
346 starts earliest in a, and of all those maximal matching blocks that |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
347 start earliest in a, return the one that starts earliest in b. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
348 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
349 >>> s = SequenceMatcher(None, " abcd", "abcd abcd") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
350 >>> s.find_longest_match(0, 5, 0, 9) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
351 (0, 4, 5) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
352 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
353 If isjunk is defined, first the longest matching block is |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
354 determined as above, but with the additional restriction that no |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
355 junk element appears in the block. Then that block is extended as |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
356 far as possible by matching (only) junk elements on both sides. So |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
357 the resulting block never matches on junk except as identical junk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
358 happens to be adjacent to an "interesting" match. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
359 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
360 Here's the same example as before, but considering blanks to be |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
361 junk. That prevents " abcd" from matching the " abcd" at the tail |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
362 end of the second sequence directly. Instead only the "abcd" can |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
363 match, and matches the leftmost "abcd" in the second sequence: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
364 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
365 >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
366 >>> s.find_longest_match(0, 5, 0, 9) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
367 (1, 0, 4) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
368 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
369 If no blocks match, return (alo, blo, 0). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
370 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
371 >>> s = SequenceMatcher(None, "ab", "c") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
372 >>> s.find_longest_match(0, 2, 0, 1) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
373 (0, 0, 0) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
374 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
375 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
376 # CAUTION: stripping common prefix or suffix would be incorrect. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
377 # E.g., |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
378 # ab |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
379 # acab |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
380 # Longest matching block is "ab", but if common prefix is |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
381 # stripped, it's "a" (tied with "b"). UNIX(tm) diff does so |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
382 # strip, so ends up claiming that ab is changed to acab by |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
383 # inserting "ca" in the middle. That's minimal but unintuitive: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
384 # "it's obvious" that someone inserted "ac" at the front. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
385 # Windiff ends up at the same place as diff, but by pairing up |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
386 # the unique 'b's and then matching the first two 'a's. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
387 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
388 a, b, b2j, isbjunk = self.a, self.b, self.b2j, self.isbjunk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
389 besti, bestj, bestsize = alo, blo, 0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
390 # find longest junk-free match |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
391 # during an iteration of the loop, j2len[j] = length of longest |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
392 # junk-free match ending with a[i-1] and b[j] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
393 j2len = {} |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
394 nothing = [] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
395 for i in xrange(alo, ahi): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
396 # look at all instances of a[i] in b; note that because |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
397 # b2j has no junk keys, the loop is skipped if a[i] is junk |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
398 j2lenget = j2len.get |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
399 newj2len = {} |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
400 for j in b2j.get(a[i], nothing): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
401 # a[i] matches b[j] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
402 if j < blo: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
403 continue |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
404 if j >= bhi: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
405 break |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
406 k = newj2len[j] = j2lenget(j-1, 0) + 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
407 if k > bestsize: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
408 besti, bestj, bestsize = i-k+1, j-k+1, k |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
409 j2len = newj2len |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
410 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
411 # Extend the best by non-junk elements on each end. In particular, |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
412 # "popular" non-junk elements aren't in b2j, which greatly speeds |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
413 # the inner loop above, but also means "the best" match so far |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
414 # doesn't contain any junk *or* popular non-junk elements. |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
415 while besti > alo and bestj > blo and \ |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
416 not isbjunk(b[bestj-1]) and \ |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
417 a[besti-1] == b[bestj-1]: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
418 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
419 while besti+bestsize < ahi and bestj+bestsize < bhi and \ |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
420 not isbjunk(b[bestj+bestsize]) and \ |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
421 a[besti+bestsize] == b[bestj+bestsize]: |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
422 bestsize += 1 |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
423 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
424 # Now that we have a wholly interesting match (albeit possibly |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
425 # empty!), we may as well suck up the matching junk on each |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
426 # side of it too. Can't think of a good reason not to, and it |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
427 # saves post-processing the (possibly considerable) expense of |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
428 # figuring out what to do with it. In the case of an empty |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
429 # interesting match, this is clearly the right thing to do, |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
430 # because no other kind of match is possible in the regions. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
431 while besti > alo and bestj > blo and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
432 isbjunk(b[bestj-1]) and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
433 a[besti-1] == b[bestj-1]: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
434 besti, bestj, bestsize = besti-1, bestj-1, bestsize+1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
435 while besti+bestsize < ahi and bestj+bestsize < bhi and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
436 isbjunk(b[bestj+bestsize]) and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
437 a[besti+bestsize] == b[bestj+bestsize]: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
438 bestsize = bestsize + 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
439 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
440 return besti, bestj, bestsize |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
441 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
442 def get_matching_blocks(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
443 """Return list of triples describing matching subsequences. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
444 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
445 Each triple is of the form (i, j, n), and means that |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
446 a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
447 i and in j. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
448 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
449 The last triple is a dummy, (len(a), len(b), 0), and is the only |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
450 triple with n==0. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
451 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
452 >>> s = SequenceMatcher(None, "abxcd", "abcd") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
453 >>> s.get_matching_blocks() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
454 [(0, 0, 2), (3, 2, 2), (5, 4, 0)] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
455 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
456 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
457 if self.matching_blocks is not None: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
458 return self.matching_blocks |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
459 self.matching_blocks = [] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
460 la, lb = len(self.a), len(self.b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
461 self.__helper(0, la, 0, lb, self.matching_blocks) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
462 self.matching_blocks.append( (la, lb, 0) ) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
463 return self.matching_blocks |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
464 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
465 # builds list of matching blocks covering a[alo:ahi] and |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
466 # b[blo:bhi], appending them in increasing order to answer |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
467 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
468 def __helper(self, alo, ahi, blo, bhi, answer): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
469 i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
470 # a[alo:i] vs b[blo:j] unknown |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
471 # a[i:i+k] same as b[j:j+k] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
472 # a[i+k:ahi] vs b[j+k:bhi] unknown |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
473 if k: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
474 if alo < i and blo < j: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
475 self.__helper(alo, i, blo, j, answer) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
476 answer.append(x) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
477 if i+k < ahi and j+k < bhi: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
478 self.__helper(i+k, ahi, j+k, bhi, answer) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
479 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
480 def get_opcodes(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
481 """Return list of 5-tuples describing how to turn a into b. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
482 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
483 Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
484 has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
485 tuple preceding it, and likewise for j1 == the previous j2. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
486 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
487 The tags are strings, with these meanings: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
488 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
489 'replace': a[i1:i2] should be replaced by b[j1:j2] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
490 'delete': a[i1:i2] should be deleted. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
491 Note that j1==j2 in this case. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
492 'insert': b[j1:j2] should be inserted at a[i1:i1]. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
493 Note that i1==i2 in this case. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
494 'equal': a[i1:i2] == b[j1:j2] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
495 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
496 >>> a = "qabxcd" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
497 >>> b = "abycdf" |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
498 >>> s = SequenceMatcher(None, a, b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
499 >>> for tag, i1, i2, j1, j2 in s.get_opcodes(): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
500 ... print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
501 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
502 delete a[0:1] (q) b[0:0] () |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
503 equal a[1:3] (ab) b[0:2] (ab) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
504 replace a[3:4] (x) b[2:3] (y) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
505 equal a[4:6] (cd) b[3:5] (cd) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
506 insert a[6:6] () b[5:6] (f) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
507 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
508 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
509 if self.opcodes is not None: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
510 return self.opcodes |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
511 i = j = 0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
512 self.opcodes = answer = [] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
513 for ai, bj, size in self.get_matching_blocks(): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
514 # invariant: we've pumped out correct diffs to change |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
515 # a[:i] into b[:j], and the next matching block is |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
516 # a[ai:ai+size] == b[bj:bj+size]. So we need to pump |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
517 # out a diff to change a[i:ai] into b[j:bj], pump out |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
518 # the matching block, and move (i,j) beyond the match |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
519 tag = '' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
520 if i < ai and j < bj: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
521 tag = 'replace' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
522 elif i < ai: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
523 tag = 'delete' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
524 elif j < bj: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
525 tag = 'insert' |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
526 if tag: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
527 answer.append( (tag, i, ai, j, bj) ) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
528 i, j = ai+size, bj+size |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
529 # the list of matching blocks is terminated by a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
530 # sentinel with size 0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
531 if size: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
532 answer.append( ('equal', ai, i, bj, j) ) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
533 return answer |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
534 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
535 def ratio(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
536 """Return a measure of the sequences' similarity (float in [0,1]). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
537 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
538 Where T is the total number of elements in both sequences, and |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
539 M is the number of matches, this is 2,0*M / T. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
540 Note that this is 1 if the sequences are identical, and 0 if |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
541 they have nothing in common. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
542 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
543 .ratio() is expensive to compute if you haven't already computed |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
544 .get_matching_blocks() or .get_opcodes(), in which case you may |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
545 want to try .quick_ratio() or .real_quick_ratio() first to get an |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
546 upper bound. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
547 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
548 >>> s = SequenceMatcher(None, "abcd", "bcde") |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
549 >>> s.ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
550 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
551 >>> s.quick_ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
552 0.75 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
553 >>> s.real_quick_ratio() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
554 1.0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
555 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
556 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
557 matches = reduce(lambda sum, triple: sum + triple[-1], |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
558 self.get_matching_blocks(), 0) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
559 return 2.0 * matches / (len(self.a) + len(self.b)) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
560 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
561 def quick_ratio(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
562 """Return an upper bound on ratio() relatively quickly. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
563 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
564 This isn't defined beyond that it is an upper bound on .ratio(), and |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
565 is faster to compute. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
566 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
567 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
568 # viewing a and b as multisets, set matches to the cardinality |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
569 # of their intersection; this counts the number of matches |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
570 # without regard to order, so is clearly an upper bound |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
571 if self.fullbcount is None: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
572 self.fullbcount = fullbcount = {} |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
573 for elt in self.b: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
574 fullbcount[elt] = fullbcount.get(elt, 0) + 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
575 fullbcount = self.fullbcount |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
576 # avail[x] is the number of times x appears in 'b' less the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
577 # number of times we've seen it in 'a' so far ... kinda |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
578 avail = {} |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
579 availhas, matches = avail.has_key, 0 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
580 for elt in self.a: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
581 if availhas(elt): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
582 numb = avail[elt] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
583 else: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
584 numb = fullbcount.get(elt, 0) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
585 avail[elt] = numb - 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
586 if numb > 0: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
587 matches = matches + 1 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
588 return 2.0 * matches / (len(self.a) + len(self.b)) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
589 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
590 def real_quick_ratio(self): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
591 """Return an upper bound on ratio() very quickly. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
592 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
593 This isn't defined beyond that it is an upper bound on .ratio(), and |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
594 is faster to compute than either .ratio() or .quick_ratio(). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
595 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
596 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
597 la, lb = len(self.a), len(self.b) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
598 # can't have more matches than the number of elements in the |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
599 # shorter sequence |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
600 return 2.0 * min(la, lb) / (la + lb) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
601 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
602 def get_close_matches(word, possibilities, n=3, cutoff=0.6): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
603 """Use SequenceMatcher to return list of the best "good enough" matches. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
604 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
605 word is a sequence for which close matches are desired (typically a |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
606 string). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
607 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
608 possibilities is a list of sequences against which to match word |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
609 (typically a list of strings). |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
610 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
611 Optional arg n (default 3) is the maximum number of close matches to |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
612 return. n must be > 0. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
613 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
614 Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
615 that don't score at least that similar to word are ignored. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
616 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
617 The best (no more than n) matches among the possibilities are returned |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
618 in a list, sorted by similarity score, most similar first. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
619 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
620 >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"]) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
621 ['apple', 'ape'] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
622 >>> import keyword as _keyword |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
623 >>> get_close_matches("wheel", _keyword.kwlist) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
624 ['while'] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
625 >>> get_close_matches("apple", _keyword.kwlist) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
626 [] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
627 >>> get_close_matches("accept", _keyword.kwlist) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
628 ['except'] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
629 """ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
630 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
631 if not n > 0: |
|
16695
e14c5f34fa05
Fix a few small typos in the docstrings.
Fred Drake <fdrake@acm.org>
parents:
16551
diff
changeset
|
632 raise ValueError("n must be > 0: " + `n`) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
633 if not 0.0 <= cutoff <= 1.0: |
|
16695
e14c5f34fa05
Fix a few small typos in the docstrings.
Fred Drake <fdrake@acm.org>
parents:
16551
diff
changeset
|
634 raise ValueError("cutoff must be in [0.0, 1.0]: " + `cutoff`) |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
635 result = [] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
636 s = SequenceMatcher() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
637 s.set_seq2(word) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
638 for x in possibilities: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
639 s.set_seq1(x) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
640 if s.real_quick_ratio() >= cutoff and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
641 s.quick_ratio() >= cutoff and \ |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
642 s.ratio() >= cutoff: |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
643 result.append((s.ratio(), x)) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
644 # Sort by score. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
645 result.sort() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
646 # Retain only the best n. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
647 result = result[-n:] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
648 # Move best-scorer to head of list. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
649 result.reverse() |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
650 # Strip scores. |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
651 return [x for score, x in result] |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
652 |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
653 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
654 def _count_leading(line, ch): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
655 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
656 Return number of `ch` characters at the start of `line`. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
657 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
658 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
659 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
660 >>> _count_leading(' abc', ' ') |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
661 3 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
662 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
663 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
664 i, n = 0, len(line) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
665 while i < n and line[i] == ch: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
666 i += 1 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
667 return i |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
668 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
669 class Differ: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
670 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
671 Differ is a class for comparing sequences of lines of text, and |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
672 producing human-readable differences or deltas. Differ uses |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
673 SequenceMatcher both to compare sequences of lines, and to compare |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
674 sequences of characters within similar (near-matching) lines. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
675 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
676 Each line of a Differ delta begins with a two-letter code: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
677 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
678 '- ' line unique to sequence 1 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
679 '+ ' line unique to sequence 2 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
680 ' ' line common to both sequences |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
681 '? ' line not present in either input sequence |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
682 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
683 Lines beginning with '? ' attempt to guide the eye to intraline |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
684 differences, and were not present in either input sequence. These lines |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
685 can be confusing if the sequences contain tab characters. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
686 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
687 Note that Differ makes no claim to produce a *minimal* diff. To the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
688 contrary, minimal diffs are often counter-intuitive, because they synch |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
689 up anywhere possible, sometimes accidental matches 100 pages apart. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
690 Restricting synch points to contiguous matches preserves some notion of |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
691 locality, at the occasional cost of producing a longer diff. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
692 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
693 Example: Comparing two texts. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
694 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
695 First we set up the texts, sequences of individual single-line strings |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
696 ending with newlines (such sequences can also be obtained from the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
697 `readlines()` method of file-like objects): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
698 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
699 >>> text1 = ''' 1. Beautiful is better than ugly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
700 ... 2. Explicit is better than implicit. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
701 ... 3. Simple is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
702 ... 4. Complex is better than complicated. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
703 ... '''.splitlines(1) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
704 >>> len(text1) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
705 4 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
706 >>> text1[0][-1] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
707 '\n' |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
708 >>> text2 = ''' 1. Beautiful is better than ugly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
709 ... 3. Simple is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
710 ... 4. Complicated is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
711 ... 5. Flat is better than nested. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
712 ... '''.splitlines(1) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
713 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
714 Next we instantiate a Differ object: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
715 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
716 >>> d = Differ() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
717 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
718 Note that when instantiating a Differ object we may pass functions to |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
719 filter out line and character 'junk'. See Differ.__init__ for details. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
720 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
721 Finally, we compare the two: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
722 |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
723 >>> result = list(d.compare(text1, text2)) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
724 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
725 'result' is a list of strings, so let's pretty-print it: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
726 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
727 >>> from pprint import pprint as _pprint |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
728 >>> _pprint(result) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
729 [' 1. Beautiful is better than ugly.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
730 '- 2. Explicit is better than implicit.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
731 '- 3. Simple is better than complex.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
732 '+ 3. Simple is better than complex.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
733 '? ++\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
734 '- 4. Complex is better than complicated.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
735 '? ^ ---- ^\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
736 '+ 4. Complicated is better than complex.\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
737 '? ++++ ^ ^\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
738 '+ 5. Flat is better than nested.\n'] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
739 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
740 As a single multi-line string it looks like this: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
741 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
742 >>> print ''.join(result), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
743 1. Beautiful is better than ugly. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
744 - 2. Explicit is better than implicit. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
745 - 3. Simple is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
746 + 3. Simple is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
747 ? ++ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
748 - 4. Complex is better than complicated. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
749 ? ^ ---- ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
750 + 4. Complicated is better than complex. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
751 ? ++++ ^ ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
752 + 5. Flat is better than nested. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
753 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
754 Methods: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
755 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
756 __init__(linejunk=None, charjunk=None) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
757 Construct a text differencer, with optional filters. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
758 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
759 compare(a, b) |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
760 Compare two sequences of lines; generate the resulting delta. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
761 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
762 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
763 def __init__(self, linejunk=None, charjunk=None): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
764 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
765 Construct a text differencer, with optional filters. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
766 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
767 The two optional keyword parameters are for filter functions: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
768 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
769 - `linejunk`: A function that should accept a single string argument, |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
770 and return true iff the string is junk. The module-level function |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
771 `IS_LINE_JUNK` may be used to filter out lines without visible |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
772 characters, except for at most one splat ('#'). It is recommended |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
773 to leave linejunk None; as of Python 2.3, the underlying |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
774 SequenceMatcher class has grown an adaptive notion of "noise" lines |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
775 that's better than any static definition the author has ever been |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
776 able to craft. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
777 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
778 - `charjunk`: A function that should accept a string of length 1. The |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
779 module-level function `IS_CHARACTER_JUNK` may be used to filter out |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
780 whitespace characters (a blank or tab; **note**: bad idea to include |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
781 newline in this!). Use of IS_CHARACTER_JUNK is recommended. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
782 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
783 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
784 self.linejunk = linejunk |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
785 self.charjunk = charjunk |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
786 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
787 def compare(self, a, b): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
788 r""" |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
789 Compare two sequences of lines; generate the resulting delta. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
790 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
791 Each sequence must contain individual single-line strings ending with |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
792 newlines. Such sequences can be obtained from the `readlines()` method |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
793 of file-like objects. The delta generated also consists of newline- |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
794 terminated strings, ready to be printed as-is via the writeline() |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
795 method of a file-like object. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
796 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
797 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
798 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
799 >>> print ''.join(Differ().compare('one\ntwo\nthree\n'.splitlines(1), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
800 ... 'ore\ntree\nemu\n'.splitlines(1))), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
801 - one |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
802 ? ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
803 + ore |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
804 ? ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
805 - two |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
806 - three |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
807 ? - |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
808 + tree |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
809 + emu |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
810 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
811 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
812 cruncher = SequenceMatcher(self.linejunk, a, b) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
813 for tag, alo, ahi, blo, bhi in cruncher.get_opcodes(): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
814 if tag == 'replace': |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
815 g = self._fancy_replace(a, alo, ahi, b, blo, bhi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
816 elif tag == 'delete': |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
817 g = self._dump('-', a, alo, ahi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
818 elif tag == 'insert': |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
819 g = self._dump('+', b, blo, bhi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
820 elif tag == 'equal': |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
821 g = self._dump(' ', a, alo, ahi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
822 else: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
823 raise ValueError, 'unknown tag ' + `tag` |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
824 |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
825 for line in g: |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
826 yield line |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
827 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
828 def _dump(self, tag, x, lo, hi): |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
829 """Generate comparison results for a same-tagged range.""" |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
830 for i in xrange(lo, hi): |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
831 yield '%s %s' % (tag, x[i]) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
832 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
833 def _plain_replace(self, a, alo, ahi, b, blo, bhi): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
834 assert alo < ahi and blo < bhi |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
835 # dump the shorter block first -- reduces the burden on short-term |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
836 # memory if the blocks are of very different sizes |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
837 if bhi - blo < ahi - alo: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
838 first = self._dump('+', b, blo, bhi) |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
839 second = self._dump('-', a, alo, ahi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
840 else: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
841 first = self._dump('-', a, alo, ahi) |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
842 second = self._dump('+', b, blo, bhi) |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
843 |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
844 for g in first, second: |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
845 for line in g: |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
846 yield line |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
847 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
848 def _fancy_replace(self, a, alo, ahi, b, blo, bhi): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
849 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
850 When replacing one block of lines with another, search the blocks |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
851 for *similar* lines; the best-matching pair (if any) is used as a |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
852 synch point, and intraline difference marking is done on the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
853 similar pair. Lots of work, but often worth it. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
854 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
855 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
856 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
857 >>> d = Differ() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
858 >>> d._fancy_replace(['abcDefghiJkl\n'], 0, 1, ['abcdefGhijkl\n'], 0, 1) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
859 >>> print ''.join(d.results), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
860 - abcDefghiJkl |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
861 ? ^ ^ ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
862 + abcdefGhijkl |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
863 ? ^ ^ ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
864 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
865 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
866 # don't synch up unless the lines have a similarity score of at |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
867 # least cutoff; best_ratio tracks the best score seen so far |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
868 best_ratio, cutoff = 0.74, 0.75 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
869 cruncher = SequenceMatcher(self.charjunk) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
870 eqi, eqj = None, None # 1st indices of equal lines (if any) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
871 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
872 # search for the pair that matches best without being identical |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
873 # (identical lines must be junk lines, & we don't want to synch up |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
874 # on junk -- unless we have to) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
875 for j in xrange(blo, bhi): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
876 bj = b[j] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
877 cruncher.set_seq2(bj) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
878 for i in xrange(alo, ahi): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
879 ai = a[i] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
880 if ai == bj: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
881 if eqi is None: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
882 eqi, eqj = i, j |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
883 continue |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
884 cruncher.set_seq1(ai) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
885 # computing similarity is expensive, so use the quick |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
886 # upper bounds first -- have seen this speed up messy |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
887 # compares by a factor of 3. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
888 # note that ratio() is only expensive to compute the first |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
889 # time it's called on a sequence pair; the expensive part |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
890 # of the computation is cached by cruncher |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
891 if cruncher.real_quick_ratio() > best_ratio and \ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
892 cruncher.quick_ratio() > best_ratio and \ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
893 cruncher.ratio() > best_ratio: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
894 best_ratio, best_i, best_j = cruncher.ratio(), i, j |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
895 if best_ratio < cutoff: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
896 # no non-identical "pretty close" pair |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
897 if eqi is None: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
898 # no identical pair either -- treat it as a straight replace |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
899 for line in self._plain_replace(a, alo, ahi, b, blo, bhi): |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
900 yield line |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
901 return |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
902 # no close pair, but an identical pair -- synch up on that |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
903 best_i, best_j, best_ratio = eqi, eqj, 1.0 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
904 else: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
905 # there's a close pair, so forget the identical pair (if any) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
906 eqi = None |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
907 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
908 # a[best_i] very similar to b[best_j]; eqi is None iff they're not |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
909 # identical |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
910 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
911 # pump out diffs from before the synch point |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
912 for line in self._fancy_helper(a, alo, best_i, b, blo, best_j): |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
913 yield line |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
914 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
915 # do intraline marking on the synch pair |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
916 aelt, belt = a[best_i], b[best_j] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
917 if eqi is None: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
918 # pump out a '-', '?', '+', '?' quad for the synched lines |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
919 atags = btags = "" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
920 cruncher.set_seqs(aelt, belt) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
921 for tag, ai1, ai2, bj1, bj2 in cruncher.get_opcodes(): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
922 la, lb = ai2 - ai1, bj2 - bj1 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
923 if tag == 'replace': |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
924 atags += '^' * la |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
925 btags += '^' * lb |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
926 elif tag == 'delete': |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
927 atags += '-' * la |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
928 elif tag == 'insert': |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
929 btags += '+' * lb |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
930 elif tag == 'equal': |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
931 atags += ' ' * la |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
932 btags += ' ' * lb |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
933 else: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
934 raise ValueError, 'unknown tag ' + `tag` |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
935 for line in self._qformat(aelt, belt, atags, btags): |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
936 yield line |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
937 else: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
938 # the synch pair is identical |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
939 yield ' ' + aelt |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
940 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
941 # pump out diffs from after the synch point |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
942 for line in self._fancy_helper(a, best_i+1, ahi, b, best_j+1, bhi): |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
943 yield line |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
944 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
945 def _fancy_helper(self, a, alo, ahi, b, blo, bhi): |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
946 g = [] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
947 if alo < ahi: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
948 if blo < bhi: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
949 g = self._fancy_replace(a, alo, ahi, b, blo, bhi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
950 else: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
951 g = self._dump('-', a, alo, ahi) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
952 elif blo < bhi: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
953 g = self._dump('+', b, blo, bhi) |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
954 |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
955 for line in g: |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
956 yield line |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
957 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
958 def _qformat(self, aline, bline, atags, btags): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
959 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
960 Format "?" output and deal with leading tabs. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
961 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
962 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
963 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
964 >>> d = Differ() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
965 >>> d._qformat('\tabcDefghiJkl\n', '\t\tabcdefGhijkl\n', |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
966 ... ' ^ ^ ^ ', '+ ^ ^ ^ ') |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
967 >>> for line in d.results: print repr(line) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
968 ... |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
969 '- \tabcDefghiJkl\n' |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
970 '? \t ^ ^ ^\n' |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
971 '+ \t\tabcdefGhijkl\n' |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
972 '? \t ^ ^ ^\n' |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
973 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
974 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
975 # Can hurt, but will probably help most of the time. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
976 common = min(_count_leading(aline, "\t"), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
977 _count_leading(bline, "\t")) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
978 common = min(common, _count_leading(atags[:common], " ")) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
979 atags = atags[common:].rstrip() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
980 btags = btags[common:].rstrip() |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
981 |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
982 yield "- " + aline |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
983 if atags: |
|
20240
5c4261e74478
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
20038
diff
changeset
|
984 yield "? %s%s\n" % ("\t" * common, atags) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
985 |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
986 yield "+ " + bline |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
987 if btags: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
988 yield "? %s%s\n" % ("\t" * common, btags) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
989 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
990 # With respect to junk, an earlier version of ndiff simply refused to |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
991 # *start* a match with a junk element. The result was cases like this: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
992 # before: private Thread currentThread; |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
993 # after: private volatile Thread currentThread; |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
994 # If you consider whitespace to be junk, the longest contiguous match |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
995 # not starting with junk is "e Thread currentThread". So ndiff reported |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
996 # that "e volatil" was inserted between the 't' and the 'e' in "private". |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
997 # While an accurate view, to people that's absurd. The current version |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
998 # looks for matching blocks that are entirely junk-free, then extends the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
999 # longest one of those as far as possible but only with matching junk. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1000 # So now "currentThread" is matched, then extended to suck up the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1001 # preceding blank; then "private" is matched, and extended to suck up the |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1002 # following blank; then "Thread" is matched; and finally ndiff reports |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1003 # that "volatile " was inserted before "Thread". The only quibble |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1004 # remaining is that perhaps it was really the case that " volatile" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1005 # was inserted after "private". I can live with that <wink>. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1006 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1007 import re |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1008 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1009 def IS_LINE_JUNK(line, pat=re.compile(r"\s*#?\s*$").match): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1010 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1011 Return 1 for ignorable line: iff `line` is blank or contains a single '#'. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1012 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1013 Examples: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1014 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1015 >>> IS_LINE_JUNK('\n') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1016 True |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1017 >>> IS_LINE_JUNK(' # \n') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1018 True |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1019 >>> IS_LINE_JUNK('hello\n') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1020 False |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1021 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1022 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1023 return pat(line) is not None |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1024 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1025 def IS_CHARACTER_JUNK(ch, ws=" \t"): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1026 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1027 Return 1 for ignorable character: iff `ch` is a space or tab. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1028 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1029 Examples: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1030 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1031 >>> IS_CHARACTER_JUNK(' ') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1032 True |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1033 >>> IS_CHARACTER_JUNK('\t') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1034 True |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1035 >>> IS_CHARACTER_JUNK('\n') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1036 False |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1037 >>> IS_CHARACTER_JUNK('x') |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22719
diff
changeset
|
1038 False |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1039 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1040 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1041 return ch in ws |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1042 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1043 del re |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1044 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
1045 def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK): |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1046 r""" |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1047 Compare `a` and `b` (lists of strings); return a `Differ`-style delta. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1048 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1049 Optional keyword parameters `linejunk` and `charjunk` are for filter |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1050 functions (or None): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1051 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1052 - linejunk: A function that should accept a single string argument, and |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
1053 return true iff the string is junk. The default is None, and is |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
1054 recommended; as of Python 2.3, an adaptive notion of "noise" lines is |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
22785
diff
changeset
|
1055 used that does a good job on its own. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1056 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1057 - charjunk: A function that should accept a string of length 1. The |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1058 default is module-level function IS_CHARACTER_JUNK, which filters out |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1059 whitespace characters (a blank or tab; note: bad idea to include newline |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1060 in this!). |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1061 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1062 Tools/scripts/ndiff.py is a command-line front-end to this function. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1063 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1064 Example: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1065 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1066 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1067 ... 'ore\ntree\nemu\n'.splitlines(1)) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1068 >>> print ''.join(diff), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1069 - one |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1070 ? ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1071 + ore |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1072 ? ^ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1073 - two |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1074 - three |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1075 ? - |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1076 + tree |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1077 + emu |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1078 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1079 return Differ(linejunk, charjunk).compare(a, b) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1080 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1081 def restore(delta, which): |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1082 r""" |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1083 Generate one of the two sequences that generated a delta. |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1084 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1085 Given a `delta` produced by `Differ.compare()` or `ndiff()`, extract |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1086 lines originating from file 1 or 2 (parameter `which`), stripping off line |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1087 prefixes. |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1088 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1089 Examples: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1090 |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1091 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1092 ... 'ore\ntree\nemu\n'.splitlines(1)) |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1093 >>> diff = list(diff) |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1094 >>> print ''.join(restore(diff, 1)), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1095 one |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1096 two |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1097 three |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1098 >>> print ''.join(restore(diff, 2)), |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1099 ore |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1100 tree |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1101 emu |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1102 """ |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1103 try: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1104 tag = {1: "- ", 2: "+ "}[int(which)] |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1105 except KeyError: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1106 raise ValueError, ('unknown delta choice (must be 1 or 2): %r' |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1107 % which) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1108 prefixes = (" ", tag) |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1109 for line in delta: |
|
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1110 if line[:2] in prefixes: |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19200
diff
changeset
|
1111 yield line[2:] |
|
19200
50fb4683a461
SF patch #445412 extract ndiff functionality to difflib, from
Tim Peters <tim.peters@gmail.com>
parents:
16703
diff
changeset
|
1112 |
|
16551
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1113 def _test(): |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1114 import doctest, difflib |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1115 return doctest.testmod(difflib) |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1116 |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1117 if __name__ == "__main__": |
|
7de5043827f7
Moved SequenceMatcher from ndiff into new std library module difflib.py.
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1118 _test() |
