/
usr
/
share
/
doc
/
python2-docs
/
html
/
_sources
/
library
/
/usr/share/doc/python2-docs/html/_sources/library
mkdir
upload
Name
Size
Mode
Actions
2to3.rst.txt
14647
0644
edit
dl
rm
abc.rst.txt
7219
0644
edit
dl
rm
aepack.rst.txt
4257
0644
edit
dl
rm
aetools.rst.txt
3532
0644
edit
dl
rm
aetypes.rst.txt
4257
0644
edit
dl
rm
aifc.rst.txt
7086
0644
edit
dl
rm
al.rst.txt
5309
0644
edit
dl
rm
allos.rst.txt
695
0644
edit
dl
rm
anydbm.rst.txt
4111
0644
edit
dl
rm
archiving.rst.txt
424
0644
edit
dl
rm
argparse.rst.txt
74685
0644
edit
dl
rm
array.rst.txt
10524
0644
edit
dl
rm
ast.rst.txt
10111
0644
edit
dl
rm
asynchat.rst.txt
9206
0644
edit
dl
rm
asyncore.rst.txt
12937
0644
edit
dl
rm
atexit.rst.txt
3910
0644
edit
dl
rm
audioop.rst.txt
10395
0644
edit
dl
rm
autogil.rst.txt
1015
0644
edit
dl
rm
base64.rst.txt
6252
0644
edit
dl
rm
basehttpserver.rst.txt
10404
0644
edit
dl
rm
bastion.rst.txt
2611
0644
edit
dl
rm
bdb.rst.txt
12456
0644
edit
dl
rm
binascii.rst.txt
6508
0644
edit
dl
rm
binhex.rst.txt
1910
0644
edit
dl
rm
bisect.rst.txt
5415
0644
edit
dl
rm
bsddb.rst.txt
7575
0644
edit
dl
rm
bz2.rst.txt
8064
0644
edit
dl
rm
calendar.rst.txt
11282
0644
edit
dl
rm
carbon.rst.txt
15956
0644
edit
dl
rm
cd.rst.txt
11974
0644
edit
dl
rm
cgi.rst.txt
22853
0644
edit
dl
rm
cgihttpserver.rst.txt
2788
0644
edit
dl
rm
cgitb.rst.txt
2874
0644
edit
dl
rm
chunk.rst.txt
4955
0644
edit
dl
rm
cmath.rst.txt
7647
0644
edit
dl
rm
cmd.rst.txt
8565
0644
edit
dl
rm
code.rst.txt
7115
0644
edit
dl
rm
codecs.rst.txt
66966
0644
edit
dl
rm
codeop.rst.txt
3774
0644
edit
dl
rm
collections.rst.txt
41448
0644
edit
dl
rm
colorpicker.rst.txt
913
0644
edit
dl
rm
colorsys.rst.txt
1819
0644
edit
dl
rm
commands.rst.txt
2595
0644
edit
dl
rm
compileall.rst.txt
4678
0644
edit
dl
rm
compiler.rst.txt
37464
0644
edit
dl
rm
configparser.rst.txt
19607
0644
edit
dl
rm
constants.rst.txt
2328
0644
edit
dl
rm
contextlib.rst.txt
6012
0644
edit
dl
rm
cookie.rst.txt
9543
0644
edit
dl
rm
cookielib.rst.txt
27875
0644
edit
dl
rm
copy.rst.txt
3351
0644
edit
dl
rm
copy_reg.rst.txt
2329
0644
edit
dl
rm
crypt.rst.txt
2292
0644
edit
dl
rm
crypto.rst.txt
355
0644
edit
dl
rm
csv.rst.txt
22757
0644
edit
dl
rm
ctypes.rst.txt
90513
0644
edit
dl
rm
curses.ascii.rst.txt
9046
0644
edit
dl
rm
curses.panel.rst.txt
2740
0644
edit
dl
rm
curses.rst.txt
74877
0644
edit
dl
rm
custominterp.rst.txt
570
0644
edit
dl
rm
datatypes.rst.txt
864
0644
edit
dl
rm
datetime.rst.txt
74679
0644
edit
dl
rm
dbhash.rst.txt
3865
0644
edit
dl
rm
dbm.rst.txt
3117
0644
edit
dl
rm
debug.rst.txt
446
0644
edit
dl
rm
decimal.rst.txt
70926
0644
edit
dl
rm
development.rst.txt
640
0644
edit
dl
rm
difflib.rst.txt
30716
0644
edit
dl
rm
dircache.rst.txt
1813
0644
edit
dl
rm
dis.rst.txt
23248
0644
edit
dl
rm
distribution.rst.txt
426
0644
edit
dl
rm
distutils.rst.txt
1958
0644
edit
dl
rm
dl.rst.txt
3392
0644
edit
dl
rm
doctest.rst.txt
73985
0644
edit
dl
rm
docxmlrpcserver.rst.txt
3802
0644
edit
dl
rm
dumbdbm.rst.txt
2841
0644
edit
dl
rm
dummy_thread.rst.txt
1058
0644
edit
dl
rm
dummy_threading.rst.txt
799
0644
edit
dl
rm
easydialogs.rst.txt
10346
0644
edit
dl
rm
email-examples.rst.txt
1271
0644
edit
dl
rm
email.charset.rst.txt
9654
0644
edit
dl
rm
email.encoders.rst.txt
2376
0644
edit
dl
rm
email.errors.rst.txt
4006
0644
edit
dl
rm
email.generator.rst.txt
6132
0644
edit
dl
rm
email.header.rst.txt
7531
0644
edit
dl
rm
email.iterators.rst.txt
2421
0644
edit
dl
rm
email.message.rst.txt
25217
0644
edit
dl
rm
email.mime.rst.txt
9917
0644
edit
dl
rm
email.parser.rst.txt
10327
0644
edit
dl
rm
email.rst.txt
16111
0644
edit
dl
rm
email.utils.rst.txt
6475
0644
edit
dl
rm
ensurepip.rst.txt
5022
0644
edit
dl
rm
errno.rst.txt
6708
0644
edit
dl
rm
exceptions.rst.txt
18343
0644
edit
dl
rm
fcntl.rst.txt
7361
0644
edit
dl
rm
filecmp.rst.txt
5348
0644
edit
dl
rm
fileformats.rst.txt
302
0644
edit
dl
rm
fileinput.rst.txt
7417
0644
edit
dl
rm
filesys.rst.txt
806
0644
edit
dl
rm
fl.rst.txt
17646
0644
edit
dl
rm
fm.rst.txt
2699
0644
edit
dl
rm
fnmatch.rst.txt
3104
0644
edit
dl
rm
formatter.rst.txt
13245
0644
edit
dl
rm
fpectl.rst.txt
4171
0644
edit
dl
rm
fpformat.rst.txt
1747
0644
edit
dl
rm
fractions.rst.txt
5297
0644
edit
dl
rm
framework.rst.txt
11444
0644
edit
dl
rm
frameworks.rst.txt
378
0644
edit
dl
rm
ftplib.rst.txt
15720
0644
edit
dl
rm
functions.rst.txt
75554
0644
edit
dl
rm
functools.rst.txt
7448
0644
edit
dl
rm
future_builtins.rst.txt
2010
0644
edit
dl
rm
gc.rst.txt
9014
0644
edit
dl
rm
gdbm.rst.txt
4882
0644
edit
dl
rm
gensuitemodule.rst.txt
3113
0644
edit
dl
rm
getopt.rst.txt
6670
0644
edit
dl
rm
getpass.rst.txt
1876
0644
edit
dl
rm
gettext.rst.txt
29040
0644
edit
dl
rm
gl.rst.txt
6009
0644
edit
dl
rm
glob.rst.txt
2414
0644
edit
dl
rm
grp.rst.txt
2256
0644
edit
dl
rm
gzip.rst.txt
4828
0644
edit
dl
rm
hashlib.rst.txt
7379
0644
edit
dl
rm
heapq.rst.txt
13185
0644
edit
dl
rm
hmac.rst.txt
3073
0644
edit
dl
rm
hotshot.rst.txt
4289
0644
edit
dl
rm
htmllib.rst.txt
7379
0644
edit
dl
rm
htmlparser.rst.txt
11640
0644
edit
dl
rm
httplib.rst.txt
37454
0644
edit
dl
rm
i18n.rst.txt
409
0644
edit
dl
rm
ic.rst.txt
5005
0644
edit
dl
rm
idle.rst.txt
22182
0644
edit
dl
rm
imageop.rst.txt
4001
0644
edit
dl
rm
imaplib.rst.txt
17221
0644
edit
dl
rm
imgfile.rst.txt
2765
0644
edit
dl
rm
imghdr.rst.txt
2635
0644
edit
dl
rm
imp.rst.txt
12593
0644
edit
dl
rm
importlib.rst.txt
1126
0644
edit
dl
rm
imputil.rst.txt
7023
0644
edit
dl
rm
index.rst.txt
2287
0644
edit
dl
rm
inspect.rst.txt
28151
0644
edit
dl
rm
internet.rst.txt
950
0644
edit
dl
rm
intro.rst.txt
2803
0644
edit
dl
rm
io.rst.txt
39015
0644
edit
dl
rm
ipc.rst.txt
630
0644
edit
dl
rm
itertools.rst.txt
36324
0644
edit
dl
rm
jpeg.rst.txt
3858
0644
edit
dl
rm
json.rst.txt
25545
0644
edit
dl
rm
keyword.rst.txt
617
0644
edit
dl
rm
language.rst.txt
523
0644
edit
dl
rm
linecache.rst.txt
1887
0644
edit
dl
rm
locale.rst.txt
24975
0644
edit
dl
rm
logging.config.rst.txt
31560
0644
edit
dl
rm
logging.handlers.rst.txt
28153
0644
edit
dl
rm
logging.rst.txt
46882
0644
edit
dl
rm
mac.rst.txt
791
0644
edit
dl
rm
macos.rst.txt
3824
0644
edit
dl
rm
macosa.rst.txt
3964
0644
edit
dl
rm
macostools.rst.txt
4017
0644
edit
dl
rm
macpath.rst.txt
650
0644
edit
dl
rm
mailbox.rst.txt
68129
0644
edit
dl
rm
mailcap.rst.txt
3675
0644
edit
dl
rm
markup.rst.txt
1249
0644
edit
dl
rm
marshal.rst.txt
5689
0644
edit
dl
rm
math.rst.txt
10920
0644
edit
dl
rm
md5.rst.txt
2815
0644
edit
dl
rm
mhlib.rst.txt
3966
0644
edit
dl
rm
mimetools.rst.txt
4504
0644
edit
dl
rm
mimetypes.rst.txt
9839
0644
edit
dl
rm
mimewriter.rst.txt
3360
0644
edit
dl
rm
mimify.rst.txt
3519
0644
edit
dl
rm
miniaeframe.rst.txt
2564
0644
edit
dl
rm
misc.rst.txt
248
0644
edit
dl
rm
mm.rst.txt
447
0644
edit
dl
rm
mmap.rst.txt
10481
0644
edit
dl
rm
modulefinder.rst.txt
3380
0644
edit
dl
rm
modules.rst.txt
382
0644
edit
dl
rm
msilib.rst.txt
19062
0644
edit
dl
rm
msvcrt.rst.txt
4346
0644
edit
dl
rm
multifile.rst.txt
6613
0644
edit
dl
rm
multiprocessing.rst.txt
92621
0644
edit
dl
rm
mutex.rst.txt
1939
0644
edit
dl
rm
netdata.rst.txt
432
0644
edit
dl
rm
netrc.rst.txt
3119
0644
edit
dl
rm
new.rst.txt
2653
0644
edit
dl
rm
nis.rst.txt
2108
0644
edit
dl
rm
nntplib.rst.txt
14531
0644
edit
dl
rm
numbers.rst.txt
8007
0644
edit
dl
rm
numeric.rst.txt
751
0644
edit
dl
rm
operator.rst.txt
22092
0644
edit
dl
rm
optparse.rst.txt
77101
0644
edit
dl
rm
os.path.rst.txt
13094
0644
edit
dl
rm
os.rst.txt
82580
0644
edit
dl
rm
ossaudiodev.rst.txt
17309
0644
edit
dl
rm
othergui.rst.txt
2721
0644
edit
dl
rm
parser.rst.txt
15385
0644
edit
dl
rm
pdb.rst.txt
16043
0644
edit
dl
rm
persistence.rst.txt
826
0644
edit
dl
rm
pickle.rst.txt
37262
0644
edit
dl
rm
pickletools.rst.txt
1997
0644
edit
dl
rm
pipes.rst.txt
3786
0644
edit
dl
rm
pkgutil.rst.txt
7714
0644
edit
dl
rm
platform.rst.txt
9554
0644
edit
dl
rm
plistlib.rst.txt
4134
0644
edit
dl
rm
popen2.rst.txt
7022
0644
edit
dl
rm
poplib.rst.txt
6220
0644
edit
dl
rm
posix.rst.txt
3630
0644
edit
dl
rm
posixfile.rst.txt
7200
0644
edit
dl
rm
pprint.rst.txt
9071
0644
edit
dl
rm
profile.rst.txt
28666
0644
edit
dl
rm
pty.rst.txt
1762
0644
edit
dl
rm
pwd.rst.txt
2725
0644
edit
dl
rm
pyclbr.rst.txt
3296
0644
edit
dl
rm
pydoc.rst.txt
4083
0644
edit
dl
rm
pyexpat.rst.txt
28891
0644
edit
dl
rm
python.rst.txt
514
0644
edit
dl
rm
py_compile.rst.txt
2479
0644
edit
dl
rm
queue.rst.txt
7034
0644
edit
dl
rm
quopri.rst.txt
2670
0644
edit
dl
rm
random.rst.txt
13319
0644
edit
dl
rm
re.rst.txt
55102
0644
edit
dl
rm
readline.rst.txt
10513
0644
edit
dl
rm
repr.rst.txt
4718
0644
edit
dl
rm
resource.rst.txt
9595
0644
edit
dl
rm
restricted.rst.txt
3327
0644
edit
dl
rm
rexec.rst.txt
11742
0644
edit
dl
rm
rfc822.rst.txt
14037
0644
edit
dl
rm
rlcompleter.rst.txt
2494
0644
edit
dl
rm
robotparser.rst.txt
2190
0644
edit
dl
rm
runpy.rst.txt
6938
0644
edit
dl
rm
sched.rst.txt
4644
0644
edit
dl
rm
scrolledtext.rst.txt
1379
0644
edit
dl
rm
select.rst.txt
20705
0644
edit
dl
rm
sets.rst.txt
14973
0644
edit
dl
rm
sgi.rst.txt
322
0644
edit
dl
rm
sgmllib.rst.txt
10664
0644
edit
dl
rm
sha.rst.txt
2807
0644
edit
dl
rm
shelve.rst.txt
8319
0644
edit
dl
rm
shlex.rst.txt
11313
0644
edit
dl
rm
shutil.rst.txt
13491
0644
edit
dl
rm
signal.rst.txt
10719
0644
edit
dl
rm
simplehttpserver.rst.txt
4559
0644
edit
dl
rm
simplexmlrpcserver.rst.txt
10881
0644
edit
dl
rm
site.rst.txt
7880
0644
edit
dl
rm
smtpd.rst.txt
2464
0644
edit
dl
rm
smtplib.rst.txt
14906
0644
edit
dl
rm
sndhdr.rst.txt
1759
0644
edit
dl
rm
socket.rst.txt
40765
0644
edit
dl
rm
socketserver.rst.txt
22512
0644
edit
dl
rm
someos.rst.txt
599
0644
edit
dl
rm
spwd.rst.txt
2825
0644
edit
dl
rm
sqlite3.rst.txt
35448
0644
edit
dl
rm
ssl.rst.txt
76554
0644
edit
dl
rm
stat.rst.txt
7778
0644
edit
dl
rm
statvfs.rst.txt
1300
0644
edit
dl
rm
stdtypes.rst.txt
122668
0644
edit
dl
rm
string.rst.txt
44085
0644
edit
dl
rm
stringio.rst.txt
4166
0644
edit
dl
rm
stringprep.rst.txt
4238
0644
edit
dl
rm
strings.rst.txt
746
0644
edit
dl
rm
struct.rst.txt
17100
0644
edit
dl
rm
subprocess.rst.txt
33648
0644
edit
dl
rm
sun.rst.txt
249
0644
edit
dl
rm
sunau.rst.txt
7124
0644
edit
dl
rm
sunaudio.rst.txt
5849
0644
edit
dl
rm
symbol.rst.txt
975
0644
edit
dl
rm
symtable.rst.txt
5064
0644
edit
dl
rm
sys.rst.txt
47520
0644
edit
dl
rm
sysconfig.rst.txt
7639
0644
edit
dl
rm
syslog.rst.txt
3931
0644
edit
dl
rm
tabnanny.rst.txt
1999
0644
edit
dl
rm
tarfile.rst.txt
28214
0644
edit
dl
rm
telnetlib.rst.txt
7482
0644
edit
dl
rm
tempfile.rst.txt
10504
0644
edit
dl
rm
termios.rst.txt
3733
0644
edit
dl
rm
test.rst.txt
17659
0644
edit
dl
rm
textwrap.rst.txt
8552
0644
edit
dl
rm
thread.rst.txt
6588
0644
edit
dl
rm
threading.rst.txt
32468
0644
edit
dl
rm
time.rst.txt
26020
0644
edit
dl
rm
timeit.rst.txt
11598
0644
edit
dl
rm
tix.rst.txt
22693
0644
edit
dl
rm
tk.rst.txt
1612
0644
edit
dl
rm
tkinter.rst.txt
33456
0644
edit
dl
rm
token.rst.txt
2451
0644
edit
dl
rm
tokenize.rst.txt
5570
0644
edit
dl
rm
trace.rst.txt
6724
0644
edit
dl
rm
traceback.rst.txt
10711
0644
edit
dl
rm
ttk.rst.txt
57411
0644
edit
dl
rm
tty.rst.txt
1011
0644
edit
dl
rm
turtle.rst.txt
64097
0644
edit
dl
rm
types.rst.txt
6200
0644
edit
dl
rm
undoc.rst.txt
6550
0644
edit
dl
rm
unicodedata.rst.txt
5736
0644
edit
dl
rm
unittest.rst.txt
82952
0644
edit
dl
rm
unix.rst.txt
490
0644
edit
dl
rm
urllib.rst.txt
24960
0644
edit
dl
rm
urllib2.rst.txt
35211
0644
edit
dl
rm
urlparse.rst.txt
16071
0644
edit
dl
rm
user.rst.txt
2748
0644
edit
dl
rm
userdict.rst.txt
9290
0644
edit
dl
rm
uu.rst.txt
2369
0644
edit
dl
rm
uuid.rst.txt
8378
0644
edit
dl
rm
warnings.rst.txt
19815
0644
edit
dl
rm
wave.rst.txt
5047
0644
edit
dl
rm
weakref.rst.txt
12926
0644
edit
dl
rm
webbrowser.rst.txt
9863
0644
edit
dl
rm
whichdb.rst.txt
931
0644
edit
dl
rm
windows.rst.txt
273
0644
edit
dl
rm
winsound.rst.txt
5066
0644
edit
dl
rm
wsgiref.rst.txt
30567
0644
edit
dl
rm
xdrlib.rst.txt
8077
0644
edit
dl
rm
xml.dom.minidom.rst.txt
11208
0644
edit
dl
rm
xml.dom.pulldom.rst.txt
1571
0644
edit
dl
rm
xml.dom.rst.txt
40182
0644
edit
dl
rm
xml.etree.elementtree.rst.txt
35707
0644
edit
dl
rm
xml.rst.txt
6090
0644
edit
dl
rm
xml.sax.handler.rst.txt
15371
0644
edit
dl
rm
xml.sax.reader.rst.txt
12257
0644
edit
dl
rm
xml.sax.rst.txt
6460
0644
edit
dl
rm
xml.sax.utils.rst.txt
3560
0644
edit
dl
rm
xmlrpclib.rst.txt
22559
0644
edit
dl
rm
zipfile.rst.txt
18871
0644
edit
dl
rm
zipimport.rst.txt
5957
0644
edit
dl
rm
zlib.rst.txt
12766
0644
edit
dl
rm
_winreg.rst.txt
23305
0644
edit
dl
rm
__builtin__.rst.txt
1494
0644
edit
dl
rm
__future__.rst.txt
4952
0644
edit
dl
rm
__main__.rst.txt
535
0644
edit
dl
rm
Edit:
/usr/share/doc/python2-docs/html/_sources/library/difflib.rst.txt
(30716B)
:mod:`difflib` --- Helpers for computing deltas =============================================== .. module:: difflib :synopsis: Helpers for computing differences between objects. .. moduleauthor:: Tim Peters <tim_one@users.sourceforge.net> .. sectionauthor:: Tim Peters <tim_one@users.sourceforge.net> .. Markup by Fred L. Drake, Jr. <fdrake@acm.org> .. testsetup:: import sys from difflib import * .. versionadded:: 2.1 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 :mod:`filecmp` module. .. class:: SequenceMatcher This is a flexible class for comparing pairs of sequences of any type, so long as the sequence elements are :term:`hashable`. The basic algorithm predates, and is a little fancier than, an algorithm published in the late 1980's by Ratcliff and Obershelp under the hyperbolic name "gestalt pattern matching." The idea is to find the longest contiguous matching subsequence that contains no "junk" elements (the Ratcliff and Obershelp algorithm doesn't address junk). The same idea is then applied recursively to the pieces of the sequences to the left and to the right of the matching subsequence. This does not yield minimal edit sequences, but does tend to yield matches that "look right" to people. **Timing:** The basic Ratcliff-Obershelp algorithm is cubic time in the worst case and quadratic time in the expected case. :class:`SequenceMatcher` 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. **Automatic junk heuristic:** :class:`SequenceMatcher` 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 ``autojunk`` argument to ``False`` when creating the :class:`SequenceMatcher`. .. versionadded:: 2.7.1 The *autojunk* parameter. .. class:: Differ This is a class for comparing sequences of lines of text, and producing human-readable differences or deltas. Differ uses :class:`SequenceMatcher` both to compare sequences of lines, and to compare sequences of characters within similar (near-matching) lines. Each line of a :class:`Differ` delta begins with a two-letter code: +----------+-------------------------------------------+ | Code | Meaning | +==========+===========================================+ | ``'- '`` | line unique to sequence 1 | +----------+-------------------------------------------+ | ``'+ '`` | line unique to sequence 2 | +----------+-------------------------------------------+ | ``' '`` | line common to both sequences | +----------+-------------------------------------------+ | ``'? '`` | line not present in either input sequence | +----------+-------------------------------------------+ Lines beginning with '``?``' 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. .. class:: HtmlDiff 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. The constructor for this class is: .. function:: __init__(tabsize=8, wrapcolumn=None, linejunk=None, charjunk=IS_CHARACTER_JUNK) Initializes instance of :class:`HtmlDiff`. *tabsize* is an optional keyword argument to specify tab stop spacing and defaults to ``8``. *wrapcolumn* is an optional keyword to specify column number where lines are broken and wrapped, defaults to ``None`` where lines are not wrapped. *linejunk* and *charjunk* are optional keyword arguments passed into :func:`ndiff` (used by :class:`HtmlDiff` to generate the side by side HTML differences). See :func:`ndiff` documentation for argument default values and descriptions. The following methods are public: .. function:: make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines]) Compares *fromlines* and *tolines* (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. *fromdesc* and *todesc* are optional keyword arguments to specify from/to file column header strings (both default to an empty string). *context* and *numlines* are both optional keyword arguments. Set *context* to ``True`` when contextual differences are to be shown, else the default is ``False`` to show the full files. *numlines* defaults to ``5``. When *context* is ``True`` *numlines* controls the number of context lines which surround the difference highlights. When *context* is ``False`` *numlines* 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). .. function:: make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines]) Compares *fromlines* and *tolines* (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. The arguments for this method are the same as those for the :meth:`make_file` method. :file:`Tools/scripts/diff.py` is a command-line front-end to this class and contains a good example of its use. .. versionadded:: 2.4 .. function:: context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]) Compare *a* and *b* (lists of strings); return a delta (a :term:`generator` generating the delta lines) in context diff format. 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 *n* which defaults to three. By default, the diff control lines (those with ``***`` or ``---``) are created with a trailing newline. This is helpful so that inputs created from :func:`file.readlines` result in diffs that are suitable for use with :func:`file.writelines` since both the inputs and outputs have trailing newlines. For inputs that do not have trailing newlines, set the *lineterm* argument to ``""`` so that the output will be uniformly newline free. The context diff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for *fromfile*, *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks. >>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n'] >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n'] >>> for line in context_diff(s1, s2, fromfile='before.py', tofile='after.py'): ... sys.stdout.write(line) # doctest: +NORMALIZE_WHITESPACE *** before.py --- after.py *************** *** 1,4 **** ! bacon ! eggs ! ham guido --- 1,4 ---- ! python ! eggy ! hamster guido See :ref:`difflib-interface` for a more detailed example. .. versionadded:: 2.3 .. function:: get_close_matches(word, possibilities[, n][, cutoff]) Return a list of the best "good enough" matches. *word* is a sequence for which close matches are desired (typically a string), and *possibilities* is a list of sequences against which to match *word* (typically a list of strings). Optional argument *n* (default ``3``) is the maximum number of close matches to return; *n* must be greater than ``0``. Optional argument *cutoff* (default ``0.6``) is a float in the range [0, 1]. Possibilities that don't score at least that similar to *word* are ignored. The best (no more than *n*) matches among the possibilities are returned in a list, sorted by similarity score, most similar first. >>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) ['apple', 'ape'] >>> import keyword >>> get_close_matches('wheel', keyword.kwlist) ['while'] >>> get_close_matches('apple', keyword.kwlist) [] >>> get_close_matches('accept', keyword.kwlist) ['except'] .. function:: ndiff(a, b[, linejunk][, charjunk]) Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style delta (a :term:`generator` generating the delta lines). Optional keyword parameters *linejunk* and *charjunk* are for filter functions (or ``None``): *linejunk*: A function that accepts a single string argument, and returns true if the string is junk, or false if not. The default is (``None``), starting with Python 2.3. Before then, the default was the module-level function :func:`IS_LINE_JUNK`, which filters out lines without visible characters, except for at most one pound character (``'#'``). As of Python 2.3, the underlying :class:`SequenceMatcher` 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. *charjunk*: 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 :func:`IS_CHARACTER_JUNK`, which filters out whitespace characters (a blank or tab; note: bad idea to include newline in this!). :file:`Tools/scripts/ndiff.py` is a command-line front-end to this function. >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), ... 'ore\ntree\nemu\n'.splitlines(1)) >>> print ''.join(diff), - one ? ^ + ore ? ^ - two - three ? - + tree + emu .. function:: restore(sequence, which) Return one of the two sequences that generated a delta. Given a *sequence* produced by :meth:`Differ.compare` or :func:`ndiff`, extract lines originating from file 1 or 2 (parameter *which*), stripping off line prefixes. Example: >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1), ... 'ore\ntree\nemu\n'.splitlines(1)) >>> diff = list(diff) # materialize the generated delta into a list >>> print ''.join(restore(diff, 1)), one two three >>> print ''.join(restore(diff, 2)), ore tree emu .. function:: unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm]) Compare *a* and *b* (lists of strings); return a delta (a :term:`generator` generating the delta lines) in unified diff format. 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 an inline style (instead of separate before/after blocks). The number of context lines is set by *n* which defaults to three. By default, the diff control lines (those with ``---``, ``+++``, or ``@@``) are created with a trailing newline. This is helpful so that inputs created from :func:`file.readlines` result in diffs that are suitable for use with :func:`file.writelines` since both the inputs and outputs have trailing newlines. For inputs that do not have trailing newlines, set the *lineterm* argument to ``""`` so that the output will be uniformly newline free. The context diff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for *fromfile*, *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks. >>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n'] >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n'] >>> for line in unified_diff(s1, s2, fromfile='before.py', tofile='after.py'): ... sys.stdout.write(line) # doctest: +NORMALIZE_WHITESPACE --- before.py +++ after.py @@ -1,4 +1,4 @@ -bacon -eggs -ham +python +eggy +hamster guido See :ref:`difflib-interface` for a more detailed example. .. versionadded:: 2.3 .. function:: IS_LINE_JUNK(line) Return true for ignorable lines. The line *line* is ignorable if *line* is blank or contains a single ``'#'``, otherwise it is not ignorable. Used as a default for parameter *linejunk* in :func:`ndiff` before Python 2.3. .. function:: IS_CHARACTER_JUNK(ch) Return true for ignorable characters. The character *ch* is ignorable if *ch* is a space or tab, otherwise it is not ignorable. Used as a default for parameter *charjunk* in :func:`ndiff`. .. seealso:: `Pattern Matching: The Gestalt Approach <http://www.drdobbs.com/database/pattern-matching-the-gestalt-approach/184407970>`_ Discussion of a similar algorithm by John W. Ratcliff and D. E. Metzener. This was published in `Dr. Dobb's Journal <http://www.drdobbs.com/>`_ in July, 1988. .. _sequence-matcher: SequenceMatcher Objects ----------------------- The :class:`SequenceMatcher` class has this constructor: .. class:: SequenceMatcher(isjunk=None, a='', b='', autojunk=True) Optional argument *isjunk* must be ``None`` (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 ``None`` for *isjunk* is equivalent to passing ``lambda x: 0``; in other words, no elements are ignored. For example, pass:: lambda x: x in " \t" if you're comparing lines as sequences of characters, and don't want to synch up on blanks or hard tabs. The optional arguments *a* and *b* are sequences to be compared; both default to empty strings. The elements of both sequences must be :term:`hashable`. The optional argument *autojunk* can be used to disable the automatic junk heuristic. .. versionadded:: 2.7.1 The *autojunk* parameter. :class:`SequenceMatcher` objects have the following methods: .. method:: set_seqs(a, b) Set the two sequences to be compared. :class:`SequenceMatcher` computes and caches detailed information about the second sequence, so if you want to compare one sequence against many sequences, use :meth:`set_seq2` to set the commonly used sequence once and call :meth:`set_seq1` repeatedly, once for each of the other sequences. .. method:: set_seq1(a) Set the first sequence to be compared. The second sequence to be compared is not changed. .. method:: set_seq2(b) Set the second sequence to be compared. The first sequence to be compared is not changed. .. method:: find_longest_match(alo, ahi, blo, bhi) Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``. If *isjunk* was omitted or ``None``, :meth:`find_longest_match` returns ``(i, j, k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo <= i <= i+k <= ahi`` and ``blo <= j <= j+k <= bhi``. For all ``(i', j', k')`` meeting those conditions, the additional conditions ``k >= k'``, ``i <= i'``, and if ``i == i'``, ``j <= j'`` are also met. In other words, of all maximal matching blocks, return one that starts earliest in *a*, and of all those maximal matching blocks that start earliest in *a*, return the one that starts earliest in *b*. >>> s = SequenceMatcher(None, " abcd", "abcd abcd") >>> s.find_longest_match(0, 5, 0, 9) Match(a=0, b=4, size=5) If *isjunk* 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. Here's the same example as before, but considering blanks to be junk. That prevents ``' abcd'`` from matching the ``' abcd'`` at the tail end of the second sequence directly. Instead only the ``'abcd'`` can match, and matches the leftmost ``'abcd'`` in the second sequence: >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") >>> s.find_longest_match(0, 5, 0, 9) Match(a=1, b=0, size=4) If no blocks match, this returns ``(alo, blo, 0)``. .. versionchanged:: 2.6 This method returns a :term:`named tuple` ``Match(a, b, size)``. .. method:: get_matching_blocks() Return list of triples describing non-overlapping matching subsequences. Each triple is of the form ``(i, j, n)``, and means that ``a[i:i+n] == b[j:j+n]``. The triples are monotonically increasing in *i* and *j*. The last triple is a dummy, and has the value ``(len(a), len(b), 0)``. It is the only triple with ``n == 0``. If ``(i, j, n)`` and ``(i', j', n')`` are adjacent triples in the list, and the second is not the last triple in the list, then ``i+n < i'`` or ``j+n < j'``; in other words, adjacent triples always describe non-adjacent equal blocks. .. XXX Explain why a dummy is used! .. versionchanged:: 2.5 The guarantee that adjacent triples always describe non-adjacent blocks was implemented. .. doctest:: >>> s = SequenceMatcher(None, "abxcd", "abcd") >>> s.get_matching_blocks() [Match(a=0, b=0, size=2), Match(a=3, b=2, size=2), Match(a=5, b=4, size=0)] .. method:: get_opcodes() Return list of 5-tuples describing how to turn *a* into *b*. Each tuple is of the form ``(tag, i1, i2, j1, j2)``. The first tuple has ``i1 == j1 == 0``, and remaining tuples have *i1* equal to the *i2* from the preceding tuple, and, likewise, *j1* equal to the previous *j2*. The *tag* values are strings, with these meanings: +---------------+---------------------------------------------+ | Value | Meaning | +===============+=============================================+ | ``'replace'`` | ``a[i1:i2]`` should be replaced by | | | ``b[j1:j2]``. | +---------------+---------------------------------------------+ | ``'delete'`` | ``a[i1:i2]`` should be deleted. Note that | | | ``j1 == j2`` in this case. | +---------------+---------------------------------------------+ | ``'insert'`` | ``b[j1:j2]`` should be inserted at | | | ``a[i1:i1]``. Note that ``i1 == i2`` in | | | this case. | +---------------+---------------------------------------------+ | ``'equal'`` | ``a[i1:i2] == b[j1:j2]`` (the sub-sequences | | | are equal). | +---------------+---------------------------------------------+ For example: >>> a = "qabxcd" >>> b = "abycdf" >>> s = SequenceMatcher(None, a, b) >>> for tag, i1, i2, j1, j2 in s.get_opcodes(): ... print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])) delete a[0:1] (q) b[0:0] () equal a[1:3] (ab) b[0:2] (ab) replace a[3:4] (x) b[2:3] (y) equal a[4:6] (cd) b[3:5] (cd) insert a[6:6] () b[5:6] (f) .. method:: get_grouped_opcodes([n]) Return a :term:`generator` of groups with up to *n* lines of context. Starting with the groups returned by :meth:`get_opcodes`, this method splits out smaller change clusters and eliminates intervening ranges which have no changes. The groups are returned in the same format as :meth:`get_opcodes`. .. versionadded:: 2.3 .. method:: ratio() Return a measure of the sequences' similarity as a float in the range [0, 1]. 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 ``1.0`` if the sequences are identical, and ``0.0`` if they have nothing in common. This is expensive to compute if :meth:`get_matching_blocks` or :meth:`get_opcodes` hasn't already been called, in which case you may want to try :meth:`quick_ratio` or :meth:`real_quick_ratio` first to get an upper bound. .. method:: quick_ratio() Return an upper bound on :meth:`ratio` relatively quickly. .. method:: real_quick_ratio() Return an upper bound on :meth:`ratio` very quickly. The three methods that return the ratio of matching to total characters can give different results due to differing levels of approximation, although :meth:`quick_ratio` and :meth:`real_quick_ratio` are always at least as large as :meth:`ratio`: >>> s = SequenceMatcher(None, "abcd", "bcde") >>> s.ratio() 0.75 >>> s.quick_ratio() 0.75 >>> s.real_quick_ratio() 1.0 .. _sequencematcher-examples: SequenceMatcher Examples ------------------------ This example compares two strings, considering blanks to be "junk:" >>> s = SequenceMatcher(lambda x: x == " ", ... "private Thread currentThread;", ... "private volatile Thread currentThread;") :meth:`ratio` returns a float in [0, 1], measuring the similarity of the sequences. As a rule of thumb, a :meth:`ratio` value over 0.6 means the sequences are close matches: >>> print round(s.ratio(), 3) 0.866 If you're only interested in where the sequences match, :meth:`get_matching_blocks` is handy: >>> for block in s.get_matching_blocks(): ... print "a[%d] and b[%d] match for %d elements" % block a[0] and b[0] match for 8 elements a[8] and b[17] match for 21 elements a[29] and b[38] match for 0 elements Note that the last tuple returned by :meth:`get_matching_blocks` is always a dummy, ``(len(a), len(b), 0)``, and this is the only case in which the last tuple element (number of elements matched) is ``0``. If you want to know how to change the first sequence into the second, use :meth:`get_opcodes`: >>> for opcode in s.get_opcodes(): ... print "%6s a[%d:%d] b[%d:%d]" % opcode equal a[0:8] b[0:8] insert a[8:8] b[8:17] equal a[8:29] b[17:38] .. seealso:: * The :func:`get_close_matches` function in this module which shows how simple code building on :class:`SequenceMatcher` can be used to do useful work. * `Simple version control recipe <https://code.activestate.com/recipes/576729/>`_ for a small application built with :class:`SequenceMatcher`. .. _differ-objects: Differ Objects -------------- Note that :class:`Differ`\ -generated deltas make no claim to be **minimal** 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. The :class:`Differ` class has this constructor: .. class:: Differ([linejunk[, charjunk]]) Optional keyword parameters *linejunk* and *charjunk* are for filter functions (or ``None``): *linejunk*: A function that accepts a single string argument, and returns true if the string is junk. The default is ``None``, meaning that no line is considered junk. *charjunk*: A function that accepts a single character argument (a string of length 1), and returns true if the character is junk. The default is ``None``, meaning that no character is considered junk. :class:`Differ` objects are used (deltas generated) via a single method: .. method:: Differ.compare(a, b) Compare two sequences of lines, and generate the delta (a sequence of lines). Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the :meth:`~file.readlines` method of file-like objects. The delta generated also consists of newline-terminated strings, ready to be printed as-is via the :meth:`~file.writelines` method of a file-like object. .. _differ-examples: Differ Example -------------- 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 :meth:`~file.readlines` method of file-like objects): >>> text1 = ''' 1. Beautiful is better than ugly. ... 2. Explicit is better than implicit. ... 3. Simple is better than complex. ... 4. Complex is better than complicated. ... '''.splitlines(1) >>> len(text1) 4 >>> text1[0][-1] '\n' >>> text2 = ''' 1. Beautiful is better than ugly. ... 3. Simple is better than complex. ... 4. Complicated is better than complex. ... 5. Flat is better than nested. ... '''.splitlines(1) Next we instantiate a Differ object: >>> d = Differ() Note that when instantiating a :class:`Differ` object we may pass functions to filter out line and character "junk." See the :meth:`Differ` constructor for details. Finally, we compare the two: >>> result = list(d.compare(text1, text2)) ``result`` is a list of strings, so let's pretty-print it: >>> from pprint import pprint >>> pprint(result) [' 1. Beautiful is better than ugly.\n', '- 2. Explicit is better than implicit.\n', '- 3. Simple is better than complex.\n', '+ 3. Simple is better than complex.\n', '? ++\n', '- 4. Complex is better than complicated.\n', '? ^ ---- ^\n', '+ 4. Complicated is better than complex.\n', '? ++++ ^ ^\n', '+ 5. Flat is better than nested.\n'] As a single multi-line string it looks like this: >>> import sys >>> sys.stdout.writelines(result) 1. Beautiful is better than ugly. - 2. Explicit is better than implicit. - 3. Simple is better than complex. + 3. Simple is better than complex. ? ++ - 4. Complex is better than complicated. ? ^ ---- ^ + 4. Complicated is better than complex. ? ++++ ^ ^ + 5. Flat is better than nested. .. _difflib-interface: A command-line interface to difflib ----------------------------------- This example shows how to use difflib to create a ``diff``-like utility. It is also contained in the Python source distribution, as :file:`Tools/scripts/diff.py`. .. testcode:: """ Command line interface to difflib.py providing diffs in four formats: * ndiff: lists every line and highlights interline changes. * context: highlights clusters of changes in a before/after format. * unified: highlights clusters of changes in an inline format. * html: generates side by side comparison with change highlights. """ import sys, os, time, difflib, optparse def main(): # Configure the option parser usage = "usage: %prog [options] fromfile tofile" parser = optparse.OptionParser(usage) parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)') parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff') hlp = 'Produce HTML side by side diff (can use -c and -l in conjunction)' parser.add_option("-m", action="store_true", default=False, help=hlp) parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff') parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)') (options, args) = parser.parse_args() if len(args) == 0: parser.print_help() sys.exit(1) if len(args) != 2: parser.error("need to specify both a fromfile and tofile") n = options.lines fromfile, tofile = args # as specified in the usage string # we're passing these as arguments to the diff function fromdate = time.ctime(os.stat(fromfile).st_mtime) todate = time.ctime(os.stat(tofile).st_mtime) with open(fromfile, 'U') as f: fromlines = f.readlines() with open(tofile, 'U') as f: tolines = f.readlines() if options.u: diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) elif options.n: diff = difflib.ndiff(fromlines, tolines) elif options.m: diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile, tofile, context=options.c, numlines=n) else: diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n) # we're using writelines because diff is a generator sys.stdout.writelines(diff) if __name__ == '__main__': main()
Save
cmd:
run