Skip to content

Commit 57548c8

Browse files
committed
visual example of skew in docstring
1 parent 568b0ef commit 57548c8

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

Lib/difflib.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,10 +2746,10 @@ def __repr__(self):
27462746

27472747

27482748
def _calc_skew(i, j, k, alo, ahi, blo, bhi):
2749-
"""Difference in normalized positions
2749+
"""Difference in normalized positions of block mid-points
27502750
Returns skew : float, where -1 < skew < 1
27512751
"""
2752-
k_div_2 = k // 2
2752+
k_div_2 = k / 2
27532753
apos = (i + k_div_2 - alo) / (ahi - alo)
27542754
bpos = (j + k_div_2 - blo) / (bhi - blo)
27552755
return apos - bpos
@@ -2793,7 +2793,21 @@ def __init__(self, isjunk=None, a='', b='', balancing=0):
27932793
where -1 <= skew <= 1.
27942794
Recommended value is 2/3, which means that 2/3 of
27952795
worst possible skew values will be eligible for balancing.
2796-
Note for the future: balancing procedure scales to k-srings well
2796+
Note for the future: balancing procedure scales to k-strings well
2797+
2798+
Skewed matching block visually:
2799+
2800+
m1 = (6 + 9) / 2 = 7.5
2801+
|
2802+
------###--
2803+
--###------
2804+
|
2805+
m2 = (2 + 5) / 2 = 3.5
2806+
2807+
skew = 7.5 / 11 - 3.5 / 11 = 0.3636
2808+
do_balancing = abs(skew) > 1 - balancing
2809+
2810+
with balancing == 2/3, this would try alternatives with lookahead
27972811
27982812
Comparison to SequenceMatcher:
27992813
In terms of results, the following 2 are equivalent:

0 commit comments

Comments
 (0)