def html_diff(old, new):
 diff = SequenceMatcher(None, old, new)
 result = []
 for (tag, oldpos1, oldpos2, newpos1, newpos2) in diff.get_opcodes():
  if (tag == 'replace'):
   result.append(('<del>%s</del><ins>%s</ins>' % (old[oldpos1:oldpos2], new[newpos1:newpos2])))
  elif (tag == 'delete'):
   result.append(('<del>%s</del>' % old[oldpos1:oldpos2]))
  elif (tag == 'insert'):
   result.append(('<ins>%s</ins>' % new[newpos1:newpos2]))
  elif (tag == 'equal'):
   result.append(new[newpos1:newpos2])
 return ''.join(result)

def html_diff ( old , new ) : diff = SequenceMatcher ( None , old , new ) result = [ ] for ( tag , oldpos1 , oldpos2 , newpos1 , newpos2 ) in diff . get_opcodes ( ) : if ( tag == 'replace' ) : result . append ( ( '<del>%s</del><ins>%s</ins>' % ( old [ oldpos1 : oldpos2 ] , new [ newpos1 : newpos2 ] ) ) ) elif ( tag == 'delete' ) : result . append ( ( '<del>%s</del>' % old [ oldpos1 : oldpos2 ] ) ) elif ( tag == 'insert' ) : result . append ( ( '<ins>%s</ins>' % new [ newpos1 : newpos2 ] ) ) elif ( tag == 'equal' ) : result . append ( new [ newpos1 : newpos2 ] ) return '' . join ( result )
generates html formatted diff of two strings .