Consider the two text files file_1_1.txt and file_1_2.txt:
file_1_1.txt
1 A line to delete: 1
2 A line to delete: 2
3 A line that stays: 1
4 A line that stays: 2
5 A line to change: 1
6 A line that stays: 3
7 A line that stays: 4
8 A line that stays: 5
9 A line that stays: 6
10 A line to delete: 3
11 A line that stays: 7
12 A line that stays: 8
13 A line to change: 2
14 A line to change: 3
15 A line to change: 4
16 A line to change: 5
17 A line that stays: 9
file_1_2.txt
1 A line that stays: 1
2 A line to insert: 1
3 A line that stays: 2
4 A changed line: 1
5 A line that stays: 3
6 A line that stays: 4
7 A line to insert: 2
8 A line to insert: 3
9 A line that stays: 5
10 A line that stays: 6
11 A line that stays: 7
12 A line that stays: 8
13 A changed line: 2
14 A changed line: 3
15 A changed line: 4
16 A line that stays: 9
The Unix diff command can be applied to file_1_1.txt and file_1_2.txt:
$ diff file_1_1.txt file_1_2.txt
1,2d0
< A line to delete: 1
< A line to delete: 2
3a2
> A line to insert: 1
5c4
< A line to change: 1
---
> A changed line: 1
7a7,8
> A line to insert: 2
> A line to insert: 3
10d10
< A line to delete: 3
13,16c13,15
< A line to change: 2
< A line to change: 3
< A line to change: 4
< A line to change: 5
---
> A changed line: 2
> A changed line: 3
> A changed line: 4
Study this example to understand diff’s output.
- d is for delete, for commands of the form l_1dl_2 with l_1 and l_2 two line numbers, or of the form l_1,l_2dl_3 with l_1, l_2 and l_3 three line numbers.
- a is for add, for commands of the form l_1al_2 with l_1 and l_2 two line numbers, or of the form l_1al_2,l_3 with l_1, l_2 and l_3 three line numbers.
- c is for change, for commands of the form l_1cl_2 with l_1 and l_2 two line numbers, or of the form l_1,l_2cl_3 with l_1, l_2 and l_3 three line numbers, or of the form l_1cl_2,l_3 with l_1, l_2 and l_3 three line numbers, or of the form l_1,l_2cl_3,l_4 with l_1, l_2, l_3 and l_4 four line numbers.
diff identifies a longest common subsequence (LCS) of lines that are common to both files. It then computes
the unique minimal set of commands that allows one to convert the first file into the second one (the ed editorcan actually precisely do this). With the previous example, there is a unique LCS, consisting of the lines:
A line that stays: 1, A line that stays: 2, A line that stays: 3 and A line that stays: 4
Still the LCS is not always unique. Consider for instance the two text files file_2_1.txt and file_2_2.txt:
file_2_1.txt
1 A line
file_2_2.txt
1 A line
2 A line
There are then 2 LCSs, as one can chose from file_2_2.txt either the first or the second line. Unix diff determines one LCS and yields the corresponding output:
$ diff file_2_1.txt file_2_2.txt
1a2
> A line
But another implementation could have output instead:
$ diff file_2_1.txt file_2_2.txt
0a1
> A line
For another example, consider the two text files file_3_1.txt and file_3_2.txt:
file_3_1.txt
1 Line 1
2 Line 2
3 A line to go
4 A line to go
5 A line to go
6 Line 3
7 Line 4
8 A line to go
file_3_2.txt
1 A line to come
2 A line to come
3 Line 1
4 Line 2
5 A line to come
6 A line to come
7 Line 1
8 Line 2
9 A line to come
10 A line to come
11 A line to come
12 Line 3
13 Line 45
There are then 3 LCSs, as the unique occurrences of Line 1 and Line 2 in the first file can be matched with the first occurrences of Line 1 and Line 2 in the second file, or with the first occurrence of Line 1 and the second occurrence of Line 2 in the second file, or with the second occurrences of Line 1 and Line 2 in the second file.
Unix diff determines one LCS and yields the corresponding output:
$ diff file_3_1.txt file_3_2.txt
0a1,2
> A line to come
> A line to come
3,5c5,11
< A line to go
< A line to go
< A line to go
---
> A line to come
> A line to come
> Line 1
> Line 2
> A line to come
> A line to come
> A line to come
8d13
< A line to go
But another implementation could have output instead either
0a1,2
> A line to come
> A line to come
1a4,7
> Line 2
> A line to come
> A line to come
> Line 1
3,5c9,11
< A line to go
< A line to go
< A line to go
---
> A line to come
> A line to come
> A line to come
8d13
< A line to go6
or
0a1,6
> A line to come
> A line to come
> Line 1
> Line 2
> A line to come
> A line to come
3,5c9,11
< A line to go
< A line to go
< A line to go
---
> A line to come
> A line to come
> A line to come
8d13
< A line to go
One could want to output the LCS, common to both files, with ... for longest nonempty sequences of lines that are not part of the LCS, w.r.t. the first file, or w.r.t. the second file.
For the Unix diff applied to file_1_1.txt and file_1_2.txt, that would be, w.r.t. file_1_1.txt:
...
A line that stays: 1
A line that stays: 2
...
A line that stays: 3
A line that stays: 4
A line that stays: 5
A line that stays: 6
...
A line that stays: 7
A line that stays: 8
...
A line that stays: 97
and w.r.t. file_1_2.txt:
A line that stays: 1
...
A line that stays: 2
...
A line that stays: 3
A line that stays: 4
...
A line that stays: 5
A line that stays: 6
A line that stays: 7
A line that stays: 8
...
A line that stays: 9
For the Unix diff applied to file_2_1.txt and file_2_2.txt, that would be, w.r.t. file_2_1.txt:
A line
and w.r.t. file_2_2.txt:
A line
...
For the Unix diff applied to file_3_1.txt and file_3_2.txt, that would be, w.r.t. file_3_1.txt:
Line 1
Line 2
...
Line 3
Line 4
...
and w.r.t. file_3_2.txt:
...
Line 1
Line 2
...
Line 3
Line 4
Task specifications
Write a program stored in a file named diff.py that implements three classes,
• DiffCommands,
• DiffCommandsError
• OriginalNewFiles
the second one deriving from Exception.
DiffCommands does not provide any method in its public interface. It builds a DiffCommands object from a text file meant to store a plausible sequence of diff commands, one command per line, without any space on any line, and without any extra line. In case the text file does not satisfy those conditions, the __init__() method of DiffCommands raises a DiffCommandsError error with as message "Cannot possibly be the commands for the diff of two files"
Seven files of this type are provided (wrong_1.txt to wrong_7.txt). Three files from which you can build a DiffCommands object are provided:
- diff_1.txt, which stores the only possible sequence of diff commands for the sample files file_1_1.txt and file_1_2.txt;
- diff_2.txt, which stores one of the two possible sequences of diff commands for the sample files file_2_1.txt and file_2_2.txt;
- diff_3.txt, which stores one of the three possible sequences of diff commands for the sample files file_3_1.txt and file_3_2.txt.
DiffCommands implements the __str__() function so that print() can be used to output the diff commands of the object under consideration.
OriginalNewFiles provides a user interface with 4 methods:
• output_diff()
• output_unmodified_from_original()
• output_unmodified_from_new()
• get_all_diff_commands()
The following example of interaction shows how the methods are supposed to be called and what they are supposed to return. The only point which might not be obvious from that example is that in the sequence of diff commands output by the method OriginalNewFiles.pair_of_files.get_all_diff_commands()
the diff commands are lexicographically ordered.
Solution
Code structure should like this:
Let's focus on how to implement function get_all_diff_commands
Example
pair_of_files = OriginalNewFiles('file_3_2.txt', 'file_3_1.txt')
diffs = pair_of_files.get_all_diff_commands()
- Create a tree as follow:
- get leaves with max level
- then get all LCS
- transfer into commands
- sort lexicographically
Link: Attachments password: 4sa9
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。