String comparison function in Python (alpha)


22nd of December 2007

I was working on a unittest which when it failed would say "this string != that string" and because some of these strings were very long (output of a HTML lib I wrote which spits out snippets of HTML code) it became hard to spot how they were different. So I decided to override the usual self.assertEqual(str1, str2) in Python's unittest class instance with this little baby:

 def assertEqualLongString(a, b):
    NOT, POINT = '-', '*'
    if a != b:
        print a
        o = ''
        for i, e in enumerate(a):
            try:
                if e != b[i]:
                    o += POINT
                else:
                    o += NOT
            except IndexError:
                o += '*'

        o += NOT * (len(a)-len(o))
        if len(b) > len(a):
            o += POINT* (len(b)-len(a))

        print o
        print b

        raise AssertionError, '(see string comparison above)'

It's far from perfect and doesn't really work when you've got Unicode characters that the terminal you use can't print properly. It might not look great on strings that are really really long but I'm sure that's something that can be solved too. After all, this is just a quick hack that helped me spot that the difference between one snippet and another was that one produced <br/> and the other produced <br />. Below are some examples of this utility function in action.

Beware, you can use this in many different ways that fits your need so I'm not going to focus on how it's executed:

 u = MyUnittest()
 u.assertEqualLongString('Peter Bengtsson 123', 'Peter PengtsXon 124'); print ""
 u.assertEqualLongString('Bengtsson','Bengtzzon'); print ""
 u.assertEqualLongString('Bengtsson','BengtzzonLonger'); print ""
 u.assertEqualLongString('BengtssonLonger','Bengtzzon'); print ""
 u.assertEqualLongString('Bengtssonism','Bengtsson'); print ""

 # Results:

 Peter Bengtsson 123
 ------*-----*-----*
 Peter PengtsXon 124

 Bengtsson
 -----**--
 Bengtzzon

 Bengtsson
 -----**--******
 BengtzzonLonger

 BengtssonLonger
 -----**--******
 Bengtzzon

 Bengtssonism
 ---------***
 Bengtsson



Comment

Show all 7 comments
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.