Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libdifflib.tex @ 17388:e4abe74bc973 legacy-trunk
Add note that difflib was added in Python 2.1.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Tue, 10 Apr 2001 19:56:09 +0000 |
| parents | d368552c2b72 |
| children | a1575dbf7d81 |
| rev | line source |
|---|---|
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 \section{\module{difflib} --- |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 Helpers for computing deltas} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
3 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 \declaremodule{standard}{difflib} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 \modulesynopsis{Helpers for computing differences between objects.} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 \moduleauthor{Tim Peters}{tim.one@home.com} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 \sectionauthor{Tim Peters}{tim.one@home.com} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
8 % LaTeXification by Fred L. Drake, Jr. <fdrake@acm.org>. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 |
|
17388
e4abe74bc973
Add note that difflib was added in Python 2.1.
Fred Drake <fdrake@acm.org>
parents:
17382
diff
changeset
|
10 \versionadded{2.1} |
|
e4abe74bc973
Add note that difflib was added in Python 2.1.
Fred Drake <fdrake@acm.org>
parents:
17382
diff
changeset
|
11 |
|
e4abe74bc973
Add note that difflib was added in Python 2.1.
Fred Drake <fdrake@acm.org>
parents:
17382
diff
changeset
|
12 |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 \begin{funcdesc}{get_close_matches}{word, possibilities\optional{, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 n\optional{, cutoff}}} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 Return a list of the best ``good enough'' matches. \var{word} is a |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 sequence for which close matches are desired (typically a string), |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 and \var{possibilities} is a list of sequences against which to |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 match \var{word} (typically a list of strings). |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 Optional argument \var{n} (default \code{3}) is the maximum number |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 of close matches to return; \var{n} must be greater than \code{0}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
23 Optional argument \var{cutoff} (default \code{0.6}) is a float in |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 the range [0, 1]. Possibilities that don't score at least that |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
25 similar to \var{word} are ignored. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 The best (no more than \var{n}) matches among the possibilities are |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 returned in a list, sorted by similarity score, most similar first. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 >>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
32 ['apple', 'ape'] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
33 >>> import keyword |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
34 >>> get_close_matches('wheel', keyword.kwlist) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 ['while'] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 >>> get_close_matches('apple', keyword.kwlist) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
37 [] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
38 >>> get_close_matches('accept', keyword.kwlist) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
39 ['except'] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
40 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
41 \end{funcdesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
42 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
43 \begin{classdesc}{SequenceMatcher}{\unspecified} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
44 This is a flexible class for comparing pairs of sequences of any |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
45 type, so long as the sequence elements are hashable. The basic |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
46 algorithm predates, and is a little fancier than, an algorithm |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
47 published in the late 1980's by Ratcliff and Obershelp under the |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
48 hyperbolic name ``gestalt pattern matching.'' The idea is to find |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
49 the longest contiguous matching subsequence that contains no |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
50 ``junk'' elements (the Ratcliff and Obershelp algorithm doesn't |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
51 address junk). The same idea is then applied recursively to the |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
52 pieces of the sequences to the left and to the right of the matching |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
53 subsequence. This does not yield minimal edit sequences, but does |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
54 tend to yield matches that ``look right'' to people. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
56 \strong{Timing:} The basic Ratcliff-Obershelp algorithm is cubic |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 time in the worst case and quadratic time in the expected case. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
58 \class{SequenceMatcher} is quadratic time for the worst case and has |
|
16703
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
59 expected-case behavior dependent in a complicated way on how many |
|
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
60 elements the sequences have in common; best case time is linear. |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
61 \end{classdesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
63 |
|
17382
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
64 \begin{seealso} |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
65 \seetitle{Pattern Matching: The Gestalt Approach}{Discussion of a |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
66 similar algorithm by John W. Ratcliff and D. E. Metzener. |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
67 This was published in |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
68 \citetitle[http://www.ddj.com/]{Dr. Dobb's Journal} in |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
69 July, 1988.} |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
70 \end{seealso} |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
71 |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
72 |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 \subsection{SequenceMatcher Objects \label{sequence-matcher}} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
74 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 \begin{classdesc}{SequenceMatcher}{\optional{isjunk\optional{, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 a\optional{, b}}}} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
77 Optional argument \var{isjunk} must be \code{None} (the default) or |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 a one-argument function that takes a sequence element and returns |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
79 true if and only if the element is ``junk'' and should be ignored. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 \code{None} is equivalent to passing \code{lambda x: 0}, i.e.\ no |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
81 elements are ignored. For example, pass |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 \begin{verbatim} |
|
16758
8996288a8873
Fix extra backslash in example.
Fred Drake <fdrake@acm.org>
parents:
16703
diff
changeset
|
84 lambda x: x in " \t" |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
85 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
86 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
87 if you're comparing lines as sequences of characters, and don't want |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
88 to synch up on blanks or hard tabs. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
89 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
90 The optional arguments \var{a} and \var{b} are sequences to be |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
91 compared; both default to empty strings. The elements of both |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
92 sequences must be hashable. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
93 \end{classdesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
94 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
95 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
96 \class{SequenceMatcher} objects have the following methods: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
97 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
98 \begin{methoddesc}{set_seqs}{a, b} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
99 Set the two sequences to be compared. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
100 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
101 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
102 \class{SequenceMatcher} computes and caches detailed information about |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
103 the second sequence, so if you want to compare one sequence against |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
104 many sequences, use \method{set_seq2()} to set the commonly used |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
105 sequence once and call \method{set_seq1()} repeatedly, once for each |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
106 of the other sequences. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
107 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
108 \begin{methoddesc}{set_seq1}{a} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
109 Set the first sequence to be compared. The second sequence to be |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
110 compared is not changed. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
111 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
112 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
113 \begin{methoddesc}{set_seq2}{b} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
114 Set the second sequence to be compared. The first sequence to be |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
115 compared is not changed. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
116 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
117 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
118 \begin{methoddesc}{find_longest_match}{alo, ahi, blo, bhi} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
119 Find longest matching block in \code{\var{a}[\var{alo}:\var{ahi}]} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
120 and \code{\var{b}[\var{blo}:\var{bhi}]}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
121 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
122 If \var{isjunk} was omitted or \code{None}, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
123 \method{get_longest_match()} returns \code{(\var{i}, \var{j}, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
124 \var{k})} such that \code{\var{a}[\var{i}:\var{i}+\var{k}]} is equal |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
125 to \code{\var{b}[\var{j}:\var{j}+\var{k}]}, where |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
126 \code{\var{alo} <= \var{i} <= \var{i}+\var{k} <= \var{ahi}} and |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
127 \code{\var{blo} <= \var{j} <= \var{j}+\var{k} <= \var{bhi}}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
128 For all \code{(\var{i'}, \var{j'}, \var{k'})} meeting those |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
129 conditions, the additional conditions |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
130 \code{\var{k} >= \var{k'}}, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
131 \code{\var{i} <= \var{i'}}, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
132 and if \code{\var{i} == \var{i'}}, \code{\var{j} <= \var{j'}} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
133 are also met. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
134 In other words, of all maximal matching blocks, return one that |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
135 starts earliest in \var{a}, and of all those maximal matching blocks |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
136 that start earliest in \var{a}, return the one that starts earliest |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
137 in \var{b}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
138 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
139 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
140 >>> s = SequenceMatcher(None, " abcd", "abcd abcd") |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
141 >>> s.find_longest_match(0, 5, 0, 9) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
142 (0, 4, 5) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
143 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
144 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
145 If \var{isjunk} was provided, first the longest matching block is |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
146 determined as above, but with the additional restriction that no |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
147 junk element appears in the block. Then that block is extended as |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
148 far as possible by matching (only) junk elements on both sides. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
149 So the resulting block never matches on junk except as identical |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
150 junk happens to be adjacent to an interesting match. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
151 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
152 Here's the same example as before, but considering blanks to be junk. |
|
16703
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
153 That prevents \code{' abcd'} from matching the \code{' abcd'} at the |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
154 tail end of the second sequence directly. Instead only the |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
155 \code{'abcd'} can match, and matches the leftmost \code{'abcd'} in |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
156 the second sequence: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
157 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
158 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
159 >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
160 >>> s.find_longest_match(0, 5, 0, 9) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
161 (1, 0, 4) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
162 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
163 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
164 If no blocks match, this returns \code{(\var{alo}, \var{blo}, 0)}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
165 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
166 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
167 \begin{methoddesc}{get_matching_blocks}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
168 Return list of triples describing matching subsequences. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 Each triple is of the form \code{(\var{i}, \var{j}, \var{n})}, and |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
170 means that \code{\var{a}[\var{i}:\var{i}+\var{n}] == |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
171 \var{b}[\var{j}:\var{j}+\var{n}]}. The triples are monotonically |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
172 increasing in \var{i} and \var{j}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
173 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
174 The last triple is a dummy, and has the value \code{(len(\var{a}), |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
175 len(\var{b}), 0)}. It is the only triple with \code{\var{n} == 0}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
176 % Explain why a dummy is used! |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
177 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
178 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
179 >>> s = SequenceMatcher(None, "abxcd", "abcd") |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
180 >>> s.get_matching_blocks() |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
181 [(0, 0, 2), (3, 2, 2), (5, 4, 0)] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
182 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
183 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
184 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
185 \begin{methoddesc}{get_opcodes}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
186 Return list of 5-tuples describing how to turn \var{a} into \var{b}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
187 Each tuple is of the form \code{(\var{tag}, \var{i1}, \var{i2}, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
188 \var{j1}, \var{j2})}. The first tuple has \code{\var{i1} == |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
189 \var{j1} == 0}, and remaining tuples have \var{i1} equal to the |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
190 \var{i2} from the preceeding tuple, and, likewise, \var{j1} equal to |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
191 the previous \var{j2}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
192 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
193 The \var{tag} values are strings, with these meanings: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
194 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
195 \begin{tableii}{l|l}{code}{Value}{Meaning} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
196 \lineii{'replace'}{\code{\var{a}[\var{i1}:\var{i2}]} should be |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
197 replaced by \code{\var{b}[\var{j1}:\var{j2}]}.} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
198 \lineii{'delete'}{\code{\var{a}[\var{i1}:\var{i2}]} should be |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
199 deleted. Note that \code{\var{j1} == \var{j2}} in |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
200 this case.} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
201 \lineii{'insert'}{\code{\var{b}[\var{j1}:\var{j2}]} should be |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
202 inserted at \code{\var{a}[\var{i1}:\var{i1}]}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
203 Note that \code{\var{i1} == \var{i2}} in this |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
204 case.} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
205 \lineii{'equal'}{\code{\var{a}[\var{i1}:\var{i2}] == |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
206 \var{b}[\var{j1}:\var{j2}]} (the sub-sequences are |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
207 equal).} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
208 \end{tableii} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
209 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
210 For example: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
211 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
212 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
213 >>> a = "qabxcd" |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
214 >>> b = "abycdf" |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
215 >>> s = SequenceMatcher(None, a, b) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
216 >>> for tag, i1, i2, j1, j2 in s.get_opcodes(): |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
217 ... print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
218 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
219 delete a[0:1] (q) b[0:0] () |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
220 equal a[1:3] (ab) b[0:2] (ab) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
221 replace a[3:4] (x) b[2:3] (y) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
222 equal a[4:6] (cd) b[3:5] (cd) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
223 insert a[6:6] () b[5:6] (f) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
224 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
225 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
226 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
227 \begin{methoddesc}{ratio}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
228 Return a measure of the sequences' similarity as a float in the |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
229 range [0, 1]. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
230 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
231 Where T is the total number of elements in both sequences, and M is |
|
16703
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
232 the number of matches, this is 2.0*M / T. Note that this is \code{1.} |
|
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
233 if the sequences are identical, and \code{0.} if they have nothing in |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
234 common. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
235 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
236 This is expensive to compute if \method{get_matching_blocks()} or |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
237 \method{get_opcodes()} hasn't already been called, in which case you |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
238 may want to try \method{quick_ratio()} or |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
239 \method{real_quick_ratio()} first to get an upper bound. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
240 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
241 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
242 \begin{methoddesc}{quick_ratio}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
243 Return an upper bound on \method{ratio()} relatively quickly. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
244 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
245 This isn't defined beyond that it is an upper bound on |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
246 \method{ratio()}, and is faster to compute. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
247 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
248 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
249 \begin{methoddesc}{real_quick_ratio}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
250 Return an upper bound on \method{ratio()} very quickly. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
251 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
252 This isn't defined beyond that it is an upper bound on |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
253 \method{ratio()}, and is faster to compute than either |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
254 \method{ratio()} or \method{quick_ratio()}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
255 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
256 |
|
16703
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
257 The three methods that return the ratio of matching to total characters |
|
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
258 can give different results due to differing levels of approximation, |
|
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
259 although \method{quick_ratio()} and \method{real_quick_ratio()} are always |
|
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
260 at least as large as \method{ratio()}: |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
261 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
262 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
263 >>> s = SequenceMatcher(None, "abcd", "bcde") |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
264 >>> s.ratio() |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
265 0.75 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
266 >>> s.quick_ratio() |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
267 0.75 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
268 >>> s.real_quick_ratio() |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
269 1.0 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
270 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
271 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
272 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
273 \subsection{Examples \label{difflib-examples}} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
274 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
275 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
276 This example compares two strings, considering blanks to be ``junk:'' |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
277 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
278 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
279 >>> s = SequenceMatcher(lambda x: x == " ", |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
280 ... "private Thread currentThread;", |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
281 ... "private volatile Thread currentThread;") |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
282 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
283 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
284 \method{ratio()} returns a float in [0, 1], measuring the similarity |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
285 of the sequences. As a rule of thumb, a \method{ratio()} value over |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
286 0.6 means the sequences are close matches: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
287 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
288 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
289 >>> print round(s.ratio(), 3) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
290 0.866 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
291 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
292 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
293 If you're only interested in where the sequences match, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
294 \method{get_matching_blocks()} is handy: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
295 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
296 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
297 >>> for block in s.get_matching_blocks(): |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
298 ... print "a[%d] and b[%d] match for %d elements" % block |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
299 a[0] and b[0] match for 8 elements |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
300 a[8] and b[17] match for 6 elements |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
301 a[14] and b[23] match for 15 elements |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
302 a[29] and b[38] match for 0 elements |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
303 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
304 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
305 Note that the last tuple returned by \method{get_matching_blocks()} is |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
306 always a dummy, \code{(len(\var{a}), len(\var{b}), 0)}, and this is |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
307 the only case in which the last tuple element (number of elements |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
308 matched) is \code{0}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
309 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
310 If you want to know how to change the first sequence into the second, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
311 use \method{get_opcodes()}: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
312 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
313 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
314 >>> for opcode in s.get_opcodes(): |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
315 ... print "%6s a[%d:%d] b[%d:%d]" % opcode |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
316 equal a[0:8] b[0:8] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
317 insert a[8:8] b[8:17] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
318 equal a[8:14] b[17:23] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
319 equal a[14:29] b[23:38] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
320 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
321 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
322 See \file{Tools/scripts/ndiff.py} from the Python source distribution |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
323 for a fancy human-friendly file differencer, which uses |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
324 \class{SequenceMatcher} both to view files as sequences of lines, and |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
325 lines as sequences of characters. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
326 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
327 See also the function \function{get_close_matches()} in this module, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
328 which shows how simple code building on \class{SequenceMatcher} can be |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
329 used to do useful work. |
