Method: difflib.unified_diff
Calls: 18, Exceptions: 3, Paths: 4Back
Path 1: 6 calls (0.33)
['hello', 'andr\udce9'] (3) 'one' (2) ['one', 'two', 'three', 'four'] (1)
['hello', 'andr\udcc3\udca9'] (3) 'two' (2) ['zero', 'one', 'tree', 'four'] (1)
'Original' (3) 'a' (2) '' (1)
'Current' (3) 'b' (2) '' (1)
'' (3) '2005-01-26 23:30:50' (2) '2005' (1)
'' (3) '2010-04-02 10:20:52' (2) '2013' (1)
3 (6)
'\n' (3) '' (3)
'@@ -1,2 +1,2 @@\n' (3) ' hello' (3) '-andr\udce9' (3) '+andr\udcc3\udca9' (3) '@@ -1,3 +1,3 @@' (2) '+t' (2) '+w' (2) ' o' (2) '-n' (2) '-e' (2)
1def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
2 tofiledate='', n=3, lineterm='\n'):
3 r"""
4 Compare two sequences of lines; generate the delta as a unified diff.
5
6 Unified diffs are a compact way of showing line changes and a few
7 lines of context. The number of context lines is set by 'n' which
8 defaults to three.
9
10 By default, the diff control lines (those with ---, +++, or @@) are
11 created with a trailing newline. This is helpful so that inputs
12 created from file.readlines() result in diffs that are suitable for
13 file.writelines() since both the inputs and outputs have trailing
14 newlines.
15
16 For inputs that do not have trailing newlines, set the lineterm
17 argument to "" so that the output will be uniformly newline free.
18
19 The unidiff format normally has a header for filenames and modification
20 times. Any or all of these may be specified using strings for
21 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
22 The modification times are normally expressed in the ISO 8601 format.
23
24 Example:
25
26 >>> for line in unified_diff('one two three four'.split(),
27 ... 'zero one tree four'.split(), 'Original', 'Current',
28 ... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
29 ... lineterm=''):
30 ... print(line) # doctest: +NORMALIZE_WHITESPACE
31 --- Original 2005-01-26 23:30:50
32 +++ Current 2010-04-02 10:20:52
33 @@ -1,4 +1,4 @@
34 +zero
35 one
36 -two
37 -three
38 +tree
39 four
40 """
41
42 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
43 started = False
44 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
45 if not started:
46 started = True
47 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
48 todate = '\t{}'.format(tofiledate) if tofiledate else ''
49 yield '--- {}{}{}'.format(fromfile, fromdate, lineterm)
50 yield '+++ {}{}{}'.format(tofile, todate, lineterm)
51
52 first, last = group[0], group[-1]
53 file1_range = _format_range_unified(first[1], last[2])
54 file2_range = _format_range_unified(first[3], last[4])
55 yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm)
56
57 for tag, i1, i2, j1, j2 in group:
58 if tag == 'equal':
59 for line in a[i1:i2]:
60 yield ' ' + line
61 continue
62 if tag in {'replace', 'delete'}:
63 for line in a[i1:i2]:
64 yield '-' + line
65 if tag in {'replace', 'insert'}:
66 for line in b[j1:j2]:
67 yield '+' + line
Path 2: 5 calls (0.28)
['\udca3odz is a city in Poland.'] (4) ['foo\n'] (1)
['\udcc5\udc81odz is a city in Poland.'] (4) ['bar\n'] (1)
'\udcb3odz.txt' (4) 'a' (1)
'\udcc5\udc82odz.txt' (4) 'b' (1)
'' (2) '2005-03-18' (2) '1 fév' (1)
'' (2) '2005-03-19' (2) '3 fév' (1)
3 (5)
'\n' (3) '' (2)
'-\udca3odz is a city in Poland.' (4) '+\udcc5\udc81odz is a city in Poland.' (4) '@@ -1 +1 @@\n' (3) '@@ -1 +1 @@' (2) '--- \udcb3odz.txt\n' (1) '+++...
1def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
2 tofiledate='', n=3, lineterm='\n'):
3 r"""
4 Compare two sequences of lines; generate the delta as a unified diff.
5
6 Unified diffs are a compact way of showing line changes and a few
7 lines of context. The number of context lines is set by 'n' which
8 defaults to three.
9
10 By default, the diff control lines (those with ---, +++, or @@) are
11 created with a trailing newline. This is helpful so that inputs
12 created from file.readlines() result in diffs that are suitable for
13 file.writelines() since both the inputs and outputs have trailing
14 newlines.
15
16 For inputs that do not have trailing newlines, set the lineterm
17 argument to "" so that the output will be uniformly newline free.
18
19 The unidiff format normally has a header for filenames and modification
20 times. Any or all of these may be specified using strings for
21 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
22 The modification times are normally expressed in the ISO 8601 format.
23
24 Example:
25
26 >>> for line in unified_diff('one two three four'.split(),
27 ... 'zero one tree four'.split(), 'Original', 'Current',
28 ... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
29 ... lineterm=''):
30 ... print(line) # doctest: +NORMALIZE_WHITESPACE
31 --- Original 2005-01-26 23:30:50
32 +++ Current 2010-04-02 10:20:52
33 @@ -1,4 +1,4 @@
34 +zero
35 one
36 -two
37 -three
38 +tree
39 four
40 """
41
42 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
43 started = False
44 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
45 if not started:
46 started = True
47 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
48 todate = '\t{}'.format(tofiledate) if tofiledate else ''
49 yield '--- {}{}{}'.format(fromfile, fromdate, lineterm)
50 yield '+++ {}{}{}'.format(tofile, todate, lineterm)
51
52 first, last = group[0], group[-1]
53 file1_range = _format_range_unified(first[1], last[2])
54 file2_range = _format_range_unified(first[3], last[4])
55 yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm)
56
57 for tag, i1, i2, j1, j2 in group:
58 if tag == 'equal':
59 for line in a[i1:i2]:
60 yield ' ' + line
61 continue
62 if tag in {'replace', 'delete'}:
63 for line in a[i1:i2]:
64 yield '-' + line
65 if tag in {'replace', 'insert'}:
66 for line in b[j1:j2]:
67 yield '+' + line
Path 3: 4 calls (0.22)
['hello', 'andr\udce9'] (3) [] (1)
['hello', 'andr\udce9'] (3) [] (1)
'' (2) 'a' (2)
'' (2) 'a' (2)
'' (3) '2005' (1)
'' (3) '2013' (1)
3 (4)
'\n' (4)
1def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
2 tofiledate='', n=3, lineterm='\n'):
3 r"""
4 Compare two sequences of lines; generate the delta as a unified diff.
5
6 Unified diffs are a compact way of showing line changes and a few
7 lines of context. The number of context lines is set by 'n' which
8 defaults to three.
9
10 By default, the diff control lines (those with ---, +++, or @@) are
11 created with a trailing newline. This is helpful so that inputs
12 created from file.readlines() result in diffs that are suitable for
13 file.writelines() since both the inputs and outputs have trailing
14 newlines.
15
16 For inputs that do not have trailing newlines, set the lineterm
17 argument to "" so that the output will be uniformly newline free.
18
19 The unidiff format normally has a header for filenames and modification
20 times. Any or all of these may be specified using strings for
21 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
22 The modification times are normally expressed in the ISO 8601 format.
23
24 Example:
25
26 >>> for line in unified_diff('one two three four'.split(),
27 ... 'zero one tree four'.split(), 'Original', 'Current',
28 ... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
29 ... lineterm=''):
30 ... print(line) # doctest: +NORMALIZE_WHITESPACE
31 --- Original 2005-01-26 23:30:50
32 +++ Current 2010-04-02 10:20:52
33 @@ -1,4 +1,4 @@
34 +zero
35 one
36 -two
37 -three
38 +tree
39 four
40 """
41
42 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
43 started = False
44 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
45 if not started:
46 started = True
47 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
48 todate = '\t{}'.format(tofiledate) if tofiledate else ''
49 yield '--- {}{}{}'.format(fromfile, fromdate, lineterm)
50 yield '+++ {}{}{}'.format(tofile, todate, lineterm)
51
52 first, last = group[0], group[-1]
53 file1_range = _format_range_unified(first[1], last[2])
54 file2_range = _format_range_unified(first[3], last[4])
55 yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm)
56
57 for tag, i1, i2, j1, j2 in group:
58 if tag == 'equal':
59 for line in a[i1:i2]:
60 yield ' ' + line
61 continue
62 if tag in {'replace', 'delete'}:
63 for line in a[i1:i2]:
64 yield '-' + line
65 if tag in {'replace', 'insert'}:
66 for line in b[j1:j2]:
67 yield '+' + line
Path 4: 3 calls (0.17)
list (1) ['hello'] (1) ['hello\n'] (1)
['hello'] (1) list (1) ['ohell\n'] (1)
'' (2) bytes (1)
'' (2) bytes (1)
'' (3)
'' (3)
3 (3)
'\n' (3)
TypeError (3)
1def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
2 tofiledate='', n=3, lineterm='\n'):
3 r"""
4 Compare two sequences of lines; generate the delta as a unified diff.
5
6 Unified diffs are a compact way of showing line changes and a few
7 lines of context. The number of context lines is set by 'n' which
8 defaults to three.
9
10 By default, the diff control lines (those with ---, +++, or @@) are
11 created with a trailing newline. This is helpful so that inputs
12 created from file.readlines() result in diffs that are suitable for
13 file.writelines() since both the inputs and outputs have trailing
14 newlines.
15
16 For inputs that do not have trailing newlines, set the lineterm
17 argument to "" so that the output will be uniformly newline free.
18
19 The unidiff format normally has a header for filenames and modification
20 times. Any or all of these may be specified using strings for
21 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
22 The modification times are normally expressed in the ISO 8601 format.
23
24 Example:
25
26 >>> for line in unified_diff('one two three four'.split(),
27 ... 'zero one tree four'.split(), 'Original', 'Current',
28 ... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
29 ... lineterm=''):
30 ... print(line) # doctest: +NORMALIZE_WHITESPACE
31 --- Original 2005-01-26 23:30:50
32 +++ Current 2010-04-02 10:20:52
33 @@ -1,4 +1,4 @@
34 +zero
35 one
36 -two
37 -three
38 +tree
39 four
40 """
41
42 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
43 started = False
44 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
45 if not started:
46 started = True
47 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
48 todate = '\t{}'.format(tofiledate) if tofiledate else ''
49 yield '--- {}{}{}'.format(fromfile, fromdate, lineterm)
50 yield '+++ {}{}{}'.format(tofile, todate, lineterm)
51
52 first, last = group[0], group[-1]
53 file1_range = _format_range_unified(first[1], last[2])
54 file2_range = _format_range_unified(first[3], last[4])
55 yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm)
56
57 for tag, i1, i2, j1, j2 in group:
58 if tag == 'equal':
59 for line in a[i1:i2]:
60 yield ' ' + line
61 continue
62 if tag in {'replace', 'delete'}:
63 for line in a[i1:i2]:
64 yield '-' + line
65 if tag in {'replace', 'insert'}:
66 for line in b[j1:j2]:
67 yield '+' + line