Mercurial > cpython-lru_cache-weakref
annotate Doc/lib/libdifflib.tex @ 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 | b5f6e9f6e7f4 |
| children | 242ad5c592cc |
| 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 |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
13 \begin{classdesc*}{SequenceMatcher} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
14 This is a flexible class for comparing pairs of sequences of any |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
15 type, so long as the sequence elements are hashable. The basic |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
16 algorithm predates, and is a little fancier than, an algorithm |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
17 published in the late 1980's by Ratcliff and Obershelp under the |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
18 hyperbolic name ``gestalt pattern matching.'' The idea is to find |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
19 the longest contiguous matching subsequence that contains no |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
20 ``junk'' elements (the Ratcliff and Obershelp algorithm doesn't |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
21 address junk). The same idea is then applied recursively to the |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
22 pieces of the sequences to the left and to the right of the matching |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
23 subsequence. This does not yield minimal edit sequences, but does |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
24 tend to yield matches that ``look right'' to people. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
25 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
26 \strong{Timing:} The basic Ratcliff-Obershelp algorithm is cubic |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
27 time in the worst case and quadratic time in the expected case. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
28 \class{SequenceMatcher} is quadratic time for the worst case and has |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
29 expected-case behavior dependent in a complicated way on how many |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
30 elements the sequences have in common; best case time is linear. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
31 \end{classdesc*} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
32 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
33 \begin{classdesc*}{Differ} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
34 This is a class for comparing sequences of lines of text, and |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
35 producing human-readable differences or deltas. Differ uses |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
36 \class{SequenceMatcher} both to compare sequences of lines, and to |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
37 compare sequences of characters within similar (near-matching) |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
38 lines. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
39 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
40 Each line of a \class{Differ} delta begins with a two-letter code: |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
41 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
42 \begin{tableii}{l|l}{code}{Code}{Meaning} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
43 \lineii{'- '}{line unique to sequence 1} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
44 \lineii{'+ '}{line unique to sequence 2} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
45 \lineii{' '}{line common to both sequences} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
46 \lineii{'? '}{line not present in either input sequence} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
47 \end{tableii} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
48 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
49 Lines beginning with `\code{?~}' attempt to guide the eye to |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
50 intraline differences, and were not present in either input |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
51 sequence. These lines can be confusing if the sequences contain tab |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
52 characters. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
53 \end{classdesc*} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
54 |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
55 \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
|
56 n\optional{, cutoff}}} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 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
|
61 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
62 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
|
63 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
|
64 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
65 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
|
66 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
|
67 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
|
68 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
69 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
|
70 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
|
71 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
72 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
73 >>> 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
|
74 ['apple', 'ape'] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
75 >>> import keyword |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
76 >>> 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
|
77 ['while'] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
78 >>> 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
|
79 [] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
80 >>> 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
|
81 ['except'] |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
82 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
83 \end{funcdesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
84 |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
85 \begin{funcdesc}{ndiff}{a, b\optional{, linejunk\optional{, |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
86 charjunk}}} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
87 Compare \var{a} and \var{b} (lists of strings); return a |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
88 \class{Differ}-style delta (a generator generating the delta lines). |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
89 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
90 Optional keyword parameters \var{linejunk} and \var{charjunk} are |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
91 for filter functions (or \code{None}): |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
92 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
93 \var{linejunk}: A function that accepts a single string |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
94 argument, and returns true if the string is junk, or false if not. |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
95 The default is (\code{None}), starting with Python 2.3. Before then, |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
96 the default was the module-level function |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
97 \function{IS_LINE_JUNK()}, which filters out lines without visible |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
98 characters, except for at most one pound character (\character{\#}). |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
99 As of Python 2.3, the underlying \class{SequenceMatcher} class |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
100 does a dynamic analysis of which lines are so frequent as to |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
101 constitute noise, and this usually works better than the pre-2.3 |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
102 default. |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
103 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
104 \var{charjunk}: A function that accepts a character (a string of |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
105 length 1), and returns if the character is junk, or false if not. |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
106 The default is module-level function \function{IS_CHARACTER_JUNK()}, |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
107 which filters out whitespace characters (a blank or tab; note: bad |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
108 idea to include newline in this!). |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
109 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
110 \file{Tools/scripts/ndiff.py} is a command-line front-end to this |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
111 function. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
112 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
113 \begin{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
114 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
115 ... 'ore\ntree\nemu\n'.splitlines(1))) |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
116 >>> print ''.join(diff), |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
117 - one |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
118 ? ^ |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
119 + ore |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
120 ? ^ |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
121 - two |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
122 - three |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
123 ? - |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
124 + tree |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
125 + emu |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
126 \end{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
127 \end{funcdesc} |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
128 |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
129 \begin{funcdesc}{restore}{sequence, which} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
130 Return one of the two sequences that generated a delta. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
131 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
132 Given a \var{sequence} produced by \method{Differ.compare()} or |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
133 \function{ndiff()}, extract lines originating from file 1 or 2 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
134 (parameter \var{which}), stripping off line prefixes. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
135 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
136 Example: |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
137 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
138 \begin{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
139 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
140 ... 'ore\ntree\nemu\n'.splitlines(1)) |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
141 >>> diff = list(diff) # materialize the generated delta into a list |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
142 >>> print ''.join(restore(diff, 1)), |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
143 one |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
144 two |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
145 three |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
146 >>> print ''.join(restore(diff, 2)), |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
147 ore |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
148 tree |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
149 emu |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
150 \end{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
151 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
152 \end{funcdesc} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
153 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
154 |
|
20677
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
155 \begin{funcdesc}{IS_LINE_JUNK}{line} |
|
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
156 Return true for ignorable lines. The line \var{line} is ignorable |
|
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
157 if \var{line} is blank or contains a single \character{\#}, |
|
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
158 otherwise it is not ignorable. Used as a default for parameter |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
159 \var{linejunk} in \function{ndiff()} before Python 2.3. |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
160 \end{funcdesc} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
161 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
162 |
|
20677
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
163 \begin{funcdesc}{IS_CHARACTER_JUNK}{ch} |
|
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
164 Return true for ignorable characters. The character \var{ch} is |
|
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
165 ignorable if \var{ch} is a space or tab, otherwise it is not |
|
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
166 ignorable. Used as a default for parameter \var{charjunk} in |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
167 \function{ndiff()}. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
168 \end{funcdesc} |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
169 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
170 |
|
17382
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
171 \begin{seealso} |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
172 \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
|
173 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
|
174 This was published in |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
175 \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
|
176 July, 1988.} |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
177 \end{seealso} |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
178 |
|
d368552c2b72
Add reference to the DDJ article discussing a similar algorithm.
Fred Drake <fdrake@acm.org>
parents:
16758
diff
changeset
|
179 |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
180 \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
|
181 |
|
17780
a1575dbf7d81
Replace "\begin{classdesc}{SomeClass}{\unspecified}" with
Fred Drake <fdrake@acm.org>
parents:
17388
diff
changeset
|
182 The \class{SequenceMatcher} class has this constructor: |
|
a1575dbf7d81
Replace "\begin{classdesc}{SomeClass}{\unspecified}" with
Fred Drake <fdrake@acm.org>
parents:
17388
diff
changeset
|
183 |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
184 \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
|
185 a\optional{, b}}}} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
186 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
|
187 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
|
188 true if and only if the element is ``junk'' and should be ignored. |
|
20677
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
189 Passing \code{None} for \var{b} is equivalent to passing |
|
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
190 \code{lambda x: 0}; in other words, no elements are ignored. For |
|
f614825d326a
Enforce a bit of markup consistency.
Fred Drake <fdrake@acm.org>
parents:
20038
diff
changeset
|
191 example, pass: |
|
16682
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 \begin{verbatim} |
|
16758
8996288a8873
Fix extra backslash in example.
Fred Drake <fdrake@acm.org>
parents:
16703
diff
changeset
|
194 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
|
195 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
196 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
197 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
|
198 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
|
199 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
200 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
|
201 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
|
202 sequences must be hashable. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
203 \end{classdesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
204 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
205 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
206 \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
|
207 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
208 \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
|
209 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
|
210 \end{methoddesc} |
|
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 \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
|
213 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
|
214 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
|
215 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
|
216 of the other sequences. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
217 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
218 \begin{methoddesc}{set_seq1}{a} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
219 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
|
220 compared is not changed. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
221 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
222 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
223 \begin{methoddesc}{set_seq2}{b} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
224 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
|
225 compared is not changed. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
226 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
227 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
228 \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
|
229 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
|
230 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
|
231 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
232 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
|
233 \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
|
234 \var{k})} such that \code{\var{a}[\var{i}:\var{i}+\var{k}]} is equal |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
235 to \code{\var{b}[\var{j}:\var{j}+\var{k}]}, where |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
236 \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
|
237 \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
|
238 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
|
239 conditions, the additional conditions |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
240 \code{\var{k} >= \var{k'}}, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
241 \code{\var{i} <= \var{i'}}, |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
242 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
|
243 are also met. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
244 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
|
245 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
|
246 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
|
247 in \var{b}. |
|
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{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
250 >>> 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
|
251 >>> 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
|
252 (0, 4, 5) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
253 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
254 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 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
|
259 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
|
260 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
|
261 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 \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
|
266 the second sequence: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
267 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
268 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
269 >>> 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
|
270 >>> 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
|
271 (1, 0, 4) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
272 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
273 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
274 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
|
275 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
276 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
277 \begin{methoddesc}{get_matching_blocks}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
278 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
|
279 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
|
280 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
|
281 \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
|
282 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
|
283 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
284 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
|
285 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
|
286 % 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
|
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 >>> s = SequenceMatcher(None, "abxcd", "abcd") |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
290 >>> s.get_matching_blocks() |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
291 [(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
|
292 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
293 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
294 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
295 \begin{methoddesc}{get_opcodes}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
296 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
|
297 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
|
298 \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
|
299 \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
|
300 \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
|
301 the previous \var{j2}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
302 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
303 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
|
304 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
305 \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
|
306 \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
|
307 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
|
308 \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
|
309 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
|
310 this case.} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
311 \lineii{'insert'}{\code{\var{b}[\var{j1}:\var{j2}]} should be |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
312 inserted at \code{\var{a}[\var{i1}:\var{i1}]}. |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
313 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
|
314 case.} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
315 \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
|
316 \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
|
317 equal).} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
318 \end{tableii} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
319 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
320 For example: |
|
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 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
323 >>> a = "qabxcd" |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
324 >>> b = "abycdf" |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
325 >>> s = SequenceMatcher(None, a, b) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
326 >>> 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
|
327 ... 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
|
328 ... (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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
335 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
336 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
337 \begin{methoddesc}{ratio}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
338 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
|
339 range [0, 1]. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
340 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
341 Where T is the total number of elements in both sequences, and M is |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
342 the number of matches, this is 2.0*M / T. Note that this is |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
343 \code{1.0} if the sequences are identical, and \code{0.0} if they |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
344 have nothing in common. |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
345 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
346 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
|
347 \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
|
348 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
|
349 \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
|
350 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
351 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
352 \begin{methoddesc}{quick_ratio}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
353 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
|
354 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
355 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
|
356 \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
|
357 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
358 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
359 \begin{methoddesc}{real_quick_ratio}{} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
360 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
|
361 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
362 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
|
363 \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
|
364 \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
|
365 \end{methoddesc} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
366 |
|
16703
8172906c6dc8
Improve accuracy. In the .tex file, note the new "% BUG:" comments: an
Tim Peters <tim.peters@gmail.com>
parents:
16682
diff
changeset
|
367 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
|
368 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
|
369 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
|
370 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
|
371 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
372 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
373 >>> s = SequenceMatcher(None, "abcd", "bcde") |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
374 >>> s.ratio() |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
375 0.75 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
376 >>> s.quick_ratio() |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
377 0.75 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
378 >>> s.real_quick_ratio() |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
379 1.0 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
380 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
381 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
382 |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
383 \subsection{SequenceMatcher Examples \label{sequencematcher-examples}} |
|
16682
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
384 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
385 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
386 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
|
387 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
388 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
389 >>> s = SequenceMatcher(lambda x: x == " ", |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
390 ... "private Thread currentThread;", |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
391 ... "private volatile Thread currentThread;") |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
392 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
393 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
394 \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
|
395 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
|
396 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
|
397 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
398 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
399 >>> print round(s.ratio(), 3) |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
400 0.866 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
401 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
402 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
403 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
|
404 \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
|
405 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
406 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
407 >>> 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
|
408 ... 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
|
409 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
|
410 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
|
411 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
|
412 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
|
413 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
414 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
415 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
|
416 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
|
417 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
|
418 matched) is \code{0}. |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
419 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
420 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
|
421 use \method{get_opcodes()}: |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
422 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
423 \begin{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
424 >>> 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
|
425 ... 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
|
426 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
|
427 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
|
428 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
|
429 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
|
430 \end{verbatim} |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
431 |
|
3cb00cbe7455
Documentation for the difflib module, converted from the module docstrings.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
432 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
|
433 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
|
434 used to do useful work. |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
435 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
436 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
437 \subsection{Differ Objects \label{differ-objects}} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
438 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
439 Note that \class{Differ}-generated deltas make no claim to be |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
440 \strong{minimal} diffs. To the contrary, minimal diffs are often |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
441 counter-intuitive, because they synch up anywhere possible, sometimes |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
442 accidental matches 100 pages apart. Restricting synch points to |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
443 contiguous matches preserves some notion of locality, at the |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
444 occasional cost of producing a longer diff. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
445 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
446 The \class{Differ} class has this constructor: |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
447 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
448 \begin{classdesc}{Differ}{\optional{linejunk\optional{, charjunk}}} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
449 Optional keyword parameters \var{linejunk} and \var{charjunk} are |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
450 for filter functions (or \code{None}): |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
451 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
452 \var{linejunk}: A function that accepts a single string |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
453 argument, and returns true if the string is junk. The default is |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
454 \code{None}, meaning that no line is considered junk. |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
455 |
|
23300
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
456 \var{charjunk}: A function that accepts a single character argument |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
457 (a string of length 1), and returns true if the character is junk. |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
458 The default is \code{None}, meaning that no character is |
|
b5f6e9f6e7f4
Mostly in SequenceMatcher.{__chain_b, find_longest_match}:
Tim Peters <tim.peters@gmail.com>
parents:
21106
diff
changeset
|
459 considered junk. |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
460 \end{classdesc} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
461 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
462 \class{Differ} objects are used (deltas generated) via a single |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
463 method: |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
464 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
465 \begin{methoddesc}{compare}{a, b} |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
466 Compare two sequences of lines, and generate the delta (a sequence |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
467 of lines). |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
468 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
469 Each sequence must contain individual single-line strings ending |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
470 with newlines. Such sequences can be obtained from the |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
471 \method{readlines()} method of file-like objects. The delta generated |
|
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
472 also consists of newline-terminated strings, ready to be printed as-is |
| 21106 | 473 via the \method{writelines()} method of a file-like object. |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
474 \end{methoddesc} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
475 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
476 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
477 \subsection{Differ Example \label{differ-examples}} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
478 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
479 This example compares two texts. First we set up the texts, sequences |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
480 of individual single-line strings ending with newlines (such sequences |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
481 can also be obtained from the \method{readlines()} method of file-like |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
482 objects): |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
483 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
484 \begin{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
485 >>> text1 = ''' 1. Beautiful is better than ugly. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
486 ... 2. Explicit is better than implicit. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
487 ... 3. Simple is better than complex. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
488 ... 4. Complex is better than complicated. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
489 ... '''.splitlines(1) |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
490 >>> len(text1) |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
491 4 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
492 >>> text1[0][-1] |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
493 '\n' |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
494 >>> text2 = ''' 1. Beautiful is better than ugly. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
495 ... 3. Simple is better than complex. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
496 ... 4. Complicated is better than complex. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
497 ... 5. Flat is better than nested. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
498 ... '''.splitlines(1) |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
499 \end{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
500 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
501 Next we instantiate a Differ object: |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
502 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
503 \begin{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
504 >>> d = Differ() |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
505 \end{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
506 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
507 Note that when instantiating a \class{Differ} object we may pass |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
508 functions to filter out line and character ``junk.'' See the |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
509 \method{Differ()} constructor for details. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
510 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
511 Finally, we compare the two: |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
512 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
513 \begin{verbatim} |
|
20038
8a6f2ee6eaba
Make difflib.ndiff() and difflib.Differ.compare() generators. This
Tim Peters <tim.peters@gmail.com>
parents:
19232
diff
changeset
|
514 >>> result = list(d.compare(text1, text2)) |
|
19232
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
515 \end{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
516 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
517 \code{result} is a list of strings, so let's pretty-print it: |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
518 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
519 \begin{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
520 >>> from pprint import pprint |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
521 >>> pprint(result) |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
522 [' 1. Beautiful is better than ugly.\n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
523 '- 2. Explicit is better than implicit.\n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
524 '- 3. Simple is better than complex.\n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
525 '+ 3. Simple is better than complex.\n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
526 '? ++ \n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
527 '- 4. Complex is better than complicated.\n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
528 '? ^ ---- ^ \n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
529 '+ 4. Complicated is better than complex.\n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
530 '? ++++ ^ ^ \n', |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
531 '+ 5. Flat is better than nested.\n'] |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
532 \end{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
533 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
534 As a single multi-line string it looks like this: |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
535 |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
536 \begin{verbatim} |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
537 >>> import sys |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
538 >>> sys.stdout.writelines(result) |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
539 1. Beautiful is better than ugly. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
540 - 2. Explicit is better than implicit. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
541 - 3. Simple is better than complex. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
542 + 3. Simple is better than complex. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
543 ? ++ |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
544 - 4. Complex is better than complicated. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
545 ? ^ ---- ^ |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
546 + 4. Complicated is better than complex. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
547 ? ++++ ^ ^ |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
548 + 5. Flat is better than nested. |
|
65885ea76c37
David Goodger <dgoodger@atsautomation.com>:
Fred Drake <fdrake@acm.org>
parents:
17785
diff
changeset
|
549 \end{verbatim} |
