0

Comparing the following examples of using difflib.ndiff()

from difflib import unified_diff, ndiff

print("".join(ndiff(
    ["aba\n"],
    ["abbba\n"]
)))

print("".join(ndiff(
    ["aba\n"],
    ["abbbba\n"]
)))

Output:

- aba
+ abbba
?  ++

- aba
+ abbbba

The first points out where characters have to be added while the second one basically gave up and replaced the whole line.

How can I make the second one print out the equivalent of
"You need to insert 3 'b's [here]."?

Expected output for the second print:

- aba
+ abbbba
?  +++
Fabian N.
  • 3,807
  • 2
  • 23
  • 46

1 Answers1

1

The determination on whether to show that visual hilighting of differences is based on the match ratio of the two strings being 0.75 or higher. It's 0.80 for your first example, about 0.72 for your second that didn't get hilighted.

There is no provided way to adjust this threshold - but difflib is implemented in pure Python; you could copy difflib.py from the Python library and modify it to behave however you want. (In Python 2.7 at least, the threshold is near the top of a method named _fancy_replace.)

You could also fake out the existing difflib by padding all the strings with spaces, so that their match ratio is higher. For example, with 10 spaces added to both strings, you could go as far as comparing aba to abbbbbbbbbba and still get the difference hilighting.

jasonharper
  • 9,450
  • 2
  • 18
  • 42
  • Thank you very much for pointing me at `_fancy_replace` and I especially like the idea of faking the ration by adding space and removing them again. (I was going to just overwrite the `0.75` directly but that could become problematic with longer strings) – Fabian N. Apr 25 '17 at 22:52
  • Okay the space idea is to much of a hassle with multiple lines I'm just going to create a local copy of difflib.py – Fabian N. Apr 25 '17 at 23:06
  • I got it working. Colored difflib is finally doing what it should. See here if interested: https://github.com/Farbdose/ColoredDifflib – Fabian N. Apr 28 '17 at 11:59