Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-difflib.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-fpformat.html" />
<link rel="prev" href="module-struct.html" />
<link rel="parent" href="strings.html" />
<link rel="next" href="sequence-matcher.html" />
<meta name='aesop' content='information' />
<title>4.4 difflib -- Helpers for computing deltas</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="4.3 struct "
href="module-struct.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="4. String Services"
href="strings.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="4.4.1 SequenceMatcher Objects"
href="sequence-matcher.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-struct.html">4.3 struct </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="strings.html">4. String Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="sequence-matcher.html">4.4.1 SequenceMatcher Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION006400000000000000000">
4.4 <tt class="module">difflib</tt> --
Helpers for computing deltas</A>
</H1>
<P>
<A NAME="module-difflib"></A>
<P>
<span class="versionnote">New in version 2.1.</span>
<P>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-923' xml:id='l2h-923' class="class">SequenceMatcher</tt></b>
<dd>
This is a flexible class for comparing pairs of sequences of any
type, so long as the sequence elements are hashable. The basic
algorithm predates, and is a little fancier than, an algorithm
published in the late 1980's by Ratcliff and Obershelp under the
hyperbolic name ``gestalt pattern matching.'' The idea is to find
the longest contiguous matching subsequence that contains no
``junk'' elements (the Ratcliff and Obershelp algorithm doesn't
address junk). The same idea is then applied recursively to the
pieces of the sequences to the left and to the right of the matching
subsequence. This does not yield minimal edit sequences, but does
tend to yield matches that ``look right'' to people.
<P>
<strong>Timing:</strong> The basic Ratcliff-Obershelp algorithm is cubic
time in the worst case and quadratic time in the expected case.
<tt class="class">SequenceMatcher</tt> is quadratic time for the worst case and has
expected-case behavior dependent in a complicated way on how many
elements the sequences have in common; best case time is linear.
</dl>
<P>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-924' xml:id='l2h-924' class="class">Differ</tt></b>
<dd>
This is a class for comparing sequences of lines of text, and
producing human-readable differences or deltas. Differ uses
<tt class="class">SequenceMatcher</tt> both to compare sequences of lines, and to
compare sequences of characters within similar (near-matching)
lines.
<P>
Each line of a <tt class="class">Differ</tt> delta begins with a two-letter code:
<P>
<div class="center"><table class="realtable">
<thead>
<tr>
<th class="left" >Code</th>
<th class="left" >Meaning</th>
</tr>
</thead>
<tbody>
<tr><td class="left" valign="baseline"><code>'- '</code></td>
<td class="left" >line unique to sequence 1</td></tr>
<tr><td class="left" valign="baseline"><code>'+ '</code></td>
<td class="left" >line unique to sequence 2</td></tr>
<tr><td class="left" valign="baseline"><code>' '</code></td>
<td class="left" >line common to both sequences</td></tr>
<tr><td class="left" valign="baseline"><code>'? '</code></td>
<td class="left" >line not present in either input sequence</td></tr></tbody>
</table></div>
<P>
Lines beginning with `<code>?&nbsp;</code>' attempt to guide the eye to
intraline differences, and were not present in either input
sequence. These lines can be confusing if the sequences contain tab
characters.
</dl>
<P>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-925' xml:id='l2h-925' class="class">HtmlDiff</tt></b>
<dd>
<P>
This class can be used to create an HTML table (or a complete HTML file
containing the table) showing a side by side, line by line comparison
of text with inter-line and intra-line change highlights. The table can
be generated in either full or contextual difference mode.
<P>
The constructor for this class is:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-926' xml:id='l2h-926' class="function">__init__</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>tabsize</var><big>]</big><var></var><big>[</big><var>,
wrapcolumn</var><big>]</big><var></var><big>[</big><var>, linejunk</var><big>]</big><var></var><big>[</big><var>, charjunk</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
<P>
Initializes instance of <tt class="class">HtmlDiff</tt>.
<P>
<var>tabsize</var> is an optional keyword argument to specify tab stop spacing
and defaults to <code>8</code>.
<P>
<var>wrapcolumn</var> is an optional keyword to specify column number where
lines are broken and wrapped, defaults to <code>None</code> where lines are not
wrapped.
<P>
<var>linejunk</var> and <var>charjunk</var> are optional keyword arguments passed
into <code>ndiff()</code> (used by <tt class="class">HtmlDiff</tt> to generate the
side by side HTML differences). See <code>ndiff()</code> documentation for
argument default values and descriptions.
<P>
</dl>
<P>
The following methods are public:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-927' xml:id='l2h-927' class="function">make_file</tt></b>(</nobr></td>
<td><var>fromlines, tolines
</var><big>[</big><var>, fromdesc</var><big>]</big><var></var><big>[</big><var>, todesc</var><big>]</big><var></var><big>[</big><var>, context</var><big>]</big><var></var><big>[</big><var>,
numlines</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Compares <var>fromlines</var> and <var>tolines</var> (lists of strings) and returns
a string which is a complete HTML file containing a table showing line by
line differences with inter-line and intra-line changes highlighted.
<P>
<var>fromdesc</var> and <var>todesc</var> are optional keyword arguments to specify
from/to file column header strings (both default to an empty string).
<P>
<var>context</var> and <var>numlines</var> are both optional keyword arguments.
Set <var>context</var> to <code>True</code> when contextual differences are to be
shown, else the default is <code>False</code> to show the full files.
<var>numlines</var> defaults to <code>5</code>. When <var>context</var> is <code>True</code>
<var>numlines</var> controls the number of context lines which surround the
difference highlights. When <var>context</var> is <code>False</code> <var>numlines</var>
controls the number of lines which are shown before a difference
highlight when using the "next" hyperlinks (setting to zero would cause
the "next" hyperlinks to place the next difference highlight at the top of
the browser without any leading context).
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-928' xml:id='l2h-928' class="function">make_table</tt></b>(</nobr></td>
<td><var>fromlines, tolines
</var><big>[</big><var>, fromdesc</var><big>]</big><var></var><big>[</big><var>, todesc</var><big>]</big><var></var><big>[</big><var>, context</var><big>]</big><var></var><big>[</big><var>,
numlines</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Compares <var>fromlines</var> and <var>tolines</var> (lists of strings) and returns
a string which is a complete HTML table showing line by line differences
with inter-line and intra-line changes highlighted.
<P>
The arguments for this method are the same as those for the
<tt class="method">make_file()</tt> method.
</dl>
<P>
<span class="file">Tools/scripts/diff.py</span> is a command-line front-end to this class
and contains a good example of its use.
<P>
<span class="versionnote">New in version 2.4.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-929' xml:id='l2h-929' class="function">context_diff</tt></b>(</nobr></td>
<td><var>a, b</var><big>[</big><var>, fromfile</var><big>]</big><var></var><big>[</big><var>,
tofile</var><big>]</big><var></var><big>[</big><var>, fromfiledate</var><big>]</big><var></var><big>[</big><var>, tofiledate</var><big>]</big><var></var><big>[</big><var>,
n</var><big>]</big><var></var><big>[</big><var>, lineterm</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Compare <var>a</var> and <var>b</var> (lists of strings); return a
delta (a generator generating the delta lines) in context diff
format.
<P>
Context diffs are a compact way of showing just the lines that have
changed plus a few lines of context. The changes are shown in a
before/after style. The number of context lines is set by <var>n</var>
which defaults to three.
<P>
By default, the diff control lines (those with <code>***</code> or <code>-&#45;-</code>)
are created with a trailing newline. This is helpful so that inputs created
from <tt class="function">file.readlines()</tt> result in diffs that are suitable for use
with <tt class="function">file.writelines()</tt> since both the inputs and outputs have
trailing newlines.
<P>
For inputs that do not have trailing newlines, set the <var>lineterm</var>
argument to <code>""</code> so that the output will be uniformly newline free.
<P>
The context diff format normally has a header for filenames and
modification times. Any or all of these may be specified using strings for
<var>fromfile</var>, <var>tofile</var>, <var>fromfiledate</var>, and <var>tofiledate</var>.
The modification times are normally expressed in the format returned by
<tt class="function">time.ctime()</tt>. If not specified, the strings default to blanks.
<P>
<span class="file">Tools/scripts/diff.py</span> is a command-line front-end for this
function.
<P>
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-930' xml:id='l2h-930' class="function">get_close_matches</tt></b>(</nobr></td>
<td><var>word, possibilities</var><big>[</big><var>,
n</var><big>]</big><var></var><big>[</big><var>, cutoff</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Return a list of the best ``good enough'' matches. <var>word</var> is a
sequence for which close matches are desired (typically a string),
and <var>possibilities</var> is a list of sequences against which to
match <var>word</var> (typically a list of strings).
<P>
Optional argument <var>n</var> (default <code>3</code>) is the maximum number
of close matches to return; <var>n</var> must be greater than <code>0</code>.
<P>
Optional argument <var>cutoff</var> (default <code>0.6</code>) is a float in
the range [0, 1]. Possibilities that don't score at least that
similar to <var>word</var> are ignored.
<P>
The best (no more than <var>n</var>) matches among the possibilities are
returned in a list, sorted by similarity score, most similar first.
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
&gt;&gt;&gt; import keyword
&gt;&gt;&gt; get_close_matches('wheel', keyword.kwlist)
['while']
&gt;&gt;&gt; get_close_matches('apple', keyword.kwlist)
[]
&gt;&gt;&gt; get_close_matches('accept', keyword.kwlist)
['except']
</pre></div>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-931' xml:id='l2h-931' class="function">ndiff</tt></b>(</nobr></td>
<td><var>a, b</var><big>[</big><var>, linejunk</var><big>]</big><var></var><big>[</big><var>, charjunk</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Compare <var>a</var> and <var>b</var> (lists of strings); return a
<tt class="class">Differ</tt>-style delta (a generator generating the delta lines).
<P>
Optional keyword parameters <var>linejunk</var> and <var>charjunk</var> are
for filter functions (or <code>None</code>):
<P>
<var>linejunk</var>: A function that accepts a single string
argument, and returns true if the string is junk, or false if not.
The default is (<code>None</code>), starting with Python 2.3. Before then,
the default was the module-level function
<tt class="function">IS_LINE_JUNK()</tt>, which filters out lines without visible
characters, except for at most one pound character ("<tt class="character">#</tt>").
As of Python 2.3, the underlying <tt class="class">SequenceMatcher</tt> class
does a dynamic analysis of which lines are so frequent as to
constitute noise, and this usually works better than the pre-2.3
default.
<P>
<var>charjunk</var>: A function that accepts a character (a string of
length 1), and returns if the character is junk, or false if not.
The default is module-level function <tt class="function">IS_CHARACTER_JUNK()</tt>,
which filters out whitespace characters (a blank or tab; note: bad
idea to include newline in this!).
<P>
<span class="file">Tools/scripts/ndiff.py</span> is a command-line front-end to this
function.
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
... 'ore\ntree\nemu\n'.splitlines(1))
&gt;&gt;&gt; print ''.join(diff),
- one
? ^
+ ore
? ^
- two
- three
? -
+ tree
+ emu
</pre></div>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-932' xml:id='l2h-932' class="function">restore</tt></b>(</nobr></td>
<td><var>sequence, which</var>)</td></tr></table></dt>
<dd>
Return one of the two sequences that generated a delta.
<P>
Given a <var>sequence</var> produced by <tt class="method">Differ.compare()</tt> or
<tt class="function">ndiff()</tt>, extract lines originating from file 1 or 2
(parameter <var>which</var>), stripping off line prefixes.
<P>
Example:
<P>
<div class="verbatim"><pre>
&gt;&gt;&gt; diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
... 'ore\ntree\nemu\n'.splitlines(1))
&gt;&gt;&gt; diff = list(diff) # materialize the generated delta into a list
&gt;&gt;&gt; print ''.join(restore(diff, 1)),
one
two
three
&gt;&gt;&gt; print ''.join(restore(diff, 2)),
ore
tree
emu
</pre></div>
<P>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-933' xml:id='l2h-933' class="function">unified_diff</tt></b>(</nobr></td>
<td><var>a, b</var><big>[</big><var>, fromfile</var><big>]</big><var></var><big>[</big><var>,
tofile</var><big>]</big><var></var><big>[</big><var>, fromfiledate</var><big>]</big><var></var><big>[</big><var>, tofiledate</var><big>]</big><var></var><big>[</big><var>,
n</var><big>]</big><var></var><big>[</big><var>, lineterm</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Compare <var>a</var> and <var>b</var> (lists of strings); return a
delta (a generator generating the delta lines) in unified diff
format.
<P>
Unified diffs are a compact way of showing just the lines that have
changed plus a few lines of context. The changes are shown in a
inline style (instead of separate before/after blocks). The number
of context lines is set by <var>n</var> which defaults to three.
<P>
By default, the diff control lines (those with <code>-&#45;-</code>, <code>+++</code>,
or <code>@@</code>) are created with a trailing newline. This is helpful so
that inputs created from <tt class="function">file.readlines()</tt> result in diffs
that are suitable for use with <tt class="function">file.writelines()</tt> since both
the inputs and outputs have trailing newlines.
<P>
For inputs that do not have trailing newlines, set the <var>lineterm</var>
argument to <code>""</code> so that the output will be uniformly newline free.
<P>
The context diff format normally has a header for filenames and
modification times. Any or all of these may be specified using strings for
<var>fromfile</var>, <var>tofile</var>, <var>fromfiledate</var>, and <var>tofiledate</var>.
The modification times are normally expressed in the format returned by
<tt class="function">time.ctime()</tt>. If not specified, the strings default to blanks.
<P>
<span class="file">Tools/scripts/diff.py</span> is a command-line front-end for this
function.
<P>
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-934' xml:id='l2h-934' class="function">IS_LINE_JUNK</tt></b>(</nobr></td>
<td><var>line</var>)</td></tr></table></dt>
<dd>
Return true for ignorable lines. The line <var>line</var> is ignorable
if <var>line</var> is blank or contains a single "<tt class="character">#</tt>",
otherwise it is not ignorable. Used as a default for parameter
<var>linejunk</var> in <tt class="function">ndiff()</tt> before Python 2.3.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-935' xml:id='l2h-935' class="function">IS_CHARACTER_JUNK</tt></b>(</nobr></td>
<td><var>ch</var>)</td></tr></table></dt>
<dd>
Return true for ignorable characters. The character <var>ch</var> is
ignorable if <var>ch</var> is a space or tab, otherwise it is not
ignorable. Used as a default for parameter <var>charjunk</var> in
<tt class="function">ndiff()</tt>.
</dl>
<P>
<div class="seealso">
<p class="heading">See Also:</p>
<dl compact="compact" class="seetitle">
<dt><em class="citetitle"><a href="http://www.ddj.com/documents/s=1103/ddj8807c/"
>Pattern Matching: The Gestalt Approach</a></em></dt>
<dd>Discussion of a
similar algorithm by John W. Ratcliff and D. E. Metzener.
This was published in
<em class="citetitle"><a
href="http://www.ddj.com/"
title="Dr. Dobb's Journal"
>Dr. Dobb's Journal</a></em> in
July, 1988.</dd>
</dl>
</div>
<P>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="sequence-matcher.html">4.4.1 SequenceMatcher Objects</a>
<LI><A href="sequencematcher-examples.html">4.4.2 SequenceMatcher Examples</a>
<LI><A href="differ-objects.html">4.4.3 Differ Objects</a>
<LI><A href="differ-examples.html">4.4.4 Differ Example</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="4.3 struct "
href="module-struct.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="4. String Services"
href="strings.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="4.4.1 SequenceMatcher Objects"
href="sequence-matcher.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-struct.html">4.3 struct </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="strings.html">4. String Services</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="sequence-matcher.html">4.4.1 SequenceMatcher Objects</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>