Skip to content

Commit c0edd7e

Browse files
committed
utility doctests
1 parent 41b5a12 commit c0edd7e

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

Lib/difflib.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ def _expand_block(block, a, b, alo, ahi, blo, bhi, *, pred=None):
4747
4848
pred: callable
4949
additionally, only expand if pred(matching_element) returns True
50+
51+
Examples:
52+
>>> a, b = '_cabbac_', '.cabbac.'
53+
>>> _expand_block((3, 3, 2), a, b, 0, 8, 0, 8)
54+
(1, 1, 6)
55+
>>> _expand_block((3, 3, 2), a, b, 0, 8, 0, 8, pred='a'.__contains__)
56+
(2, 2, 4)
5057
"""
5158
i, j, k = block
5259
while i > alo and j > blo:
@@ -63,13 +70,16 @@ def _expand_block(block, a, b, alo, ahi, blo, bhi, *, pred=None):
6370
k += 1
6471
return (i, j, k)
6572

66-
def _collapse_adjacent_blocks(blocks, remove_zero_length=True):
67-
"""Collapses adjacent blocks
73+
def _collapse_adjacent_blocks(blocks):
74+
"""Collapses adjacent blocks and remove null blocks
75+
76+
Examples:
77+
>>> blocks = [(1, 1, 2), (3, 3, 2), (6, 6, 0), (10, 10, 1)]
78+
>>> list(_collapse_adjacent_blocks(blocks))
79+
[(1, 1, 4), (10, 10, 1)]
6880
"""
6981
i1 = j1 = k1 = 0
7082
for i2, j2, k2 in blocks:
71-
if remove_zero_length and not k2:
72-
continue
7383
# Is this block adjacent to i1, j1, k1?
7484
if i1 + k1 == i2 and j1 + k1 == j2:
7585
# Yes, so collapse them -- this just increases the length of
@@ -282,7 +292,7 @@ def get_matching_blocks(self):
282292
blocks = self.matching_blocks
283293
if blocks is None:
284294
blocks = self._get_matching_blocks()
285-
blocks = _collapse_adjacent_blocks(blocks, remove_zero_length=True)
295+
blocks = _collapse_adjacent_blocks(blocks)
286296
blocks = list(map(Match._make, blocks))
287297
# Append dummy at the end
288298
blocks.append(Match(len(self.a), len(self.b), 0))

0 commit comments

Comments
 (0)