I am trying to make comprehensive diff that compares command line output of two programs. I used difflib and came up with this code:
from difflib import Differ
from pprint import pprint
import sys
def readable_whitespace(line):
return line.replace("\n", "\\n")
# Two strings are expected as input
def print_diff(text1, text2):
d = Differ()
text1 = text1.splitlines(True)
text2 = text2.splitlines(True)
text1 = [readable_whitespace(line) for line in text1]
text1 = [readable_whitespace(line) for line in text2]
result = list(d.compare(text1, text2))
sys.stdout.writelines(result)
sys.stdout.write("\n")
Some requirements I have:
- (obvious) It should be clear what is from which output when there is a difference
- New lines are replaced with
\nbecause they matter in my case and must be clearly visible when causing conflict
I made a simple test for my diff function:
A = "AAABAAA\n"
A += "BBB\n"
B = "AAAAAAA\n"
B += "\n"
B += "BBB"
print_diff(A,B)
For your convenience, here is test merged with the function so that you can execute it as file: http://pastebin.com/BvQw9naa
I have no idea what is this output trying to say to me:
- AAAAAAA\n? ^^
+ AAAAAAA
? ^
- \n+
BBB
Notice those two ^ symbols on first line? What are they pointing to...? Also, I intentionally put trailing new line into one test string. I don't think the diff noticed that.
How to make the output comprehensive or learn to understand it?