title: "diff -u -B -w" in python?
body: <p>Using Python, I'd like to output the difference between two strings as a unified diff (-u) while, optionally, ignoring blank lines (-B) and spaces (-w).</p>
  
  <p>Since the strings were generated internally, I'd prefer to not deal with nuanced complexity of writing one or both strings to a file, running GNU diff, fixing up the output, and finally cleaning up.</p>
  
  <p>While <a href="https://docs.python.org/3.4/library/difflib.html#difflib.unified_diff" rel="noreferrer">difflib.unified_diff</a> generates unified diffs it doesn't seem to let me tweak how spaces and blank lines are handled.  I've looked at its implementation and, I suspect, the only solution is to copy/hack that function's body.</p>
  
  <p>Is there anything better?</p>
  
  <p>For the moment I'm stripping the pad characters using something like:</p>
  
  <pre><code>import difflib
  import re
  import sys
  
  l = "line 1\nline 2\nline 3\n"
  r = "\nline 1\n\nline 2\nline3\n"
  strip_spaces = True
  strip_blank_lines = True
  
  if strip_spaces:
      l = re.sub(r"[ \t]+", r"", l)
      r = re.sub(r"[ \t]+", r"", r)
  if strip_blank_lines:
      l = re.sub(r"^\n", r"", re.sub(r"\n+", r"\n", l))
      r = re.sub(r"^\n", r"", re.sub(r"\n+", r"\n", r))
  # run diff
  diff = difflib.unified_diff(l.splitlines(keepends=True), r.splitlines(keepends=True))
  sys.stdout.writelines(list(diff))
  </code></pre>
  
  <p>which, of course, results in the output for a diff of something something other than the original input.  For instance, pass the above text to GNU diff 3.3 run as "diff -u -w" and "line 3" is displayed as part of the context, the above would display "line3".</p>