MCPcopy Index your code
hub / github.com/python/cpython / unified_diff

Function unified_diff

Lib/difflib.py:1101–1176  ·  view source on GitHub ↗

r""" Compare two sequences of lines; generate the delta as a unified diff. Unified 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 with ---,

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

Source from the content-addressed store, hash-verified

1099 return '{},{}'.format(beginning, length)
1100
1101def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
1102 tofiledate='', n=3, lineterm='\n', *, color=False):
1103 r"""
1104 Compare two sequences of lines; generate the delta as a unified diff.
1105
1106 Unified diffs are a compact way of showing line changes and a few
1107 lines of context. The number of context lines is set by 'n' which
1108 defaults to three.
1109
1110 By default, the diff control lines (those with ---, +++, or @@) are
1111 created with a trailing newline. This is helpful so that inputs
1112 created from file.readlines() result in diffs that are suitable for
1113 file.writelines() since both the inputs and outputs have trailing
1114 newlines.
1115
1116 For inputs that do not have trailing newlines, set the lineterm
1117 argument to "" so that the output will be uniformly newline free.
1118
1119 Set 'color' to True to enable output in color, similar to
1120 'git diff --color'. Even if enabled, it can be
1121 controlled using environment variables such as 'NO_COLOR'.
1122
1123 The unidiff format normally has a header for filenames and modification
1124 times. Any or all of these may be specified using strings for
1125 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
1126 The modification times are normally expressed in the ISO 8601 format.
1127
1128 Example:
1129
1130 >>> for line in unified_diff('one two three four'.split(),
1131 ... 'zero one tree four'.split(), 'Original', 'Current',
1132 ... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
1133 ... lineterm=''):
1134 ... print(line) # doctest: +NORMALIZE_WHITESPACE
1135 --- Original 2005-01-26 23:30:50
1136 +++ Current 2010-04-02 10:20:52
1137 @@ -1,4 +1,4 @@
1138 +zero
1139 one
1140 -two
1141 -three
1142 +tree
1143 four
1144 """
1145
1146 if color and can_colorize():
1147 t = get_theme(force_color=True).difflib
1148 else:
1149 t = get_theme(force_no_color=True).difflib
1150
1151 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
1152 started = False
1153 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
1154 if not started:
1155 started = True
1156 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
1157 todate = '\t{}'.format(tofiledate) if tofiledate else ''
1158 yield f'{t.header}--- {fromfile}{fromdate}{lineterm}{t.reset}'

Callers 1

test_cprofileMethod · 0.90

Calls 7

can_colorizeFunction · 0.90
get_themeFunction · 0.90
_check_typesFunction · 0.85
SequenceMatcherClass · 0.85
_format_range_unifiedFunction · 0.85
get_grouped_opcodesMethod · 0.80
formatMethod · 0.45

Tested by 1

test_cprofileMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…