MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / context_diff

Function context_diff

tools/python-3.11.9-amd64/Lib/difflib.py:1180–1254  ·  view source on GitHub ↗

r""" Compare two sequences of lines; generate the delta as a context diff. Context diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three. By default, the diff control lines (those wi

(a, b, fromfile='', tofile='',
                 fromfiledate='', tofiledate='', n=3, lineterm='\n')

Source from the content-addressed store, hash-verified

1178
1179# See http://www.unix.org/single_unix_specification/
1180def context_diff(a, b, fromfile='', tofile='',
1181 fromfiledate='', tofiledate='', n=3, lineterm='\n'):
1182 r"""
1183 Compare two sequences of lines; generate the delta as a context diff.
1184
1185 Context diffs are a compact way of showing line changes and a few
1186 lines of context. The number of context lines is set by 'n' which
1187 defaults to three.
1188
1189 By default, the diff control lines (those with *** or ---) are
1190 created with a trailing newline. This is helpful so that inputs
1191 created from file.readlines() result in diffs that are suitable for
1192 file.writelines() since both the inputs and outputs have trailing
1193 newlines.
1194
1195 For inputs that do not have trailing newlines, set the lineterm
1196 argument to "" so that the output will be uniformly newline free.
1197
1198 The context diff format normally has a header for filenames and
1199 modification times. Any or all of these may be specified using
1200 strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
1201 The modification times are normally expressed in the ISO 8601 format.
1202 If not specified, the strings default to blanks.
1203
1204 Example:
1205
1206 >>> print(''.join(context_diff('one\ntwo\nthree\nfour\n'.splitlines(True),
1207 ... 'zero\none\ntree\nfour\n'.splitlines(True), 'Original', 'Current')),
1208 ... end="")
1209 *** Original
1210 --- Current
1211 ***************
1212 *** 1,4 ****
1213 one
1214 ! two
1215 ! three
1216 four
1217 --- 1,4 ----
1218 + zero
1219 one
1220 ! tree
1221 four
1222 """
1223
1224 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
1225 prefix = dict(insert='+ ', delete='- ', replace='! ', equal=' ')
1226 started = False
1227 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
1228 if not started:
1229 started = True
1230 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
1231 todate = '\t{}'.format(tofiledate) if tofiledate else ''
1232 yield '*** {}{}{}'.format(fromfile, fromdate, lineterm)
1233 yield '--- {}{}{}'.format(tofile, todate, lineterm)
1234
1235 first, last = group[0], group[-1]
1236 yield '***************' + lineterm
1237

Callers

nothing calls this directly

Calls 6

_check_typesFunction · 0.85
SequenceMatcherClass · 0.85
_format_range_contextFunction · 0.85
get_grouped_opcodesMethod · 0.80
anyFunction · 0.70
formatMethod · 0.45

Tested by

no test coverage detected