Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 3.137.219.68
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
49.27
KB
-rw-r--r--
__builtin__.html
10.26
KB
-rw-r--r--
__future__.html
13.79
KB
-rw-r--r--
__main__.html
7.05
KB
-rw-r--r--
_winreg.html
59.21
KB
-rw-r--r--
abc.html
23.9
KB
-rw-r--r--
aepack.html
13.16
KB
-rw-r--r--
aetools.html
14.91
KB
-rw-r--r--
aetypes.html
18.88
KB
-rw-r--r--
aifc.html
22.4
KB
-rw-r--r--
al.html
17.34
KB
-rw-r--r--
allos.html
33.72
KB
-rw-r--r--
anydbm.html
16.33
KB
-rw-r--r--
archiving.html
9.26
KB
-rw-r--r--
argparse.html
237.62
KB
-rw-r--r--
array.html
29.29
KB
-rw-r--r--
ast.html
34.98
KB
-rw-r--r--
asynchat.html
31.43
KB
-rw-r--r--
asyncore.html
36.51
KB
-rw-r--r--
atexit.html
16.8
KB
-rw-r--r--
audioop.html
31.36
KB
-rw-r--r--
autogil.html
8.19
KB
-rw-r--r--
base64.html
19.67
KB
-rw-r--r--
basehttpserver.html
34.04
KB
-rw-r--r--
bastion.html
11.04
KB
-rw-r--r--
bdb.html
36.68
KB
-rw-r--r--
binascii.html
20.67
KB
-rw-r--r--
binhex.html
10.58
KB
-rw-r--r--
bisect.html
23.24
KB
-rw-r--r--
bsddb.html
26.43
KB
-rw-r--r--
bz2.html
26.08
KB
-rw-r--r--
calendar.html
37.79
KB
-rw-r--r--
carbon.html
48.94
KB
-rw-r--r--
cd.html
27.96
KB
-rw-r--r--
cgi.html
49.92
KB
-rw-r--r--
cgihttpserver.html
13.1
KB
-rw-r--r--
cgitb.html
11.41
KB
-rw-r--r--
chunk.html
14.66
KB
-rw-r--r--
cmath.html
25.63
KB
-rw-r--r--
cmd.html
26.09
KB
-rw-r--r--
code.html
24.58
KB
-rw-r--r--
codecs.html
100.64
KB
-rw-r--r--
codeop.html
14.84
KB
-rw-r--r--
collections.html
133.96
KB
-rw-r--r--
colorpicker.html
7.52
KB
-rw-r--r--
colorsys.html
11.04
KB
-rw-r--r--
commands.html
14.36
KB
-rw-r--r--
compileall.html
16.83
KB
-rw-r--r--
compiler.html
67.75
KB
-rw-r--r--
configparser.html
62.13
KB
-rw-r--r--
constants.html
12.83
KB
-rw-r--r--
contextlib.html
19.39
KB
-rw-r--r--
cookie.html
39.07
KB
-rw-r--r--
cookielib.html
83.82
KB
-rw-r--r--
copy.html
12.19
KB
-rw-r--r--
copy_reg.html
13.76
KB
-rw-r--r--
crypt.html
10.04
KB
-rw-r--r--
crypto.html
7.59
KB
-rw-r--r--
csv.html
67.37
KB
-rw-r--r--
ctypes.html
238.78
KB
-rw-r--r--
curses.ascii.html
22.29
KB
-rw-r--r--
curses.html
146.63
KB
-rw-r--r--
curses.panel.html
14.39
KB
-rw-r--r--
custominterp.html
7.62
KB
-rw-r--r--
datatypes.html
16.84
KB
-rw-r--r--
datetime.html
226.59
KB
-rw-r--r--
dbhash.html
15.48
KB
-rw-r--r--
dbm.html
12.07
KB
-rw-r--r--
debug.html
10.15
KB
-rw-r--r--
decimal.html
194.44
KB
-rw-r--r--
development.html
14.17
KB
-rw-r--r--
difflib.html
84.83
KB
-rw-r--r--
dircache.html
11.41
KB
-rw-r--r--
dis.html
69.95
KB
-rw-r--r--
distutils.html
8.05
KB
-rw-r--r--
dl.html
16.33
KB
-rw-r--r--
doctest.html
165.54
KB
-rw-r--r--
docxmlrpcserver.html
16.43
KB
-rw-r--r--
dumbdbm.html
14.02
KB
-rw-r--r--
dummy_thread.html
9.43
KB
-rw-r--r--
dummy_threading.html
8.37
KB
-rw-r--r--
easydialogs.html
30.55
KB
-rw-r--r--
email-examples.html
45.65
KB
-rw-r--r--
email.charset.html
26.8
KB
-rw-r--r--
email.encoders.html
11.86
KB
-rw-r--r--
email.errors.html
15.77
KB
-rw-r--r--
email.generator.html
20.77
KB
-rw-r--r--
email.header.html
26.92
KB
-rw-r--r--
email.html
44.24
KB
-rw-r--r--
email.iterators.html
11.52
KB
-rw-r--r--
email.message.html
63.16
KB
-rw-r--r--
email.mime.html
27.93
KB
-rw-r--r--
email.parser.html
30.45
KB
-rw-r--r--
email.util.html
24.46
KB
-rw-r--r--
errno.html
37.99
KB
-rw-r--r--
exceptions.html
56.13
KB
-rw-r--r--
fcntl.html
22.67
KB
-rw-r--r--
filecmp.html
22.3
KB
-rw-r--r--
fileformats.html
9.14
KB
-rw-r--r--
fileinput.html
24.28
KB
-rw-r--r--
filesys.html
10.2
KB
-rw-r--r--
fl.html
49.92
KB
-rw-r--r--
fm.html
11.91
KB
-rw-r--r--
fnmatch.html
14.58
KB
-rw-r--r--
formatter.html
34.06
KB
-rw-r--r--
fpectl.html
16.01
KB
-rw-r--r--
fpformat.html
10.59
KB
-rw-r--r--
fractions.html
22.61
KB
-rw-r--r--
framework.html
33.34
KB
-rw-r--r--
frameworks.html
7.14
KB
-rw-r--r--
ftplib.html
43.99
KB
-rw-r--r--
functions.html
183.14
KB
-rw-r--r--
functools.html
27.17
KB
-rw-r--r--
future_builtins.html
13.04
KB
-rw-r--r--
gc.html
25.75
KB
-rw-r--r--
gdbm.html
15.96
KB
-rw-r--r--
gensuitemodule.html
11.51
KB
-rw-r--r--
getopt.html
23.66
KB
-rw-r--r--
getpass.html
10.65
KB
-rw-r--r--
gettext.html
78.76
KB
-rw-r--r--
gl.html
22.09
KB
-rw-r--r--
glob.html
13.26
KB
-rw-r--r--
grp.html
10.49
KB
-rw-r--r--
gzip.html
18.99
KB
-rw-r--r--
hashlib.html
18.2
KB
-rw-r--r--
heapq.html
31.61
KB
-rw-r--r--
hmac.html
10.46
KB
-rw-r--r--
hotshot.html
18.65
KB
-rw-r--r--
htmllib.html
25.32
KB
-rw-r--r--
htmlparser.html
39.11
KB
-rw-r--r--
httplib.html
62.95
KB
-rw-r--r--
i18n.html
9.52
KB
-rw-r--r--
ic.html
17.17
KB
-rw-r--r--
idle.html
20.9
KB
-rw-r--r--
imageop.html
14.76
KB
-rw-r--r--
imaplib.html
51.99
KB
-rw-r--r--
imgfile.html
11.71
KB
-rw-r--r--
imghdr.html
11.3
KB
-rw-r--r--
imp.html
34.34
KB
-rw-r--r--
importlib.html
8.26
KB
-rw-r--r--
imputil.html
31.81
KB
-rw-r--r--
index.html
72.78
KB
-rw-r--r--
inspect.html
50.71
KB
-rw-r--r--
internet.html
24.87
KB
-rw-r--r--
intro.html
8.93
KB
-rw-r--r--
io.html
98.13
KB
-rw-r--r--
ipc.html
13.41
KB
-rw-r--r--
itertools.html
115.91
KB
-rw-r--r--
jpeg.html
12.74
KB
-rw-r--r--
json.html
67.04
KB
-rw-r--r--
keyword.html
7.68
KB
-rw-r--r--
language.html
11.03
KB
-rw-r--r--
linecache.html
10.59
KB
-rw-r--r--
locale.html
55.14
KB
-rw-r--r--
logging.config.html
63.36
KB
-rw-r--r--
logging.handlers.html
69.64
KB
-rw-r--r--
logging.html
95.64
KB
-rw-r--r--
mac.html
21.79
KB
-rw-r--r--
macos.html
14.76
KB
-rw-r--r--
macosa.html
12.96
KB
-rw-r--r--
macostools.html
15.52
KB
-rw-r--r--
macpath.html
7.76
KB
-rw-r--r--
mailbox.html
156.75
KB
-rw-r--r--
mailcap.html
13.21
KB
-rw-r--r--
markup.html
18.77
KB
-rw-r--r--
marshal.html
17.98
KB
-rw-r--r--
math.html
39.24
KB
-rw-r--r--
md5.html
13.97
KB
-rw-r--r--
mhlib.html
21.54
KB
-rw-r--r--
mimetools.html
19.25
KB
-rw-r--r--
mimetypes.html
28.39
KB
-rw-r--r--
mimewriter.html
15.02
KB
-rw-r--r--
mimify.html
13.36
KB
-rw-r--r--
miniaeframe.html
12.2
KB
-rw-r--r--
misc.html
6.87
KB
-rw-r--r--
mm.html
9.03
KB
-rw-r--r--
mmap.html
28.36
KB
-rw-r--r--
modulefinder.html
15.31
KB
-rw-r--r--
modules.html
8.46
KB
-rw-r--r--
msilib.html
52.43
KB
-rw-r--r--
msvcrt.html
19.37
KB
-rw-r--r--
multifile.html
24.3
KB
-rw-r--r--
multiprocessing.html
365.71
KB
-rw-r--r--
mutex.html
11.23
KB
-rw-r--r--
netdata.html
16.98
KB
-rw-r--r--
netrc.html
12.3
KB
-rw-r--r--
new.html
12.12
KB
-rw-r--r--
nis.html
10.64
KB
-rw-r--r--
nntplib.html
41.92
KB
-rw-r--r--
numbers.html
37.75
KB
-rw-r--r--
numeric.html
13.55
KB
-rw-r--r--
operator.html
82
KB
-rw-r--r--
optparse.html
222.56
KB
-rw-r--r--
os.html
214.25
KB
-rw-r--r--
os.path.html
38.34
KB
-rw-r--r--
ossaudiodev.html
41.5
KB
-rw-r--r--
othergui.html
9.08
KB
-rw-r--r--
parser.html
39.36
KB
-rw-r--r--
pdb.html
33.96
KB
-rw-r--r--
persistence.html
14.87
KB
-rw-r--r--
pickle.html
102.27
KB
-rw-r--r--
pickletools.html
10.63
KB
-rw-r--r--
pipes.html
18.01
KB
-rw-r--r--
pkgutil.html
25.11
KB
-rw-r--r--
platform.html
28.37
KB
-rw-r--r--
plistlib.html
17.03
KB
-rw-r--r--
popen2.html
25.43
KB
-rw-r--r--
poplib.html
22.32
KB
-rw-r--r--
posix.html
14.41
KB
-rw-r--r--
posixfile.html
19.76
KB
-rw-r--r--
pprint.html
29.92
KB
-rw-r--r--
profile.html
63.56
KB
-rw-r--r--
pty.html
9.48
KB
-rw-r--r--
pwd.html
11.43
KB
-rw-r--r--
py_compile.html
11.12
KB
-rw-r--r--
pyclbr.html
14.71
KB
-rw-r--r--
pydoc.html
11.48
KB
-rw-r--r--
pyexpat.html
71.53
KB
-rw-r--r--
python.html
12.27
KB
-rw-r--r--
queue.html
24.22
KB
-rw-r--r--
quopri.html
11.9
KB
-rw-r--r--
random.html
37.83
KB
-rw-r--r--
re.html
134.74
KB
-rw-r--r--
readline.html
28.24
KB
-rw-r--r--
repr.html
20.43
KB
-rw-r--r--
resource.html
26.48
KB
-rw-r--r--
restricted.html
11.65
KB
-rw-r--r--
rexec.html
37.41
KB
-rw-r--r--
rfc822.html
42.22
KB
-rw-r--r--
rlcompleter.html
13.51
KB
-rw-r--r--
robotparser.html
12.27
KB
-rw-r--r--
runpy.html
19.34
KB
-rw-r--r--
sched.html
18.54
KB
-rw-r--r--
scrolledtext.html
9.32
KB
-rw-r--r--
select.html
39.67
KB
-rw-r--r--
sets.html
36.92
KB
-rw-r--r--
sgi.html
9.71
KB
-rw-r--r--
sgmllib.html
30.77
KB
-rw-r--r--
sha.html
12.09
KB
-rw-r--r--
shelve.html
27.02
KB
-rw-r--r--
shlex.html
32.1
KB
-rw-r--r--
shutil.html
40.22
KB
-rw-r--r--
signal.html
31.14
KB
-rw-r--r--
simplehttpserver.html
18.41
KB
-rw-r--r--
simplexmlrpcserver.html
31.39
KB
-rw-r--r--
site.html
23.64
KB
-rw-r--r--
smtpd.html
12.46
KB
-rw-r--r--
smtplib.html
42.13
KB
-rw-r--r--
sndhdr.html
10.02
KB
-rw-r--r--
socket.html
106.34
KB
-rw-r--r--
socketserver.html
59.83
KB
-rw-r--r--
someos.html
15.11
KB
-rw-r--r--
spwd.html
10.33
KB
-rw-r--r--
sqlite3.html
139.5
KB
-rw-r--r--
ssl.html
65.62
KB
-rw-r--r--
stat.html
32.31
KB
-rw-r--r--
statvfs.html
10.6
KB
-rw-r--r--
stdtypes.html
260.4
KB
-rw-r--r--
string.html
106.65
KB
-rw-r--r--
stringio.html
18.81
KB
-rw-r--r--
stringprep.html
16.13
KB
-rw-r--r--
strings.html
14.93
KB
-rw-r--r--
struct.html
40.88
KB
-rw-r--r--
subprocess.html
84.91
KB
-rw-r--r--
sun.html
6.84
KB
-rw-r--r--
sunau.html
27.1
KB
-rw-r--r--
sunaudio.html
17.79
KB
-rw-r--r--
symbol.html
7.66
KB
-rw-r--r--
symtable.html
22.94
KB
-rw-r--r--
sys.html
98.7
KB
-rw-r--r--
sysconfig.html
23.84
KB
-rw-r--r--
syslog.html
17.92
KB
-rw-r--r--
tabnanny.html
10.63
KB
-rw-r--r--
tarfile.html
78.68
KB
-rw-r--r--
telnetlib.html
25.48
KB
-rw-r--r--
tempfile.html
29.42
KB
-rw-r--r--
termios.html
16.01
KB
-rw-r--r--
test.html
52.62
KB
-rw-r--r--
textwrap.html
27.25
KB
-rw-r--r--
thread.html
20.47
KB
-rw-r--r--
threading.html
76.69
KB
-rw-r--r--
time.html
56.93
KB
-rw-r--r--
timeit.html
36.27
KB
-rw-r--r--
tix.html
46.96
KB
-rw-r--r--
tk.html
23.64
KB
-rw-r--r--
tkinter.html
67.67
KB
-rw-r--r--
token.html
19.62
KB
-rw-r--r--
tokenize.html
18.45
KB
-rw-r--r--
trace.html
25.54
KB
-rw-r--r--
traceback.html
33.44
KB
-rw-r--r--
ttk.html
101.75
KB
-rw-r--r--
tty.html
9.06
KB
-rw-r--r--
turtle.html
211.74
KB
-rw-r--r--
types.html
27.59
KB
-rw-r--r--
undoc.html
23.16
KB
-rw-r--r--
unicodedata.html
18.55
KB
-rw-r--r--
unittest.html
202.85
KB
-rw-r--r--
unix.html
10.55
KB
-rw-r--r--
urllib.html
58.68
KB
-rw-r--r--
urllib2.html
100.58
KB
-rw-r--r--
urlparse.html
40.41
KB
-rw-r--r--
user.html
11.83
KB
-rw-r--r--
userdict.html
29.73
KB
-rw-r--r--
uu.html
11.03
KB
-rw-r--r--
uuid.html
28.19
KB
-rw-r--r--
warnings.html
46.6
KB
-rw-r--r--
wave.html
22.22
KB
-rw-r--r--
weakref.html
36.52
KB
-rw-r--r--
webbrowser.html
23.07
KB
-rw-r--r--
whichdb.html
8.85
KB
-rw-r--r--
windows.html
9.33
KB
-rw-r--r--
winsound.html
18.75
KB
-rw-r--r--
wsgiref.html
81.04
KB
-rw-r--r--
xdrlib.html
29.94
KB
-rw-r--r--
xml.dom.html
89.04
KB
-rw-r--r--
xml.dom.minidom.html
40.42
KB
-rw-r--r--
xml.dom.pulldom.html
12.71
KB
-rw-r--r--
xml.etree.elementtree.html
93.22
KB
-rw-r--r--
xml.html
16.49
KB
-rw-r--r--
xml.sax.handler.html
38.63
KB
-rw-r--r--
xml.sax.html
20.22
KB
-rw-r--r--
xml.sax.reader.html
39.09
KB
-rw-r--r--
xml.sax.utils.html
14.26
KB
-rw-r--r--
xmlrpclib.html
60.79
KB
-rw-r--r--
zipfile.html
53.14
KB
-rw-r--r--
zipimport.html
20.42
KB
-rw-r--r--
zlib.html
25.46
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : difflib.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>7.4. difflib — Helpers for computing deltas — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="7. String Services" href="strings.html" /> <link rel="next" title="7.5. StringIO — Read and write strings as files" href="stringio.html" /> <link rel="prev" title="7.3. struct — Interpret strings as packed binary data" href="struct.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="stringio.html" title="7.5. StringIO — Read and write strings as files" accesskey="N">next</a> |</li> <li class="right" > <a href="struct.html" title="7.3. struct — Interpret strings as packed binary data" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="strings.html" accesskey="U">7. String Services</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-difflib"> <span id="difflib-helpers-for-computing-deltas"></span><h1>7.4. <a class="reference internal" href="#module-difflib" title="difflib: Helpers for computing differences between objects."><tt class="xref py py-mod docutils literal"><span class="pre">difflib</span></tt></a> — Helpers for computing deltas<a class="headerlink" href="#module-difflib" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.1.</span></p> <p>This module provides classes and functions for comparing sequences. It can be used for example, for comparing files, and can produce difference information in various formats, including HTML and context and unified diffs. For comparing directories and files, see also, the <a class="reference internal" href="filecmp.html#module-filecmp" title="filecmp: Compare files efficiently."><tt class="xref py py-mod docutils literal"><span class="pre">filecmp</span></tt></a> module.</p> <dl class="class"> <dt id="difflib.SequenceMatcher"> <em class="property">class </em><tt class="descclassname">difflib.</tt><tt class="descname">SequenceMatcher</tt><a class="headerlink" href="#difflib.SequenceMatcher" title="Permalink to this definition">¶</a></dt> <dd><p>This is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are <a class="reference internal" href="../glossary.html#term-hashable"><em class="xref std std-term">hashable</em></a>. 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> <p><strong>Timing:</strong> The basic Ratcliff-Obershelp algorithm is cubic time in the worst case and quadratic time in the expected case. <a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a> 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.</p> <p><strong>Automatic junk heuristic:</strong> <a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a> supports a heuristic that automatically treats certain sequence items as junk. The heuristic counts how many times each individual item appears in the sequence. If an item’s duplicates (after the first one) account for more than 1% of the sequence and the sequence is at least 200 items long, this item is marked as “popular” and is treated as junk for the purpose of sequence matching. This heuristic can be turned off by setting the <tt class="docutils literal"><span class="pre">autojunk</span></tt> argument to <tt class="docutils literal"><span class="pre">False</span></tt> when creating the <a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.7.1: </span>The <em>autojunk</em> parameter.</p> </dd></dl> <dl class="class"> <dt id="difflib.Differ"> <em class="property">class </em><tt class="descclassname">difflib.</tt><tt class="descname">Differ</tt><a class="headerlink" href="#difflib.Differ" title="Permalink to this definition">¶</a></dt> <dd><p>This is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses <a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a> both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines.</p> <p>Each line of a <a class="reference internal" href="#difflib.Differ" title="difflib.Differ"><tt class="xref py py-class docutils literal"><span class="pre">Differ</span></tt></a> delta begins with a two-letter code:</p> <table border="1" class="docutils"> <colgroup> <col width="19%" /> <col width="81%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Code</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'-</span> <span class="pre">'</span></tt></td> <td>line unique to sequence 1</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'+</span> <span class="pre">'</span></tt></td> <td>line unique to sequence 2</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'</span> <span class="pre">'</span></tt></td> <td>line common to both sequences</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'?</span> <span class="pre">'</span></tt></td> <td>line not present in either input sequence</td> </tr> </tbody> </table> <p>Lines beginning with ‘<tt class="docutils literal"><span class="pre">?</span></tt>‘ 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.</p> </dd></dl> <dl class="class"> <dt id="difflib.HtmlDiff"> <em class="property">class </em><tt class="descclassname">difflib.</tt><tt class="descname">HtmlDiff</tt><a class="headerlink" href="#difflib.HtmlDiff" title="Permalink to this definition">¶</a></dt> <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> <p>The constructor for this class is:</p> <dl class="function"> <dt id="difflib.HtmlDiff.__init__"> <tt class="descname">__init__</tt><big>(</big><em>tabsize=8</em>, <em>wrapcolumn=None</em>, <em>linejunk=None</em>, <em>charjunk=IS_CHARACTER_JUNK</em><big>)</big><a class="headerlink" href="#difflib.HtmlDiff.__init__" title="Permalink to this definition">¶</a></dt> <dd><p>Initializes instance of <a class="reference internal" href="#difflib.HtmlDiff" title="difflib.HtmlDiff"><tt class="xref py py-class docutils literal"><span class="pre">HtmlDiff</span></tt></a>.</p> <p><em>tabsize</em> is an optional keyword argument to specify tab stop spacing and defaults to <tt class="docutils literal"><span class="pre">8</span></tt>.</p> <p><em>wrapcolumn</em> is an optional keyword to specify column number where lines are broken and wrapped, defaults to <tt class="docutils literal"><span class="pre">None</span></tt> where lines are not wrapped.</p> <p><em>linejunk</em> and <em>charjunk</em> are optional keyword arguments passed into <tt class="docutils literal"><span class="pre">ndiff()</span></tt> (used by <a class="reference internal" href="#difflib.HtmlDiff" title="difflib.HtmlDiff"><tt class="xref py py-class docutils literal"><span class="pre">HtmlDiff</span></tt></a> to generate the side by side HTML differences). See <tt class="docutils literal"><span class="pre">ndiff()</span></tt> documentation for argument default values and descriptions.</p> </dd></dl> <p>The following methods are public:</p> <dl class="function"> <dt id="difflib.HtmlDiff.make_file"> <tt class="descname">make_file</tt><big>(</big><em>fromlines, tolines [, fromdesc][, todesc][, context][, numlines]</em><big>)</big><a class="headerlink" href="#difflib.HtmlDiff.make_file" title="Permalink to this definition">¶</a></dt> <dd><p>Compares <em>fromlines</em> and <em>tolines</em> (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> <p><em>fromdesc</em> and <em>todesc</em> are optional keyword arguments to specify from/to file column header strings (both default to an empty string).</p> <p><em>context</em> and <em>numlines</em> are both optional keyword arguments. Set <em>context</em> to <tt class="docutils literal"><span class="pre">True</span></tt> when contextual differences are to be shown, else the default is <tt class="docutils literal"><span class="pre">False</span></tt> to show the full files. <em>numlines</em> defaults to <tt class="docutils literal"><span class="pre">5</span></tt>. When <em>context</em> is <tt class="docutils literal"><span class="pre">True</span></tt> <em>numlines</em> controls the number of context lines which surround the difference highlights. When <em>context</em> is <tt class="docutils literal"><span class="pre">False</span></tt> <em>numlines</em> 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).</p> </dd></dl> <dl class="function"> <dt id="difflib.HtmlDiff.make_table"> <tt class="descname">make_table</tt><big>(</big><em>fromlines, tolines [, fromdesc][, todesc][, context][, numlines]</em><big>)</big><a class="headerlink" href="#difflib.HtmlDiff.make_table" title="Permalink to this definition">¶</a></dt> <dd><p>Compares <em>fromlines</em> and <em>tolines</em> (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> <p>The arguments for this method are the same as those for the <a class="reference internal" href="#difflib.HtmlDiff.make_file" title="difflib.HtmlDiff.make_file"><tt class="xref py py-meth docutils literal"><span class="pre">make_file()</span></tt></a> method.</p> </dd></dl> <p><tt class="file docutils literal"><span class="pre">Tools/scripts/diff.py</span></tt> is a command-line front-end to this class and contains a good example of its use.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.4.</span></p> </dd></dl> <dl class="function"> <dt id="difflib.context_diff"> <tt class="descclassname">difflib.</tt><tt class="descname">context_diff</tt><big>(</big><em>a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]</em><big>)</big><a class="headerlink" href="#difflib.context_diff" title="Permalink to this definition">¶</a></dt> <dd><p>Compare <em>a</em> and <em>b</em> (lists of strings); return a delta (a <a class="reference internal" href="../glossary.html#term-generator"><em class="xref std std-term">generator</em></a> generating the delta lines) in context diff format.</p> <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 <em>n</em> which defaults to three.</p> <p>By default, the diff control lines (those with <tt class="docutils literal"><span class="pre">***</span></tt> or <tt class="docutils literal"><span class="pre">---</span></tt>) are created with a trailing newline. This is helpful so that inputs created from <a class="reference internal" href="stdtypes.html#file.readlines" title="file.readlines"><tt class="xref py py-func docutils literal"><span class="pre">file.readlines()</span></tt></a> result in diffs that are suitable for use with <a class="reference internal" href="stdtypes.html#file.writelines" title="file.writelines"><tt class="xref py py-func docutils literal"><span class="pre">file.writelines()</span></tt></a> since both the inputs and outputs have trailing newlines.</p> <p>For inputs that do not have trailing newlines, set the <em>lineterm</em> argument to <tt class="docutils literal"><span class="pre">""</span></tt> so that the output will be uniformly newline free.</p> <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 <em>fromfile</em>, <em>tofile</em>, <em>fromfiledate</em>, and <em>tofiledate</em>. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s1</span> <span class="o">=</span> <span class="p">[</span><span class="s">'bacon</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'eggs</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'ham</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'guido</span><span class="se">\n</span><span class="s">'</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">s2</span> <span class="o">=</span> <span class="p">[</span><span class="s">'python</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'eggy</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'hamster</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'guido</span><span class="se">\n</span><span class="s">'</span><span class="p">]</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">context_diff</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">,</span> <span class="n">fromfile</span><span class="o">=</span><span class="s">'before.py'</span><span class="p">,</span> <span class="n">tofile</span><span class="o">=</span><span class="s">'after.py'</span><span class="p">):</span> <span class="gp">... </span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="go">*** before.py</span> <span class="go">--- after.py</span> <span class="go">***************</span> <span class="go">*** 1,4 ****</span> <span class="go">! bacon</span> <span class="go">! eggs</span> <span class="go">! ham</span> <span class="go"> guido</span> <span class="go">--- 1,4 ----</span> <span class="go">! python</span> <span class="go">! eggy</span> <span class="go">! hamster</span> <span class="go"> guido</span> </pre></div> </div> <p>See <a class="reference internal" href="#difflib-interface"><em>A command-line interface to difflib</em></a> for a more detailed example.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <dl class="function"> <dt id="difflib.get_close_matches"> <tt class="descclassname">difflib.</tt><tt class="descname">get_close_matches</tt><big>(</big><em>word, possibilities[, n][, cutoff]</em><big>)</big><a class="headerlink" href="#difflib.get_close_matches" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of the best “good enough” matches. <em>word</em> is a sequence for which close matches are desired (typically a string), and <em>possibilities</em> is a list of sequences against which to match <em>word</em> (typically a list of strings).</p> <p>Optional argument <em>n</em> (default <tt class="docutils literal"><span class="pre">3</span></tt>) is the maximum number of close matches to return; <em>n</em> must be greater than <tt class="docutils literal"><span class="pre">0</span></tt>.</p> <p>Optional argument <em>cutoff</em> (default <tt class="docutils literal"><span class="pre">0.6</span></tt>) is a float in the range [0, 1]. Possibilities that don’t score at least that similar to <em>word</em> are ignored.</p> <p>The best (no more than <em>n</em>) matches among the possibilities are returned in a list, sorted by similarity score, most similar first.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">get_close_matches</span><span class="p">(</span><span class="s">'appel'</span><span class="p">,</span> <span class="p">[</span><span class="s">'ape'</span><span class="p">,</span> <span class="s">'apple'</span><span class="p">,</span> <span class="s">'peach'</span><span class="p">,</span> <span class="s">'puppy'</span><span class="p">])</span> <span class="go">['apple', 'ape']</span> <span class="gp">>>> </span><span class="kn">import</span> <span class="nn">keyword</span> <span class="gp">>>> </span><span class="n">get_close_matches</span><span class="p">(</span><span class="s">'wheel'</span><span class="p">,</span> <span class="n">keyword</span><span class="o">.</span><span class="n">kwlist</span><span class="p">)</span> <span class="go">['while']</span> <span class="gp">>>> </span><span class="n">get_close_matches</span><span class="p">(</span><span class="s">'apple'</span><span class="p">,</span> <span class="n">keyword</span><span class="o">.</span><span class="n">kwlist</span><span class="p">)</span> <span class="go">[]</span> <span class="gp">>>> </span><span class="n">get_close_matches</span><span class="p">(</span><span class="s">'accept'</span><span class="p">,</span> <span class="n">keyword</span><span class="o">.</span><span class="n">kwlist</span><span class="p">)</span> <span class="go">['except']</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="difflib.ndiff"> <tt class="descclassname">difflib.</tt><tt class="descname">ndiff</tt><big>(</big><em>a, b[, linejunk][, charjunk]</em><big>)</big><a class="headerlink" href="#difflib.ndiff" title="Permalink to this definition">¶</a></dt> <dd><p>Compare <em>a</em> and <em>b</em> (lists of strings); return a <a class="reference internal" href="#difflib.Differ" title="difflib.Differ"><tt class="xref py py-class docutils literal"><span class="pre">Differ</span></tt></a>-style delta (a <a class="reference internal" href="../glossary.html#term-generator"><em class="xref std std-term">generator</em></a> generating the delta lines).</p> <p>Optional keyword parameters <em>linejunk</em> and <em>charjunk</em> are for filter functions (or <tt class="docutils literal"><span class="pre">None</span></tt>):</p> <p><em>linejunk</em>: A function that accepts a single string argument, and returns true if the string is junk, or false if not. The default is (<tt class="docutils literal"><span class="pre">None</span></tt>), starting with Python 2.3. Before then, the default was the module-level function <a class="reference internal" href="#difflib.IS_LINE_JUNK" title="difflib.IS_LINE_JUNK"><tt class="xref py py-func docutils literal"><span class="pre">IS_LINE_JUNK()</span></tt></a>, which filters out lines without visible characters, except for at most one pound character (<tt class="docutils literal"><span class="pre">'#'</span></tt>). As of Python 2.3, the underlying <a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a> 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> <p><em>charjunk</em>: 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 <a class="reference internal" href="#difflib.IS_CHARACTER_JUNK" title="difflib.IS_CHARACTER_JUNK"><tt class="xref py py-func docutils literal"><span class="pre">IS_CHARACTER_JUNK()</span></tt></a>, which filters out whitespace characters (a blank or tab; note: bad idea to include newline in this!).</p> <p><tt class="file docutils literal"><span class="pre">Tools/scripts/ndiff.py</span></tt> is a command-line front-end to this function.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">diff</span> <span class="o">=</span> <span class="n">ndiff</span><span class="p">(</span><span class="s">'one</span><span class="se">\n</span><span class="s">two</span><span class="se">\n</span><span class="s">three</span><span class="se">\n</span><span class="s">'</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="gp">... </span> <span class="s">'ore</span><span class="se">\n</span><span class="s">tree</span><span class="se">\n</span><span class="s">emu</span><span class="se">\n</span><span class="s">'</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="gp">>>> </span><span class="k">print</span> <span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">diff</span><span class="p">),</span> <span class="go">- one</span> <span class="go">? ^</span> <span class="go">+ ore</span> <span class="go">? ^</span> <span class="go">- two</span> <span class="go">- three</span> <span class="go">? -</span> <span class="go">+ tree</span> <span class="go">+ emu</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="difflib.restore"> <tt class="descclassname">difflib.</tt><tt class="descname">restore</tt><big>(</big><em>sequence</em>, <em>which</em><big>)</big><a class="headerlink" href="#difflib.restore" title="Permalink to this definition">¶</a></dt> <dd><p>Return one of the two sequences that generated a delta.</p> <p>Given a <em>sequence</em> produced by <a class="reference internal" href="#difflib.Differ.compare" title="difflib.Differ.compare"><tt class="xref py py-meth docutils literal"><span class="pre">Differ.compare()</span></tt></a> or <a class="reference internal" href="#difflib.ndiff" title="difflib.ndiff"><tt class="xref py py-func docutils literal"><span class="pre">ndiff()</span></tt></a>, extract lines originating from file 1 or 2 (parameter <em>which</em>), stripping off line prefixes.</p> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">diff</span> <span class="o">=</span> <span class="n">ndiff</span><span class="p">(</span><span class="s">'one</span><span class="se">\n</span><span class="s">two</span><span class="se">\n</span><span class="s">three</span><span class="se">\n</span><span class="s">'</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="gp">... </span> <span class="s">'ore</span><span class="se">\n</span><span class="s">tree</span><span class="se">\n</span><span class="s">emu</span><span class="se">\n</span><span class="s">'</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="gp">>>> </span><span class="n">diff</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">diff</span><span class="p">)</span> <span class="c"># materialize the generated delta into a list</span> <span class="gp">>>> </span><span class="k">print</span> <span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">restore</span><span class="p">(</span><span class="n">diff</span><span class="p">,</span> <span class="mi">1</span><span class="p">)),</span> <span class="go">one</span> <span class="go">two</span> <span class="go">three</span> <span class="gp">>>> </span><span class="k">print</span> <span class="s">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">restore</span><span class="p">(</span><span class="n">diff</span><span class="p">,</span> <span class="mi">2</span><span class="p">)),</span> <span class="go">ore</span> <span class="go">tree</span> <span class="go">emu</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="difflib.unified_diff"> <tt class="descclassname">difflib.</tt><tt class="descname">unified_diff</tt><big>(</big><em>a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]</em><big>)</big><a class="headerlink" href="#difflib.unified_diff" title="Permalink to this definition">¶</a></dt> <dd><p>Compare <em>a</em> and <em>b</em> (lists of strings); return a delta (a <a class="reference internal" href="../glossary.html#term-generator"><em class="xref std std-term">generator</em></a> generating the delta lines) in unified diff format.</p> <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 <em>n</em> which defaults to three.</p> <p>By default, the diff control lines (those with <tt class="docutils literal"><span class="pre">---</span></tt>, <tt class="docutils literal"><span class="pre">+++</span></tt>, or <tt class="docutils literal"><span class="pre">@@</span></tt>) are created with a trailing newline. This is helpful so that inputs created from <a class="reference internal" href="stdtypes.html#file.readlines" title="file.readlines"><tt class="xref py py-func docutils literal"><span class="pre">file.readlines()</span></tt></a> result in diffs that are suitable for use with <a class="reference internal" href="stdtypes.html#file.writelines" title="file.writelines"><tt class="xref py py-func docutils literal"><span class="pre">file.writelines()</span></tt></a> since both the inputs and outputs have trailing newlines.</p> <p>For inputs that do not have trailing newlines, set the <em>lineterm</em> argument to <tt class="docutils literal"><span class="pre">""</span></tt> so that the output will be uniformly newline free.</p> <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 <em>fromfile</em>, <em>tofile</em>, <em>fromfiledate</em>, and <em>tofiledate</em>. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s1</span> <span class="o">=</span> <span class="p">[</span><span class="s">'bacon</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'eggs</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'ham</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'guido</span><span class="se">\n</span><span class="s">'</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">s2</span> <span class="o">=</span> <span class="p">[</span><span class="s">'python</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'eggy</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'hamster</span><span class="se">\n</span><span class="s">'</span><span class="p">,</span> <span class="s">'guido</span><span class="se">\n</span><span class="s">'</span><span class="p">]</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">unified_diff</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">,</span> <span class="n">fromfile</span><span class="o">=</span><span class="s">'before.py'</span><span class="p">,</span> <span class="n">tofile</span><span class="o">=</span><span class="s">'after.py'</span><span class="p">):</span> <span class="gp">... </span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="go">--- before.py</span> <span class="go">+++ after.py</span> <span class="go">@@ -1,4 +1,4 @@</span> <span class="go">-bacon</span> <span class="go">-eggs</span> <span class="go">-ham</span> <span class="go">+python</span> <span class="go">+eggy</span> <span class="go">+hamster</span> <span class="go"> guido</span> </pre></div> </div> <p>See <a class="reference internal" href="#difflib-interface"><em>A command-line interface to difflib</em></a> for a more detailed example.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <dl class="function"> <dt id="difflib.IS_LINE_JUNK"> <tt class="descclassname">difflib.</tt><tt class="descname">IS_LINE_JUNK</tt><big>(</big><em>line</em><big>)</big><a class="headerlink" href="#difflib.IS_LINE_JUNK" title="Permalink to this definition">¶</a></dt> <dd><p>Return true for ignorable lines. The line <em>line</em> is ignorable if <em>line</em> is blank or contains a single <tt class="docutils literal"><span class="pre">'#'</span></tt>, otherwise it is not ignorable. Used as a default for parameter <em>linejunk</em> in <a class="reference internal" href="#difflib.ndiff" title="difflib.ndiff"><tt class="xref py py-func docutils literal"><span class="pre">ndiff()</span></tt></a> before Python 2.3.</p> </dd></dl> <dl class="function"> <dt id="difflib.IS_CHARACTER_JUNK"> <tt class="descclassname">difflib.</tt><tt class="descname">IS_CHARACTER_JUNK</tt><big>(</big><em>ch</em><big>)</big><a class="headerlink" href="#difflib.IS_CHARACTER_JUNK" title="Permalink to this definition">¶</a></dt> <dd><p>Return true for ignorable characters. The character <em>ch</em> is ignorable if <em>ch</em> is a space or tab, otherwise it is not ignorable. Used as a default for parameter <em>charjunk</em> in <a class="reference internal" href="#difflib.ndiff" title="difflib.ndiff"><tt class="xref py py-func docutils literal"><span class="pre">ndiff()</span></tt></a>.</p> </dd></dl> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><a class="reference external" href="http://www.ddj.com/184407970?pgno=5">Pattern Matching: The Gestalt Approach</a></dt> <dd>Discussion of a similar algorithm by John W. Ratcliff and D. E. Metzener. This was published in <a class="reference external" href="http://www.ddj.com/">Dr. Dobb’s Journal</a> in July, 1988.</dd> </dl> </div> <div class="section" id="sequencematcher-objects"> <span id="sequence-matcher"></span><h2>7.4.1. SequenceMatcher Objects<a class="headerlink" href="#sequencematcher-objects" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a> class has this constructor:</p> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">difflib.</tt><tt class="descname">SequenceMatcher</tt><big>(</big><em>isjunk=None</em>, <em>a=''</em>, <em>b=''</em>, <em>autojunk=True</em><big>)</big></dt> <dd><p>Optional argument <em>isjunk</em> must be <tt class="docutils literal"><span class="pre">None</span></tt> (the default) or a one-argument function that takes a sequence element and returns true if and only if the element is “junk” and should be ignored. Passing <tt class="docutils literal"><span class="pre">None</span></tt> for <em>isjunk</em> is equivalent to passing <tt class="docutils literal"><span class="pre">lambda</span> <span class="pre">x:</span> <span class="pre">0</span></tt>; in other words, no elements are ignored. For example, pass:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="ow">in</span> <span class="s">" </span><span class="se">\t</span><span class="s">"</span> </pre></div> </div> <p>if you’re comparing lines as sequences of characters, and don’t want to synch up on blanks or hard tabs.</p> <p>The optional arguments <em>a</em> and <em>b</em> are sequences to be compared; both default to empty strings. The elements of both sequences must be <a class="reference internal" href="../glossary.html#term-hashable"><em class="xref std std-term">hashable</em></a>.</p> <p>The optional argument <em>autojunk</em> can be used to disable the automatic junk heuristic.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.7.1: </span>The <em>autojunk</em> parameter.</p> <p><a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a> objects have the following methods:</p> <dl class="method"> <dt id="difflib.SequenceMatcher.set_seqs"> <tt class="descname">set_seqs</tt><big>(</big><em>a</em>, <em>b</em><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.set_seqs" title="Permalink to this definition">¶</a></dt> <dd><p>Set the two sequences to be compared.</p> </dd></dl> <p><a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a> computes and caches detailed information about the second sequence, so if you want to compare one sequence against many sequences, use <a class="reference internal" href="#difflib.SequenceMatcher.set_seq2" title="difflib.SequenceMatcher.set_seq2"><tt class="xref py py-meth docutils literal"><span class="pre">set_seq2()</span></tt></a> to set the commonly used sequence once and call <a class="reference internal" href="#difflib.SequenceMatcher.set_seq1" title="difflib.SequenceMatcher.set_seq1"><tt class="xref py py-meth docutils literal"><span class="pre">set_seq1()</span></tt></a> repeatedly, once for each of the other sequences.</p> <dl class="method"> <dt id="difflib.SequenceMatcher.set_seq1"> <tt class="descname">set_seq1</tt><big>(</big><em>a</em><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.set_seq1" title="Permalink to this definition">¶</a></dt> <dd><p>Set the first sequence to be compared. The second sequence to be compared is not changed.</p> </dd></dl> <dl class="method"> <dt id="difflib.SequenceMatcher.set_seq2"> <tt class="descname">set_seq2</tt><big>(</big><em>b</em><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.set_seq2" title="Permalink to this definition">¶</a></dt> <dd><p>Set the second sequence to be compared. The first sequence to be compared is not changed.</p> </dd></dl> <dl class="method"> <dt id="difflib.SequenceMatcher.find_longest_match"> <tt class="descname">find_longest_match</tt><big>(</big><em>alo</em>, <em>ahi</em>, <em>blo</em>, <em>bhi</em><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.find_longest_match" title="Permalink to this definition">¶</a></dt> <dd><p>Find longest matching block in <tt class="docutils literal"><span class="pre">a[alo:ahi]</span></tt> and <tt class="docutils literal"><span class="pre">b[blo:bhi]</span></tt>.</p> <p>If <em>isjunk</em> was omitted or <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="#difflib.SequenceMatcher.find_longest_match" title="difflib.SequenceMatcher.find_longest_match"><tt class="xref py py-meth docutils literal"><span class="pre">find_longest_match()</span></tt></a> returns <tt class="docutils literal"><span class="pre">(i,</span> <span class="pre">j,</span> <span class="pre">k)</span></tt> such that <tt class="docutils literal"><span class="pre">a[i:i+k]</span></tt> is equal to <tt class="docutils literal"><span class="pre">b[j:j+k]</span></tt>, where <tt class="docutils literal"><span class="pre">alo</span> <span class="pre"><=</span> <span class="pre">i</span> <span class="pre"><=</span> <span class="pre">i+k</span> <span class="pre"><=</span> <span class="pre">ahi</span></tt> and <tt class="docutils literal"><span class="pre">blo</span> <span class="pre"><=</span> <span class="pre">j</span> <span class="pre"><=</span> <span class="pre">j+k</span> <span class="pre"><=</span> <span class="pre">bhi</span></tt>. For all <tt class="docutils literal"><span class="pre">(i',</span> <span class="pre">j',</span> <span class="pre">k')</span></tt> meeting those conditions, the additional conditions <tt class="docutils literal"><span class="pre">k</span> <span class="pre">>=</span> <span class="pre">k'</span></tt>, <tt class="docutils literal"><span class="pre">i</span> <span class="pre"><=</span> <span class="pre">i'</span></tt>, and if <tt class="docutils literal"><span class="pre">i</span> <span class="pre">==</span> <span class="pre">i'</span></tt>, <tt class="docutils literal"><span class="pre">j</span> <span class="pre"><=</span> <span class="pre">j'</span></tt> are also met. In other words, of all maximal matching blocks, return one that starts earliest in <em>a</em>, and of all those maximal matching blocks that start earliest in <em>a</em>, return the one that starts earliest in <em>b</em>.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">SequenceMatcher</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="s">" abcd"</span><span class="p">,</span> <span class="s">"abcd abcd"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">find_longest_match</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span> <span class="go">Match(a=0, b=4, size=5)</span> </pre></div> </div> <p>If <em>isjunk</em> was provided, first the longest matching block is determined as above, but with the additional restriction that no junk element appears in the block. Then that block is extended as far as possible by matching (only) junk elements on both sides. So the resulting block never matches on junk except as identical junk happens to be adjacent to an interesting match.</p> <p>Here’s the same example as before, but considering blanks to be junk. That prevents <tt class="docutils literal"><span class="pre">'</span> <span class="pre">abcd'</span></tt> from matching the <tt class="docutils literal"><span class="pre">'</span> <span class="pre">abcd'</span></tt> at the tail end of the second sequence directly. Instead only the <tt class="docutils literal"><span class="pre">'abcd'</span></tt> can match, and matches the leftmost <tt class="docutils literal"><span class="pre">'abcd'</span></tt> in the second sequence:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">SequenceMatcher</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span><span class="o">==</span><span class="s">" "</span><span class="p">,</span> <span class="s">" abcd"</span><span class="p">,</span> <span class="s">"abcd abcd"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">find_longest_match</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">9</span><span class="p">)</span> <span class="go">Match(a=1, b=0, size=4)</span> </pre></div> </div> <p>If no blocks match, this returns <tt class="docutils literal"><span class="pre">(alo,</span> <span class="pre">blo,</span> <span class="pre">0)</span></tt>.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>This method returns a <a class="reference internal" href="../glossary.html#term-named-tuple"><em class="xref std std-term">named tuple</em></a> <tt class="docutils literal"><span class="pre">Match(a,</span> <span class="pre">b,</span> <span class="pre">size)</span></tt>.</p> </dd></dl> <dl class="method"> <dt id="difflib.SequenceMatcher.get_matching_blocks"> <tt class="descname">get_matching_blocks</tt><big>(</big><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.get_matching_blocks" title="Permalink to this definition">¶</a></dt> <dd><p>Return list of triples describing matching subsequences. Each triple is of the form <tt class="docutils literal"><span class="pre">(i,</span> <span class="pre">j,</span> <span class="pre">n)</span></tt>, and means that <tt class="docutils literal"><span class="pre">a[i:i+n]</span> <span class="pre">==</span> <span class="pre">b[j:j+n]</span></tt>. The triples are monotonically increasing in <em>i</em> and <em>j</em>.</p> <p>The last triple is a dummy, and has the value <tt class="docutils literal"><span class="pre">(len(a),</span> <span class="pre">len(b),</span> <span class="pre">0)</span></tt>. It is the only triple with <tt class="docutils literal"><span class="pre">n</span> <span class="pre">==</span> <span class="pre">0</span></tt>. If <tt class="docutils literal"><span class="pre">(i,</span> <span class="pre">j,</span> <span class="pre">n)</span></tt> and <tt class="docutils literal"><span class="pre">(i',</span> <span class="pre">j',</span> <span class="pre">n')</span></tt> are adjacent triples in the list, and the second is not the last triple in the list, then <tt class="docutils literal"><span class="pre">i+n</span> <span class="pre">!=</span> <span class="pre">i'</span></tt> or <tt class="docutils literal"><span class="pre">j+n</span> <span class="pre">!=</span> <span class="pre">j'</span></tt>; in other words, adjacent triples always describe non-adjacent equal blocks.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.5: </span>The guarantee that adjacent triples always describe non-adjacent blocks was implemented.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">SequenceMatcher</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="s">"abxcd"</span><span class="p">,</span> <span class="s">"abcd"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">get_matching_blocks</span><span class="p">()</span> <span class="go">[Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)]</span> </pre></div> </div> </dd></dl> <dl class="method"> <dt id="difflib.SequenceMatcher.get_opcodes"> <tt class="descname">get_opcodes</tt><big>(</big><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.get_opcodes" title="Permalink to this definition">¶</a></dt> <dd><p>Return list of 5-tuples describing how to turn <em>a</em> into <em>b</em>. Each tuple is of the form <tt class="docutils literal"><span class="pre">(tag,</span> <span class="pre">i1,</span> <span class="pre">i2,</span> <span class="pre">j1,</span> <span class="pre">j2)</span></tt>. The first tuple has <tt class="docutils literal"><span class="pre">i1</span> <span class="pre">==</span> <span class="pre">j1</span> <span class="pre">==</span> <span class="pre">0</span></tt>, and remaining tuples have <em>i1</em> equal to the <em>i2</em> from the preceding tuple, and, likewise, <em>j1</em> equal to the previous <em>j2</em>.</p> <p>The <em>tag</em> values are strings, with these meanings:</p> <table border="1" class="docutils"> <colgroup> <col width="25%" /> <col width="75%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Value</th> <th class="head">Meaning</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'replace'</span></tt></td> <td><tt class="docutils literal"><span class="pre">a[i1:i2]</span></tt> should be replaced by <tt class="docutils literal"><span class="pre">b[j1:j2]</span></tt>.</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'delete'</span></tt></td> <td><tt class="docutils literal"><span class="pre">a[i1:i2]</span></tt> should be deleted. Note that <tt class="docutils literal"><span class="pre">j1</span> <span class="pre">==</span> <span class="pre">j2</span></tt> in this case.</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">'insert'</span></tt></td> <td><tt class="docutils literal"><span class="pre">b[j1:j2]</span></tt> should be inserted at <tt class="docutils literal"><span class="pre">a[i1:i1]</span></tt>. Note that <tt class="docutils literal"><span class="pre">i1</span> <span class="pre">==</span> <span class="pre">i2</span></tt> in this case.</td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'equal'</span></tt></td> <td><tt class="docutils literal"><span class="pre">a[i1:i2]</span> <span class="pre">==</span> <span class="pre">b[j1:j2]</span></tt> (the sub-sequences are equal).</td> </tr> </tbody> </table> <p>For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="s">"qabxcd"</span> <span class="gp">>>> </span><span class="n">b</span> <span class="o">=</span> <span class="s">"abycdf"</span> <span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">SequenceMatcher</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">tag</span><span class="p">,</span> <span class="n">i1</span><span class="p">,</span> <span class="n">i2</span><span class="p">,</span> <span class="n">j1</span><span class="p">,</span> <span class="n">j2</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">get_opcodes</span><span class="p">():</span> <span class="gp">... </span> <span class="k">print</span> <span class="p">(</span><span class="s">"</span><span class="si">%7s</span><span class="s"> a[</span><span class="si">%d</span><span class="s">:</span><span class="si">%d</span><span class="s">] (</span><span class="si">%s</span><span class="s">) b[</span><span class="si">%d</span><span class="s">:</span><span class="si">%d</span><span class="s">] (</span><span class="si">%s</span><span class="s">)"</span> <span class="o">%</span> <span class="gp">... </span> <span class="p">(</span><span class="n">tag</span><span class="p">,</span> <span class="n">i1</span><span class="p">,</span> <span class="n">i2</span><span class="p">,</span> <span class="n">a</span><span class="p">[</span><span class="n">i1</span><span class="p">:</span><span class="n">i2</span><span class="p">],</span> <span class="n">j1</span><span class="p">,</span> <span class="n">j2</span><span class="p">,</span> <span class="n">b</span><span class="p">[</span><span class="n">j1</span><span class="p">:</span><span class="n">j2</span><span class="p">]))</span> <span class="go"> delete a[0:1] (q) b[0:0] ()</span> <span class="go"> equal a[1:3] (ab) b[0:2] (ab)</span> <span class="go">replace a[3:4] (x) b[2:3] (y)</span> <span class="go"> equal a[4:6] (cd) b[3:5] (cd)</span> <span class="go"> insert a[6:6] () b[5:6] (f)</span> </pre></div> </div> </dd></dl> <dl class="method"> <dt id="difflib.SequenceMatcher.get_grouped_opcodes"> <tt class="descname">get_grouped_opcodes</tt><big>(</big><span class="optional">[</span><em>n</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.get_grouped_opcodes" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="../glossary.html#term-generator"><em class="xref std std-term">generator</em></a> of groups with up to <em>n</em> lines of context.</p> <p>Starting with the groups returned by <a class="reference internal" href="#difflib.SequenceMatcher.get_opcodes" title="difflib.SequenceMatcher.get_opcodes"><tt class="xref py py-meth docutils literal"><span class="pre">get_opcodes()</span></tt></a>, this method splits out smaller change clusters and eliminates intervening ranges which have no changes.</p> <p>The groups are returned in the same format as <a class="reference internal" href="#difflib.SequenceMatcher.get_opcodes" title="difflib.SequenceMatcher.get_opcodes"><tt class="xref py py-meth docutils literal"><span class="pre">get_opcodes()</span></tt></a>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> </dd></dl> <dl class="method"> <dt id="difflib.SequenceMatcher.ratio"> <tt class="descname">ratio</tt><big>(</big><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.ratio" title="Permalink to this definition">¶</a></dt> <dd><p>Return a measure of the sequences’ similarity as a float in the range [0, 1].</p> <p>Where T is the total number of elements in both sequences, and M is the number of matches, this is 2.0*M / T. Note that this is <tt class="docutils literal"><span class="pre">1.0</span></tt> if the sequences are identical, and <tt class="docutils literal"><span class="pre">0.0</span></tt> if they have nothing in common.</p> <p>This is expensive to compute if <a class="reference internal" href="#difflib.SequenceMatcher.get_matching_blocks" title="difflib.SequenceMatcher.get_matching_blocks"><tt class="xref py py-meth docutils literal"><span class="pre">get_matching_blocks()</span></tt></a> or <a class="reference internal" href="#difflib.SequenceMatcher.get_opcodes" title="difflib.SequenceMatcher.get_opcodes"><tt class="xref py py-meth docutils literal"><span class="pre">get_opcodes()</span></tt></a> hasn’t already been called, in which case you may want to try <a class="reference internal" href="#difflib.SequenceMatcher.quick_ratio" title="difflib.SequenceMatcher.quick_ratio"><tt class="xref py py-meth docutils literal"><span class="pre">quick_ratio()</span></tt></a> or <a class="reference internal" href="#difflib.SequenceMatcher.real_quick_ratio" title="difflib.SequenceMatcher.real_quick_ratio"><tt class="xref py py-meth docutils literal"><span class="pre">real_quick_ratio()</span></tt></a> first to get an upper bound.</p> </dd></dl> <dl class="method"> <dt id="difflib.SequenceMatcher.quick_ratio"> <tt class="descname">quick_ratio</tt><big>(</big><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.quick_ratio" title="Permalink to this definition">¶</a></dt> <dd><p>Return an upper bound on <a class="reference internal" href="#difflib.SequenceMatcher.ratio" title="difflib.SequenceMatcher.ratio"><tt class="xref py py-meth docutils literal"><span class="pre">ratio()</span></tt></a> relatively quickly.</p> </dd></dl> <dl class="method"> <dt id="difflib.SequenceMatcher.real_quick_ratio"> <tt class="descname">real_quick_ratio</tt><big>(</big><big>)</big><a class="headerlink" href="#difflib.SequenceMatcher.real_quick_ratio" title="Permalink to this definition">¶</a></dt> <dd><p>Return an upper bound on <a class="reference internal" href="#difflib.SequenceMatcher.ratio" title="difflib.SequenceMatcher.ratio"><tt class="xref py py-meth docutils literal"><span class="pre">ratio()</span></tt></a> very quickly.</p> </dd></dl> </dd></dl> <p>The three methods that return the ratio of matching to total characters can give different results due to differing levels of approximation, although <tt class="xref py py-meth docutils literal"><span class="pre">quick_ratio()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">real_quick_ratio()</span></tt> are always at least as large as <tt class="xref py py-meth docutils literal"><span class="pre">ratio()</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">SequenceMatcher</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="s">"abcd"</span><span class="p">,</span> <span class="s">"bcde"</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">ratio</span><span class="p">()</span> <span class="go">0.75</span> <span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">quick_ratio</span><span class="p">()</span> <span class="go">0.75</span> <span class="gp">>>> </span><span class="n">s</span><span class="o">.</span><span class="n">real_quick_ratio</span><span class="p">()</span> <span class="go">1.0</span> </pre></div> </div> </div> <div class="section" id="sequencematcher-examples"> <span id="id1"></span><h2>7.4.2. SequenceMatcher Examples<a class="headerlink" href="#sequencematcher-examples" title="Permalink to this headline">¶</a></h2> <p>This example compares two strings, considering blanks to be “junk:”</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="n">SequenceMatcher</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">==</span> <span class="s">" "</span><span class="p">,</span> <span class="gp">... </span> <span class="s">"private Thread currentThread;"</span><span class="p">,</span> <span class="gp">... </span> <span class="s">"private volatile Thread currentThread;"</span><span class="p">)</span> </pre></div> </div> <p><tt class="xref py py-meth docutils literal"><span class="pre">ratio()</span></tt> returns a float in [0, 1], measuring the similarity of the sequences. As a rule of thumb, a <tt class="xref py py-meth docutils literal"><span class="pre">ratio()</span></tt> value over 0.6 means the sequences are close matches:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span> <span class="nb">round</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">ratio</span><span class="p">(),</span> <span class="mi">3</span><span class="p">)</span> <span class="go">0.866</span> </pre></div> </div> <p>If you’re only interested in where the sequences match, <tt class="xref py py-meth docutils literal"><span class="pre">get_matching_blocks()</span></tt> is handy:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">for</span> <span class="n">block</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">get_matching_blocks</span><span class="p">():</span> <span class="gp">... </span> <span class="k">print</span> <span class="s">"a[</span><span class="si">%d</span><span class="s">] and b[</span><span class="si">%d</span><span class="s">] match for </span><span class="si">%d</span><span class="s"> elements"</span> <span class="o">%</span> <span class="n">block</span> <span class="go">a[0] and b[0] match for 8 elements</span> <span class="go">a[8] and b[17] match for 21 elements</span> <span class="go">a[29] and b[38] match for 0 elements</span> </pre></div> </div> <p>Note that the last tuple returned by <tt class="xref py py-meth docutils literal"><span class="pre">get_matching_blocks()</span></tt> is always a dummy, <tt class="docutils literal"><span class="pre">(len(a),</span> <span class="pre">len(b),</span> <span class="pre">0)</span></tt>, and this is the only case in which the last tuple element (number of elements matched) is <tt class="docutils literal"><span class="pre">0</span></tt>.</p> <p>If you want to know how to change the first sequence into the second, use <tt class="xref py py-meth docutils literal"><span class="pre">get_opcodes()</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">for</span> <span class="n">opcode</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">get_opcodes</span><span class="p">():</span> <span class="gp">... </span> <span class="k">print</span> <span class="s">"</span><span class="si">%6s</span><span class="s"> a[</span><span class="si">%d</span><span class="s">:</span><span class="si">%d</span><span class="s">] b[</span><span class="si">%d</span><span class="s">:</span><span class="si">%d</span><span class="s">]"</span> <span class="o">%</span> <span class="n">opcode</span> <span class="go"> equal a[0:8] b[0:8]</span> <span class="go">insert a[8:8] b[8:17]</span> <span class="go"> equal a[8:29] b[17:38]</span> </pre></div> </div> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <ul class="last simple"> <li>The <a class="reference internal" href="#difflib.get_close_matches" title="difflib.get_close_matches"><tt class="xref py py-func docutils literal"><span class="pre">get_close_matches()</span></tt></a> function in this module which shows how simple code building on <a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a> can be used to do useful work.</li> <li><a class="reference external" href="http://code.activestate.com/recipes/576729/">Simple version control recipe</a> for a small application built with <a class="reference internal" href="#difflib.SequenceMatcher" title="difflib.SequenceMatcher"><tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt></a>.</li> </ul> </div> </div> <div class="section" id="differ-objects"> <span id="id2"></span><h2>7.4.3. Differ Objects<a class="headerlink" href="#differ-objects" title="Permalink to this headline">¶</a></h2> <p>Note that <a class="reference internal" href="#difflib.Differ" title="difflib.Differ"><tt class="xref py py-class docutils literal"><span class="pre">Differ</span></tt></a>-generated deltas make no claim to be <strong>minimal</strong> diffs. To the contrary, minimal diffs are often counter-intuitive, because they synch up anywhere possible, sometimes accidental matches 100 pages apart. Restricting synch points to contiguous matches preserves some notion of locality, at the occasional cost of producing a longer diff.</p> <p>The <a class="reference internal" href="#difflib.Differ" title="difflib.Differ"><tt class="xref py py-class docutils literal"><span class="pre">Differ</span></tt></a> class has this constructor:</p> <dl class="class"> <dt> <em class="property">class </em><tt class="descclassname">difflib.</tt><tt class="descname">Differ</tt><big>(</big><span class="optional">[</span><em>linejunk</em><span class="optional">[</span>, <em>charjunk</em><span class="optional">]</span><span class="optional">]</span><big>)</big></dt> <dd><p>Optional keyword parameters <em>linejunk</em> and <em>charjunk</em> are for filter functions (or <tt class="docutils literal"><span class="pre">None</span></tt>):</p> <p><em>linejunk</em>: A function that accepts a single string argument, and returns true if the string is junk. The default is <tt class="docutils literal"><span class="pre">None</span></tt>, meaning that no line is considered junk.</p> <p><em>charjunk</em>: A function that accepts a single character argument (a string of length 1), and returns true if the character is junk. The default is <tt class="docutils literal"><span class="pre">None</span></tt>, meaning that no character is considered junk.</p> <p><a class="reference internal" href="#difflib.Differ" title="difflib.Differ"><tt class="xref py py-class docutils literal"><span class="pre">Differ</span></tt></a> objects are used (deltas generated) via a single method:</p> <dl class="method"> <dt id="difflib.Differ.compare"> <tt class="descname">compare</tt><big>(</big><em>a</em>, <em>b</em><big>)</big><a class="headerlink" href="#difflib.Differ.compare" title="Permalink to this definition">¶</a></dt> <dd><p>Compare two sequences of lines, and generate the delta (a sequence of lines).</p> <p>Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the <tt class="xref py py-meth docutils literal"><span class="pre">readlines()</span></tt> method of file-like objects. The delta generated also consists of newline-terminated strings, ready to be printed as-is via the <tt class="xref py py-meth docutils literal"><span class="pre">writelines()</span></tt> method of a file-like object.</p> </dd></dl> </dd></dl> </div> <div class="section" id="differ-example"> <span id="differ-examples"></span><h2>7.4.4. Differ Example<a class="headerlink" href="#differ-example" title="Permalink to this headline">¶</a></h2> <p>This example compares two texts. First we set up the texts, sequences of individual single-line strings ending with newlines (such sequences can also be obtained from the <tt class="xref py py-meth docutils literal"><span class="pre">readlines()</span></tt> method of file-like objects):</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">text1</span> <span class="o">=</span> <span class="s">''' 1. Beautiful is better than ugly.</span> <span class="gp">... </span><span class="s"> 2. Explicit is better than implicit.</span> <span class="gp">... </span><span class="s"> 3. Simple is better than complex.</span> <span class="gp">... </span><span class="s"> 4. Complex is better than complicated.</span> <span class="gp">... </span><span class="s">'''</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">text1</span><span class="p">)</span> <span class="go">4</span> <span class="gp">>>> </span><span class="n">text1</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="go">'\n'</span> <span class="gp">>>> </span><span class="n">text2</span> <span class="o">=</span> <span class="s">''' 1. Beautiful is better than ugly.</span> <span class="gp">... </span><span class="s"> 3. Simple is better than complex.</span> <span class="gp">... </span><span class="s"> 4. Complicated is better than complex.</span> <span class="gp">... </span><span class="s"> 5. Flat is better than nested.</span> <span class="gp">... </span><span class="s">'''</span><span class="o">.</span><span class="n">splitlines</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> </pre></div> </div> <p>Next we instantiate a Differ object:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">d</span> <span class="o">=</span> <span class="n">Differ</span><span class="p">()</span> </pre></div> </div> <p>Note that when instantiating a <a class="reference internal" href="#difflib.Differ" title="difflib.Differ"><tt class="xref py py-class docutils literal"><span class="pre">Differ</span></tt></a> object we may pass functions to filter out line and character “junk.” See the <a class="reference internal" href="#difflib.Differ" title="difflib.Differ"><tt class="xref py py-meth docutils literal"><span class="pre">Differ()</span></tt></a> constructor for details.</p> <p>Finally, we compare the two:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">result</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">compare</span><span class="p">(</span><span class="n">text1</span><span class="p">,</span> <span class="n">text2</span><span class="p">))</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">result</span></tt> is a list of strings, so let’s pretty-print it:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pprint</span> <span class="kn">import</span> <span class="n">pprint</span> <span class="gp">>>> </span><span class="n">pprint</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="go">[' 1. Beautiful is better than ugly.\n',</span> <span class="go"> '- 2. Explicit is better than implicit.\n',</span> <span class="go"> '- 3. Simple is better than complex.\n',</span> <span class="go"> '+ 3. Simple is better than complex.\n',</span> <span class="go"> '? ++\n',</span> <span class="go"> '- 4. Complex is better than complicated.\n',</span> <span class="go"> '? ^ ---- ^\n',</span> <span class="go"> '+ 4. Complicated is better than complex.\n',</span> <span class="go"> '? ++++ ^ ^\n',</span> <span class="go"> '+ 5. Flat is better than nested.\n']</span> </pre></div> </div> <p>As a single multi-line string it looks like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">sys</span> <span class="gp">>>> </span><span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">writelines</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="go"> 1. Beautiful is better than ugly.</span> <span class="go">- 2. Explicit is better than implicit.</span> <span class="go">- 3. Simple is better than complex.</span> <span class="go">+ 3. Simple is better than complex.</span> <span class="go">? ++</span> <span class="go">- 4. Complex is better than complicated.</span> <span class="go">? ^ ---- ^</span> <span class="go">+ 4. Complicated is better than complex.</span> <span class="go">? ++++ ^ ^</span> <span class="go">+ 5. Flat is better than nested.</span> </pre></div> </div> </div> <div class="section" id="a-command-line-interface-to-difflib"> <span id="difflib-interface"></span><h2>7.4.5. A command-line interface to difflib<a class="headerlink" href="#a-command-line-interface-to-difflib" title="Permalink to this headline">¶</a></h2> <p>This example shows how to use difflib to create a <tt class="docutils literal"><span class="pre">diff</span></tt>-like utility. It is also contained in the Python source distribution, as <tt class="file docutils literal"><span class="pre">Tools/scripts/diff.py</span></tt>.</p> <div class="highlight-python"><div class="highlight"><pre><span class="sd">""" Command line interface to difflib.py providing diffs in four formats:</span> <span class="sd">* ndiff: lists every line and highlights interline changes.</span> <span class="sd">* context: highlights clusters of changes in a before/after format.</span> <span class="sd">* unified: highlights clusters of changes in an inline format.</span> <span class="sd">* html: generates side by side comparison with change highlights.</span> <span class="sd">"""</span> <span class="kn">import</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">time</span><span class="o">,</span> <span class="nn">difflib</span><span class="o">,</span> <span class="nn">optparse</span> <span class="k">def</span> <span class="nf">main</span><span class="p">():</span> <span class="c"># Configure the option parser</span> <span class="n">usage</span> <span class="o">=</span> <span class="s">"usage: %prog [options] fromfile tofile"</span> <span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">(</span><span class="n">usage</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-c"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'Produce a context format diff (default)'</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-u"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'Produce a unified format diff'</span><span class="p">)</span> <span class="n">hlp</span> <span class="o">=</span> <span class="s">'Produce HTML side by side diff (can use -c and -l in conjunction)'</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-m"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="n">hlp</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-n"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store_true"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'Produce a ndiff format diff'</span><span class="p">)</span> <span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">"-l"</span><span class="p">,</span> <span class="s">"--lines"</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">"int"</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">'Set number of context lines (default 3)'</span><span class="p">)</span> <span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="n">parser</span><span class="o">.</span><span class="n">print_help</span><span class="p">()</span> <span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span> <span class="n">parser</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s">"need to specify both a fromfile and tofile"</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">lines</span> <span class="n">fromfile</span><span class="p">,</span> <span class="n">tofile</span> <span class="o">=</span> <span class="n">args</span> <span class="c"># as specified in the usage string</span> <span class="c"># we're passing these as arguments to the diff function</span> <span class="n">fromdate</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">ctime</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">fromfile</span><span class="p">)</span><span class="o">.</span><span class="n">st_mtime</span><span class="p">)</span> <span class="n">todate</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">ctime</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">tofile</span><span class="p">)</span><span class="o">.</span><span class="n">st_mtime</span><span class="p">)</span> <span class="n">fromlines</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">fromfile</span><span class="p">,</span> <span class="s">'U'</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span> <span class="n">tolines</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">tofile</span><span class="p">,</span> <span class="s">'U'</span><span class="p">)</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span> <span class="k">if</span> <span class="n">options</span><span class="o">.</span><span class="n">u</span><span class="p">:</span> <span class="n">diff</span> <span class="o">=</span> <span class="n">difflib</span><span class="o">.</span><span class="n">unified_diff</span><span class="p">(</span><span class="n">fromlines</span><span class="p">,</span> <span class="n">tolines</span><span class="p">,</span> <span class="n">fromfile</span><span class="p">,</span> <span class="n">tofile</span><span class="p">,</span> <span class="n">fromdate</span><span class="p">,</span> <span class="n">todate</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> <span class="k">elif</span> <span class="n">options</span><span class="o">.</span><span class="n">n</span><span class="p">:</span> <span class="n">diff</span> <span class="o">=</span> <span class="n">difflib</span><span class="o">.</span><span class="n">ndiff</span><span class="p">(</span><span class="n">fromlines</span><span class="p">,</span> <span class="n">tolines</span><span class="p">)</span> <span class="k">elif</span> <span class="n">options</span><span class="o">.</span><span class="n">m</span><span class="p">:</span> <span class="n">diff</span> <span class="o">=</span> <span class="n">difflib</span><span class="o">.</span><span class="n">HtmlDiff</span><span class="p">()</span><span class="o">.</span><span class="n">make_file</span><span class="p">(</span><span class="n">fromlines</span><span class="p">,</span> <span class="n">tolines</span><span class="p">,</span> <span class="n">fromfile</span><span class="p">,</span> <span class="n">tofile</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="n">options</span><span class="o">.</span><span class="n">c</span><span class="p">,</span> <span class="n">numlines</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="n">diff</span> <span class="o">=</span> <span class="n">difflib</span><span class="o">.</span><span class="n">context_diff</span><span class="p">(</span><span class="n">fromlines</span><span class="p">,</span> <span class="n">tolines</span><span class="p">,</span> <span class="n">fromfile</span><span class="p">,</span> <span class="n">tofile</span><span class="p">,</span> <span class="n">fromdate</span><span class="p">,</span> <span class="n">todate</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="n">n</span><span class="p">)</span> <span class="c"># we're using writelines because diff is a generator</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">writelines</span><span class="p">(</span><span class="n">diff</span><span class="p">)</span> <span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">'__main__'</span><span class="p">:</span> <span class="n">main</span><span class="p">()</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">7.4. <tt class="docutils literal"><span class="pre">difflib</span></tt> — Helpers for computing deltas</a><ul> <li><a class="reference internal" href="#sequencematcher-objects">7.4.1. SequenceMatcher Objects</a></li> <li><a class="reference internal" href="#sequencematcher-examples">7.4.2. SequenceMatcher Examples</a></li> <li><a class="reference internal" href="#differ-objects">7.4.3. Differ Objects</a></li> <li><a class="reference internal" href="#differ-example">7.4.4. Differ Example</a></li> <li><a class="reference internal" href="#a-command-line-interface-to-difflib">7.4.5. A command-line interface to difflib</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="struct.html" title="previous chapter">7.3. <tt class="docutils literal"><span class="pre">struct</span></tt> — Interpret strings as packed binary data</a></p> <h4>Next topic</h4> <p class="topless"><a href="stringio.html" title="next chapter">7.5. <tt class="docutils literal"><span class="pre">StringIO</span></tt> — Read and write strings as files</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/difflib.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="stringio.html" title="7.5. StringIO — Read and write strings as files" >next</a> |</li> <li class="right" > <a href="struct.html" title="7.3. struct — Interpret strings as packed binary data" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="strings.html" >7. String Services</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close