Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / differ-examples.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="prev" href="differ-objects.html" />
13<link rel="parent" href="module-difflib.html" />
14<link rel="next" href="module-fpformat.html" />
15<meta name='aesop' content='information' />
16<title>4.4.4 Differ Example </title>
17</head>
18<body>
19<DIV CLASS="navigation">
20<div id='top-navigation-panel' xml:id='top-navigation-panel'>
21<table align="center" width="100%" cellpadding="0" cellspacing="2">
22<tr>
23<td class='online-navigation'><a rel="prev" title="4.4.3 Differ Objects"
24 href="differ-objects.html"><img src='../icons/previous.png'
25 border='0' height='32' alt='Previous Page' width='32' /></A></td>
26<td class='online-navigation'><a rel="parent" title="4.4 difflib "
27 href="module-difflib.html"><img src='../icons/up.png'
28 border='0' height='32' alt='Up One Level' width='32' /></A></td>
29<td class='online-navigation'><a rel="next" title="4.5 fpformat "
30 href="module-fpformat.html"><img src='../icons/next.png'
31 border='0' height='32' alt='Next Page' width='32' /></A></td>
32<td align="center" width="100%">Python Library Reference</td>
33<td class='online-navigation'><a rel="contents" title="Table of Contents"
34 href="contents.html"><img src='../icons/contents.png'
35 border='0' height='32' alt='Contents' width='32' /></A></td>
36<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
37 border='0' height='32' alt='Module Index' width='32' /></a></td>
38<td class='online-navigation'><a rel="index" title="Index"
39 href="genindex.html"><img src='../icons/index.png'
40 border='0' height='32' alt='Index' width='32' /></A></td>
41</tr></table>
42<div class='online-navigation'>
43<b class="navlabel">Previous:</b>
44<a class="sectref" rel="prev" href="differ-objects.html">4.4.3 Differ Objects</A>
45<b class="navlabel">Up:</b>
46<a class="sectref" rel="parent" href="module-difflib.html">4.4 difflib </A>
47<b class="navlabel">Next:</b>
48<a class="sectref" rel="next" href="module-fpformat.html">4.5 fpformat </A>
49</div>
50<hr /></div>
51</DIV>
52<!--End of Navigation Panel-->
53
54<H2><A NAME="SECTION006440000000000000000"></A><A NAME="differ-examples"></A>
55<BR>
564.4.4 Differ Example
57</H2>
58
59<P>
60This example compares two texts. First we set up the texts, sequences
61of individual single-line strings ending with newlines (such sequences
62can also be obtained from the <tt class="method">readlines()</tt> method of file-like
63objects):
64
65<P>
66<div class="verbatim"><pre>
67&gt;&gt;&gt; text1 = ''' 1. Beautiful is better than ugly.
68... 2. Explicit is better than implicit.
69... 3. Simple is better than complex.
70... 4. Complex is better than complicated.
71... '''.splitlines(1)
72&gt;&gt;&gt; len(text1)
734
74&gt;&gt;&gt; text1[0][-1]
75'\n'
76&gt;&gt;&gt; text2 = ''' 1. Beautiful is better than ugly.
77... 3. Simple is better than complex.
78... 4. Complicated is better than complex.
79... 5. Flat is better than nested.
80... '''.splitlines(1)
81</pre></div>
82
83<P>
84Next we instantiate a Differ object:
85
86<P>
87<div class="verbatim"><pre>
88&gt;&gt;&gt; d = Differ()
89</pre></div>
90
91<P>
92Note that when instantiating a <tt class="class">Differ</tt> object we may pass
93functions to filter out line and character ``junk.'' See the
94<tt class="method">Differ()</tt> constructor for details.
95
96<P>
97Finally, we compare the two:
98
99<P>
100<div class="verbatim"><pre>
101&gt;&gt;&gt; result = list(d.compare(text1, text2))
102</pre></div>
103
104<P>
105<code>result</code> is a list of strings, so let's pretty-print it:
106
107<P>
108<div class="verbatim"><pre>
109&gt;&gt;&gt; from pprint import pprint
110&gt;&gt;&gt; pprint(result)
111[' 1. Beautiful is better than ugly.\n',
112 '- 2. Explicit is better than implicit.\n',
113 '- 3. Simple is better than complex.\n',
114 '+ 3. Simple is better than complex.\n',
115 '? ++ \n',
116 '- 4. Complex is better than complicated.\n',
117 '? ^ ---- ^ \n',
118 '+ 4. Complicated is better than complex.\n',
119 '? ++++ ^ ^ \n',
120 '+ 5. Flat is better than nested.\n']
121</pre></div>
122
123<P>
124As a single multi-line string it looks like this:
125
126<P>
127<div class="verbatim"><pre>
128&gt;&gt;&gt; import sys
129&gt;&gt;&gt; sys.stdout.writelines(result)
130 1. Beautiful is better than ugly.
131- 2. Explicit is better than implicit.
132- 3. Simple is better than complex.
133+ 3. Simple is better than complex.
134? ++
135- 4. Complex is better than complicated.
136? ^ ---- ^
137+ 4. Complicated is better than complex.
138? ++++ ^ ^
139+ 5. Flat is better than nested.
140</pre></div>
141
142<DIV CLASS="navigation">
143<div class='online-navigation'>
144<p></p><hr />
145<table align="center" width="100%" cellpadding="0" cellspacing="2">
146<tr>
147<td class='online-navigation'><a rel="prev" title="4.4.3 Differ Objects"
148 href="differ-objects.html"><img src='../icons/previous.png'
149 border='0' height='32' alt='Previous Page' width='32' /></A></td>
150<td class='online-navigation'><a rel="parent" title="4.4 difflib "
151 href="module-difflib.html"><img src='../icons/up.png'
152 border='0' height='32' alt='Up One Level' width='32' /></A></td>
153<td class='online-navigation'><a rel="next" title="4.5 fpformat "
154 href="module-fpformat.html"><img src='../icons/next.png'
155 border='0' height='32' alt='Next Page' width='32' /></A></td>
156<td align="center" width="100%">Python Library Reference</td>
157<td class='online-navigation'><a rel="contents" title="Table of Contents"
158 href="contents.html"><img src='../icons/contents.png'
159 border='0' height='32' alt='Contents' width='32' /></A></td>
160<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
161 border='0' height='32' alt='Module Index' width='32' /></a></td>
162<td class='online-navigation'><a rel="index" title="Index"
163 href="genindex.html"><img src='../icons/index.png'
164 border='0' height='32' alt='Index' width='32' /></A></td>
165</tr></table>
166<div class='online-navigation'>
167<b class="navlabel">Previous:</b>
168<a class="sectref" rel="prev" href="differ-objects.html">4.4.3 Differ Objects</A>
169<b class="navlabel">Up:</b>
170<a class="sectref" rel="parent" href="module-difflib.html">4.4 difflib </A>
171<b class="navlabel">Next:</b>
172<a class="sectref" rel="next" href="module-fpformat.html">4.5 fpformat </A>
173</div>
174</div>
175<hr />
176<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
177</DIV>
178<!--End of Navigation Panel-->
179<ADDRESS>
180See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
181</ADDRESS>
182</BODY>
183</HTML>