Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47e120e

Browse files
committed
Cleanup and modernize code prior to working on Issue 11747.
1 parent bed9a5b commit 47e120e

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

Lib/difflib.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,22 +1188,23 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
11881188
started = False
11891189
for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
11901190
if not started:
1191-
fromdate = '\t%s' % fromfiledate if fromfiledate else ''
1192-
todate = '\t%s' % tofiledate if tofiledate else ''
1193-
yield '--- %s%s%s' % (fromfile, fromdate, lineterm)
1194-
yield '+++ %s%s%s' % (tofile, todate, lineterm)
11951191
started = True
1196-
i1, i2, j1, j2 = group[0][1], group[-1][2], group[0][3], group[-1][4]
1197-
yield "@@ -%d,%d +%d,%d @@%s" % (i1+1, i2-i1, j1+1, j2-j1, lineterm)
1192+
fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
1193+
todate = '\t{}'.format(tofiledate) if tofiledate else ''
1194+
yield '--- {}{}{}'.format(fromfile, fromdate, lineterm)
1195+
yield '+++ {}{}{}'.format(tofile, todate, lineterm)
1196+
first, last = group[0], group[-1]
1197+
i1, i2, j1, j2 = first[1], last[2], first[3], last[4]
1198+
yield '@@ -{},{} +{},{} @@{}'.format(i1+1, i2-i1, j1+1, j2-j1, lineterm)
11981199
for tag, i1, i2, j1, j2 in group:
11991200
if tag == 'equal':
12001201
for line in a[i1:i2]:
12011202
yield ' ' + line
12021203
continue
1203-
if tag == 'replace' or tag == 'delete':
1204+
if tag in {'replace', 'delete'}:
12041205
for line in a[i1:i2]:
12051206
yield '-' + line
1206-
if tag == 'replace' or tag == 'insert':
1207+
if tag in {'replace', 'insert'}:
12071208
for line in b[j1:j2]:
12081209
yield '+' + line
12091210

@@ -1252,38 +1253,38 @@ def context_diff(a, b, fromfile='', tofile='',
12521253
four
12531254
"""
12541255

1256+
prefix = dict(insert='+ ', delete='- ', replace='! ', equal=' ')
12551257
started = False
1256-
prefixmap = {'insert':'+ ', 'delete':'- ', 'replace':'! ', 'equal':' '}
12571258
for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
12581259
if not started:
1259-
fromdate = '\t%s' % fromfiledate if fromfiledate else ''
1260-
todate = '\t%s' % tofiledate if tofiledate else ''
1261-
yield '*** %s%s%s' % (fromfile, fromdate, lineterm)
1262-
yield '--- %s%s%s' % (tofile, todate, lineterm)
12631260
started = True
1261+
fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
1262+
todate = '\t{}'.format(tofiledate) if tofiledate else ''
1263+
yield '*** {}{}{}'.format(fromfile, fromdate, lineterm)
1264+
yield '--- {}{}{}'.format(tofile, todate, lineterm)
12641265

1265-
yield '***************%s' % (lineterm,)
1266-
if group[-1][2] - group[0][1] >= 2:
1267-
yield '*** %d,%d ****%s' % (group[0][1]+1, group[-1][2], lineterm)
1266+
first, last = group[0], group[-1]
1267+
yield '***************{}'.format(lineterm)
1268+
1269+
if last[2] - first[1] > 1:
1270+
yield '*** {},{} ****{}'.format(first[1]+1, last[2], lineterm)
12681271
else:
1269-
yield '*** %d ****%s' % (group[-1][2], lineterm)
1270-
visiblechanges = [e for e in group if e[0] in ('replace', 'delete')]
1271-
if visiblechanges:
1272+
yield '*** {} ****{}'.format(last[2], lineterm)
1273+
if any(tag in {'replace', 'delete'} for tag, _, _, _, _ in group):
12721274
for tag, i1, i2, _, _ in group:
12731275
if tag != 'insert':
12741276
for line in a[i1:i2]:
1275-
yield prefixmap[tag] + line
1277+
yield prefix[tag] + line
12761278

1277-
if group[-1][4] - group[0][3] >= 2:
1278-
yield '--- %d,%d ----%s' % (group[0][3]+1, group[-1][4], lineterm)
1279+
if last[4] - first[3] > 1:
1280+
yield '--- {},{} ----{}'.format(first[3]+1, last[4], lineterm)
12791281
else:
1280-
yield '--- %d ----%s' % (group[-1][4], lineterm)
1281-
visiblechanges = [e for e in group if e[0] in ('replace', 'insert')]
1282-
if visiblechanges:
1282+
yield '--- {} ----{}'.format(last[4], lineterm)
1283+
if any(tag in {'replace', 'insert'} for tag, _, _, _, _ in group):
12831284
for tag, _, _, j1, j2 in group:
12841285
if tag != 'delete':
12851286
for line in b[j1:j2]:
1286-
yield prefixmap[tag] + line
1287+
yield prefix[tag] + line
12871288

12881289
def ndiff(a, b, linejunk=None, charjunk=IS_CHARACTER_JUNK):
12891290
r"""

0 commit comments

Comments
 (0)