blob: 95b83e63d5bd9ae400b7e12ae373d45af6f99f6a [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`difflib` --- Helpers for computing deltas
3===============================================
4
5.. module:: difflib
6 :synopsis: Helpers for computing differences between objects.
7.. moduleauthor:: Tim Peters <tim_one@users.sourceforge.net>
8.. sectionauthor:: Tim Peters <tim_one@users.sourceforge.net>
9
10
11.. % LaTeXification by Fred L. Drake, Jr. <fdrake@acm.org>.
12
13.. versionadded:: 2.1
14
15
16.. class:: SequenceMatcher
17
18 This is a flexible class for comparing pairs of sequences of any type, so long
19 as the sequence elements are hashable. The basic algorithm predates, and is a
20 little fancier than, an algorithm published in the late 1980's by Ratcliff and
21 Obershelp under the hyperbolic name "gestalt pattern matching." The idea is to
22 find the longest contiguous matching subsequence that contains no "junk"
23 elements (the Ratcliff and Obershelp algorithm doesn't address junk). The same
24 idea is then applied recursively to the pieces of the sequences to the left and
25 to the right of the matching subsequence. This does not yield minimal edit
26 sequences, but does tend to yield matches that "look right" to people.
27
28 **Timing:** The basic Ratcliff-Obershelp algorithm is cubic time in the worst
29 case and quadratic time in the expected case. :class:`SequenceMatcher` is
30 quadratic time for the worst case and has expected-case behavior dependent in a
31 complicated way on how many elements the sequences have in common; best case
32 time is linear.
33
34
35.. class:: Differ
36
37 This is a class for comparing sequences of lines of text, and producing
38 human-readable differences or deltas. Differ uses :class:`SequenceMatcher`
39 both to compare sequences of lines, and to compare sequences of characters
40 within similar (near-matching) lines.
41
42 Each line of a :class:`Differ` delta begins with a two-letter code:
43
44 +----------+-------------------------------------------+
45 | Code | Meaning |
46 +==========+===========================================+
47 | ``'- '`` | line unique to sequence 1 |
48 +----------+-------------------------------------------+
49 | ``'+ '`` | line unique to sequence 2 |
50 +----------+-------------------------------------------+
51 | ``' '`` | line common to both sequences |
52 +----------+-------------------------------------------+
53 | ``'? '`` | line not present in either input sequence |
54 +----------+-------------------------------------------+
55
56 Lines beginning with '``?``' attempt to guide the eye to intraline differences,
57 and were not present in either input sequence. These lines can be confusing if
58 the sequences contain tab characters.
59
60
61.. class:: HtmlDiff
62
63 This class can be used to create an HTML table (or a complete HTML file
64 containing the table) showing a side by side, line by line comparison of text
65 with inter-line and intra-line change highlights. The table can be generated in
66 either full or contextual difference mode.
67
68 The constructor for this class is:
69
70
71 .. function:: __init__([tabsize][, wrapcolumn][, linejunk][, charjunk])
72
73 Initializes instance of :class:`HtmlDiff`.
74
75 *tabsize* is an optional keyword argument to specify tab stop spacing and
76 defaults to ``8``.
77
78 *wrapcolumn* is an optional keyword to specify column number where lines are
79 broken and wrapped, defaults to ``None`` where lines are not wrapped.
80
81 *linejunk* and *charjunk* are optional keyword arguments passed into ``ndiff()``
82 (used by :class:`HtmlDiff` to generate the side by side HTML differences). See
83 ``ndiff()`` documentation for argument default values and descriptions.
84
85 The following methods are public:
86
87
88 .. function:: make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
89
90 Compares *fromlines* and *tolines* (lists of strings) and returns a string which
91 is a complete HTML file containing a table showing line by line differences with
92 inter-line and intra-line changes highlighted.
93
94 *fromdesc* and *todesc* are optional keyword arguments to specify from/to file
95 column header strings (both default to an empty string).
96
97 *context* and *numlines* are both optional keyword arguments. Set *context* to
98 ``True`` when contextual differences are to be shown, else the default is
99 ``False`` to show the full files. *numlines* defaults to ``5``. When *context*
100 is ``True`` *numlines* controls the number of context lines which surround the
101 difference highlights. When *context* is ``False`` *numlines* controls the
102 number of lines which are shown before a difference highlight when using the
103 "next" hyperlinks (setting to zero would cause the "next" hyperlinks to place
104 the next difference highlight at the top of the browser without any leading
105 context).
106
107
108 .. function:: make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
109
110 Compares *fromlines* and *tolines* (lists of strings) and returns a string which
111 is a complete HTML table showing line by line differences with inter-line and
112 intra-line changes highlighted.
113
114 The arguments for this method are the same as those for the :meth:`make_file`
115 method.
116
117 :file:`Tools/scripts/diff.py` is a command-line front-end to this class and
118 contains a good example of its use.
119
120 .. versionadded:: 2.4
121
122
123.. function:: context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
124
125 Compare *a* and *b* (lists of strings); return a delta (a generator generating
126 the delta lines) in context diff format.
127
128 Context diffs are a compact way of showing just the lines that have changed plus
129 a few lines of context. The changes are shown in a before/after style. The
130 number of context lines is set by *n* which defaults to three.
131
132 By default, the diff control lines (those with ``***`` or ``---``) are created
133 with a trailing newline. This is helpful so that inputs created from
134 :func:`file.readlines` result in diffs that are suitable for use with
135 :func:`file.writelines` since both the inputs and outputs have trailing
136 newlines.
137
138 For inputs that do not have trailing newlines, set the *lineterm* argument to
139 ``""`` so that the output will be uniformly newline free.
140
141 The context diff format normally has a header for filenames and modification
142 times. Any or all of these may be specified using strings for *fromfile*,
143 *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
144 expressed in the format returned by :func:`time.ctime`. If not specified, the
145 strings default to blanks.
146
147 :file:`Tools/scripts/diff.py` is a command-line front-end for this function.
148
149 .. versionadded:: 2.3
150
151
152.. function:: get_close_matches(word, possibilities[, n][, cutoff])
153
154 Return a list of the best "good enough" matches. *word* is a sequence for which
155 close matches are desired (typically a string), and *possibilities* is a list of
156 sequences against which to match *word* (typically a list of strings).
157
158 Optional argument *n* (default ``3``) is the maximum number of close matches to
159 return; *n* must be greater than ``0``.
160
161 Optional argument *cutoff* (default ``0.6``) is a float in the range [0, 1].
162 Possibilities that don't score at least that similar to *word* are ignored.
163
164 The best (no more than *n*) matches among the possibilities are returned in a
165 list, sorted by similarity score, most similar first. ::
166
167 >>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
168 ['apple', 'ape']
169 >>> import keyword
170 >>> get_close_matches('wheel', keyword.kwlist)
171 ['while']
172 >>> get_close_matches('apple', keyword.kwlist)
173 []
174 >>> get_close_matches('accept', keyword.kwlist)
175 ['except']
176
177
178.. function:: ndiff(a, b[, linejunk][, charjunk])
179
180 Compare *a* and *b* (lists of strings); return a :class:`Differ`\ -style delta
181 (a generator generating the delta lines).
182
183 Optional keyword parameters *linejunk* and *charjunk* are for filter functions
184 (or ``None``):
185
186 *linejunk*: A function that accepts a single string argument, and returns true
187 if the string is junk, or false if not. The default is (``None``), starting with
188 Python 2.3. Before then, the default was the module-level function
189 :func:`IS_LINE_JUNK`, which filters out lines without visible characters, except
190 for at most one pound character (``'#'``). As of Python 2.3, the underlying
191 :class:`SequenceMatcher` class does a dynamic analysis of which lines are so
192 frequent as to constitute noise, and this usually works better than the pre-2.3
193 default.
194
195 *charjunk*: A function that accepts a character (a string of length 1), and
196 returns if the character is junk, or false if not. The default is module-level
197 function :func:`IS_CHARACTER_JUNK`, which filters out whitespace characters (a
198 blank or tab; note: bad idea to include newline in this!).
199
200 :file:`Tools/scripts/ndiff.py` is a command-line front-end to this function. ::
201
202 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
203 ... 'ore\ntree\nemu\n'.splitlines(1))
204 >>> print ''.join(diff),
205 - one
206 ? ^
207 + ore
208 ? ^
209 - two
210 - three
211 ? -
212 + tree
213 + emu
214
215
216.. function:: restore(sequence, which)
217
218 Return one of the two sequences that generated a delta.
219
220 Given a *sequence* produced by :meth:`Differ.compare` or :func:`ndiff`, extract
221 lines originating from file 1 or 2 (parameter *which*), stripping off line
222 prefixes.
223
224 Example::
225
226 >>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),
227 ... 'ore\ntree\nemu\n'.splitlines(1))
228 >>> diff = list(diff) # materialize the generated delta into a list
229 >>> print ''.join(restore(diff, 1)),
230 one
231 two
232 three
233 >>> print ''.join(restore(diff, 2)),
234 ore
235 tree
236 emu
237
238
239.. function:: unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
240
241 Compare *a* and *b* (lists of strings); return a delta (a generator generating
242 the delta lines) in unified diff format.
243
244 Unified diffs are a compact way of showing just the lines that have changed plus
245 a few lines of context. The changes are shown in a inline style (instead of
246 separate before/after blocks). The number of context lines is set by *n* which
247 defaults to three.
248
249 By default, the diff control lines (those with ``---``, ``+++``, or ``@@``) are
250 created with a trailing newline. This is helpful so that inputs created from
251 :func:`file.readlines` result in diffs that are suitable for use with
252 :func:`file.writelines` since both the inputs and outputs have trailing
253 newlines.
254
255 For inputs that do not have trailing newlines, set the *lineterm* argument to
256 ``""`` so that the output will be uniformly newline free.
257
258 The context diff format normally has a header for filenames and modification
259 times. Any or all of these may be specified using strings for *fromfile*,
260 *tofile*, *fromfiledate*, and *tofiledate*. The modification times are normally
261 expressed in the format returned by :func:`time.ctime`. If not specified, the
262 strings default to blanks.
263
264 :file:`Tools/scripts/diff.py` is a command-line front-end for this function.
265
266 .. versionadded:: 2.3
267
268
269.. function:: IS_LINE_JUNK(line)
270
271 Return true for ignorable lines. The line *line* is ignorable if *line* is
272 blank or contains a single ``'#'``, otherwise it is not ignorable. Used as a
273 default for parameter *linejunk* in :func:`ndiff` before Python 2.3.
274
275
276.. function:: IS_CHARACTER_JUNK(ch)
277
278 Return true for ignorable characters. The character *ch* is ignorable if *ch*
279 is a space or tab, otherwise it is not ignorable. Used as a default for
280 parameter *charjunk* in :func:`ndiff`.
281
282
283.. seealso::
284
285 `Pattern Matching: The Gestalt Approach <http://www.ddj.com/184407970?pgno=5>`_
286 Discussion of a similar algorithm by John W. Ratcliff and D. E. Metzener. This
287 was published in `Dr. Dobb's Journal <http://www.ddj.com/>`_ in July, 1988.
288
289
290.. _sequence-matcher:
291
292SequenceMatcher Objects
293-----------------------
294
295The :class:`SequenceMatcher` class has this constructor:
296
297
298.. class:: SequenceMatcher([isjunk[, a[, b]]])
299
300 Optional argument *isjunk* must be ``None`` (the default) or a one-argument
301 function that takes a sequence element and returns true if and only if the
302 element is "junk" and should be ignored. Passing ``None`` for *isjunk* is
303 equivalent to passing ``lambda x: 0``; in other words, no elements are ignored.
304 For example, pass::
305
306 lambda x: x in " \t"
307
308 if you're comparing lines as sequences of characters, and don't want to synch up
309 on blanks or hard tabs.
310
311 The optional arguments *a* and *b* are sequences to be compared; both default to
312 empty strings. The elements of both sequences must be hashable.
313
314:class:`SequenceMatcher` objects have the following methods:
315
316
317.. method:: SequenceMatcher.set_seqs(a, b)
318
319 Set the two sequences to be compared.
320
321:class:`SequenceMatcher` computes and caches detailed information about the
322second sequence, so if you want to compare one sequence against many sequences,
323use :meth:`set_seq2` to set the commonly used sequence once and call
324:meth:`set_seq1` repeatedly, once for each of the other sequences.
325
326
327.. method:: SequenceMatcher.set_seq1(a)
328
329 Set the first sequence to be compared. The second sequence to be compared is
330 not changed.
331
332
333.. method:: SequenceMatcher.set_seq2(b)
334
335 Set the second sequence to be compared. The first sequence to be compared is
336 not changed.
337
338
339.. method:: SequenceMatcher.find_longest_match(alo, ahi, blo, bhi)
340
341 Find longest matching block in ``a[alo:ahi]`` and ``b[blo:bhi]``.
342
343 If *isjunk* was omitted or ``None``, :meth:`get_longest_match` returns ``(i, j,
344 k)`` such that ``a[i:i+k]`` is equal to ``b[j:j+k]``, where ``alo <= i <= i+k <=
345 ahi`` and ``blo <= j <= j+k <= bhi``. For all ``(i', j', k')`` meeting those
346 conditions, the additional conditions ``k >= k'``, ``i <= i'``, and if ``i ==
347 i'``, ``j <= j'`` are also met. In other words, of all maximal matching blocks,
348 return one that starts earliest in *a*, and of all those maximal matching blocks
349 that start earliest in *a*, return the one that starts earliest in *b*. ::
350
351 >>> s = SequenceMatcher(None, " abcd", "abcd abcd")
352 >>> s.find_longest_match(0, 5, 0, 9)
353 (0, 4, 5)
354
355 If *isjunk* was provided, first the longest matching block is determined as
356 above, but with the additional restriction that no junk element appears in the
357 block. Then that block is extended as far as possible by matching (only) junk
358 elements on both sides. So the resulting block never matches on junk except as
359 identical junk happens to be adjacent to an interesting match.
360
361 Here's the same example as before, but considering blanks to be junk. That
362 prevents ``' abcd'`` from matching the ``' abcd'`` at the tail end of the second
363 sequence directly. Instead only the ``'abcd'`` can match, and matches the
364 leftmost ``'abcd'`` in the second sequence::
365
366 >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd")
367 >>> s.find_longest_match(0, 5, 0, 9)
368 (1, 0, 4)
369
370 If no blocks match, this returns ``(alo, blo, 0)``.
371
372
373.. method:: SequenceMatcher.get_matching_blocks()
374
375 Return list of triples describing matching subsequences. Each triple is of the
376 form ``(i, j, n)``, and means that ``a[i:i+n] == b[j:j+n]``. The triples are
377 monotonically increasing in *i* and *j*.
378
379 The last triple is a dummy, and has the value ``(len(a), len(b), 0)``. It is
380 the only triple with ``n == 0``. If ``(i, j, n)`` and ``(i', j', n')`` are
381 adjacent triples in the list, and the second is not the last triple in the list,
382 then ``i+n != i'`` or ``j+n != j'``; in other words, adjacent triples always
383 describe non-adjacent equal blocks.
384
385 .. % Explain why a dummy is used!
386
387 .. versionchanged:: 2.5
388 The guarantee that adjacent triples always describe non-adjacent blocks was
389 implemented.
390
391 ::
392
393 >>> s = SequenceMatcher(None, "abxcd", "abcd")
394 >>> s.get_matching_blocks()
395 [(0, 0, 2), (3, 2, 2), (5, 4, 0)]
396
397
398.. method:: SequenceMatcher.get_opcodes()
399
400 Return list of 5-tuples describing how to turn *a* into *b*. Each tuple is of
401 the form ``(tag, i1, i2, j1, j2)``. The first tuple has ``i1 == j1 == 0``, and
402 remaining tuples have *i1* equal to the *i2* from the preceding tuple, and,
403 likewise, *j1* equal to the previous *j2*.
404
405 The *tag* values are strings, with these meanings:
406
407 +---------------+---------------------------------------------+
408 | Value | Meaning |
409 +===============+=============================================+
410 | ``'replace'`` | ``a[i1:i2]`` should be replaced by |
411 | | ``b[j1:j2]``. |
412 +---------------+---------------------------------------------+
413 | ``'delete'`` | ``a[i1:i2]`` should be deleted. Note that |
414 | | ``j1 == j2`` in this case. |
415 +---------------+---------------------------------------------+
416 | ``'insert'`` | ``b[j1:j2]`` should be inserted at |
417 | | ``a[i1:i1]``. Note that ``i1 == i2`` in |
418 | | this case. |
419 +---------------+---------------------------------------------+
420 | ``'equal'`` | ``a[i1:i2] == b[j1:j2]`` (the sub-sequences |
421 | | are equal). |
422 +---------------+---------------------------------------------+
423
424 For example::
425
426 >>> a = "qabxcd"
427 >>> b = "abycdf"
428 >>> s = SequenceMatcher(None, a, b)
429 >>> for tag, i1, i2, j1, j2 in s.get_opcodes():
430 ... print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
431 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))
432 delete a[0:1] (q) b[0:0] ()
433 equal a[1:3] (ab) b[0:2] (ab)
434 replace a[3:4] (x) b[2:3] (y)
435 equal a[4:6] (cd) b[3:5] (cd)
436 insert a[6:6] () b[5:6] (f)
437
438
439.. method:: SequenceMatcher.get_grouped_opcodes([n])
440
441 Return a generator of groups with up to *n* lines of context.
442
443 Starting with the groups returned by :meth:`get_opcodes`, this method splits out
444 smaller change clusters and eliminates intervening ranges which have no changes.
445
446 The groups are returned in the same format as :meth:`get_opcodes`.
447
448 .. versionadded:: 2.3
449
450
451.. method:: SequenceMatcher.ratio()
452
453 Return a measure of the sequences' similarity as a float in the range [0, 1].
454
455 Where T is the total number of elements in both sequences, and M is the number
456 of matches, this is 2.0\*M / T. Note that this is ``1.0`` if the sequences are
457 identical, and ``0.0`` if they have nothing in common.
458
459 This is expensive to compute if :meth:`get_matching_blocks` or
460 :meth:`get_opcodes` hasn't already been called, in which case you may want to
461 try :meth:`quick_ratio` or :meth:`real_quick_ratio` first to get an upper bound.
462
463
464.. method:: SequenceMatcher.quick_ratio()
465
466 Return an upper bound on :meth:`ratio` relatively quickly.
467
468 This isn't defined beyond that it is an upper bound on :meth:`ratio`, and is
469 faster to compute.
470
471
472.. method:: SequenceMatcher.real_quick_ratio()
473
474 Return an upper bound on :meth:`ratio` very quickly.
475
476 This isn't defined beyond that it is an upper bound on :meth:`ratio`, and is
477 faster to compute than either :meth:`ratio` or :meth:`quick_ratio`.
478
479The three methods that return the ratio of matching to total characters can give
480different results due to differing levels of approximation, although
481:meth:`quick_ratio` and :meth:`real_quick_ratio` are always at least as large as
482:meth:`ratio`::
483
484 >>> s = SequenceMatcher(None, "abcd", "bcde")
485 >>> s.ratio()
486 0.75
487 >>> s.quick_ratio()
488 0.75
489 >>> s.real_quick_ratio()
490 1.0
491
492
493.. _sequencematcher-examples:
494
495SequenceMatcher Examples
496------------------------
497
498This example compares two strings, considering blanks to be "junk:" ::
499
500 >>> s = SequenceMatcher(lambda x: x == " ",
501 ... "private Thread currentThread;",
502 ... "private volatile Thread currentThread;")
503
504:meth:`ratio` returns a float in [0, 1], measuring the similarity of the
505sequences. As a rule of thumb, a :meth:`ratio` value over 0.6 means the
506sequences are close matches::
507
508 >>> print round(s.ratio(), 3)
509 0.866
510
511If you're only interested in where the sequences match,
512:meth:`get_matching_blocks` is handy::
513
514 >>> for block in s.get_matching_blocks():
515 ... print "a[%d] and b[%d] match for %d elements" % block
516 a[0] and b[0] match for 8 elements
517 a[8] and b[17] match for 6 elements
518 a[14] and b[23] match for 15 elements
519 a[29] and b[38] match for 0 elements
520
521Note that the last tuple returned by :meth:`get_matching_blocks` is always a
522dummy, ``(len(a), len(b), 0)``, and this is the only case in which the last
523tuple element (number of elements matched) is ``0``.
524
525If you want to know how to change the first sequence into the second, use
526:meth:`get_opcodes`::
527
528 >>> for opcode in s.get_opcodes():
529 ... print "%6s a[%d:%d] b[%d:%d]" % opcode
530 equal a[0:8] b[0:8]
531 insert a[8:8] b[8:17]
532 equal a[8:14] b[17:23]
533 equal a[14:29] b[23:38]
534
535See also the function :func:`get_close_matches` in this module, which shows how
536simple code building on :class:`SequenceMatcher` can be used to do useful work.
537
538
539.. _differ-objects:
540
541Differ Objects
542--------------
543
544Note that :class:`Differ`\ -generated deltas make no claim to be **minimal**
545diffs. To the contrary, minimal diffs are often counter-intuitive, because they
546synch up anywhere possible, sometimes accidental matches 100 pages apart.
547Restricting synch points to contiguous matches preserves some notion of
548locality, at the occasional cost of producing a longer diff.
549
550The :class:`Differ` class has this constructor:
551
552
553.. class:: Differ([linejunk[, charjunk]])
554
555 Optional keyword parameters *linejunk* and *charjunk* are for filter functions
556 (or ``None``):
557
558 *linejunk*: A function that accepts a single string argument, and returns true
559 if the string is junk. The default is ``None``, meaning that no line is
560 considered junk.
561
562 *charjunk*: A function that accepts a single character argument (a string of
563 length 1), and returns true if the character is junk. The default is ``None``,
564 meaning that no character is considered junk.
565
566:class:`Differ` objects are used (deltas generated) via a single method:
567
568
569.. method:: Differ.compare(a, b)
570
571 Compare two sequences of lines, and generate the delta (a sequence of lines).
572
573 Each sequence must contain individual single-line strings ending with newlines.
574 Such sequences can be obtained from the :meth:`readlines` method of file-like
575 objects. The delta generated also consists of newline-terminated strings, ready
576 to be printed as-is via the :meth:`writelines` method of a file-like object.
577
578
579.. _differ-examples:
580
581Differ Example
582--------------
583
584This example compares two texts. First we set up the texts, sequences of
585individual single-line strings ending with newlines (such sequences can also be
586obtained from the :meth:`readlines` method of file-like objects)::
587
588 >>> text1 = ''' 1. Beautiful is better than ugly.
589 ... 2. Explicit is better than implicit.
590 ... 3. Simple is better than complex.
591 ... 4. Complex is better than complicated.
592 ... '''.splitlines(1)
593 >>> len(text1)
594 4
595 >>> text1[0][-1]
596 '\n'
597 >>> text2 = ''' 1. Beautiful is better than ugly.
598 ... 3. Simple is better than complex.
599 ... 4. Complicated is better than complex.
600 ... 5. Flat is better than nested.
601 ... '''.splitlines(1)
602
603Next we instantiate a Differ object::
604
605 >>> d = Differ()
606
607Note that when instantiating a :class:`Differ` object we may pass functions to
608filter out line and character "junk." See the :meth:`Differ` constructor for
609details.
610
611Finally, we compare the two::
612
613 >>> result = list(d.compare(text1, text2))
614
615``result`` is a list of strings, so let's pretty-print it::
616
617 >>> from pprint import pprint
618 >>> pprint(result)
619 [' 1. Beautiful is better than ugly.\n',
620 '- 2. Explicit is better than implicit.\n',
621 '- 3. Simple is better than complex.\n',
622 '+ 3. Simple is better than complex.\n',
623 '? ++ \n',
624 '- 4. Complex is better than complicated.\n',
625 '? ^ ---- ^ \n',
626 '+ 4. Complicated is better than complex.\n',
627 '? ++++ ^ ^ \n',
628 '+ 5. Flat is better than nested.\n']
629
630As a single multi-line string it looks like this::
631
632 >>> import sys
633 >>> sys.stdout.writelines(result)
634 1. Beautiful is better than ugly.
635 - 2. Explicit is better than implicit.
636 - 3. Simple is better than complex.
637 + 3. Simple is better than complex.
638 ? ++
639 - 4. Complex is better than complicated.
640 ? ^ ---- ^
641 + 4. Complicated is better than complex.
642 ? ++++ ^ ^
643 + 5. Flat is better than nested.
644