I'm not quite sure what format you mean with gitlab; I've not seen char-by-char diffs in gitlab like your example. If you want a more standardish line-by-line output, then I think you just have to pass lists to the diff function:
for text in difflib.unified_diff(first.split("\n"), second.split("\n")):
if text[:3] not in ('+++', '---', '@@ '):
print text
As every line is different in your example, diff is just going to see each line as having been totally changed and give you an output like:
-def
-baz
+deff
+ba
+bar
+foo
If you want to do something more fancy, you can treat the data as a single string (as you were) and then try and split on new-lines. The return format seems to be "{operation}{char}"
(including new line chars), so you can group and detect lines which all have the same operation and apply the correct logic.
I can't quite work out the rules you're trying to apply based on your example (are you grouping all mixed lines, then added lines then removed lines or something else?), so I can't give you an exact example.