Random notes from mg

a blog by Marius Gedminas

Marius is a Python hacker. He works for Programmers of Vilnius, a small Python/Zope 3 startup. He has a personal home page at http://gedmin.as. His email is marius@gedmin.as. He does not like spam, but is not afraid of it.

Sat, 11 Dec 2004

Diffing dicts

Say you are comparing two large dicts in a unit test, for example:

    form = extract_form(rendered_html)
    self.assertEquals(form, {'field1': u'value1',
                             'field2': u'value2',
                             ...
                             'field42': u'value42'})

When this test fails, a useful trick is to ask the test runner to drop into Pdb inside assertEquals (SchoolTool and Zope 3 test runners have a command line option -d for this) and type the following:

(Pdb) from sets import Set
(Pdb) pp list(Set(first.items()) ^ Set(second.items()))

You will get a list of (key, value) pairs that differ:

[('field.comp.c3.b.NEW', u''),
 ('field.comp.c1.b.NEW', u''),
 ('field.comp.c1.title', 'New stuff'),
 ('field.comp.c1.b.b2', u'A2'),
 ('field.comp.c3.b.b1', u'New behaviour'),
 ('field.comp.c3.title', u'New stuff'),
 ('SUBMIT', u'Save'),
 ('field.comp.c1.title', u'Comp 1'),
 ('field.comp.c2.b.b1', 'B1'),
 ('field.comp.c3.description', u'New description'),
 ('field.comp.c2.description', 'Comp two'),
 ('field.comp.c1.b.b1', u'A1'),
 ('field.comp.c2.title', 'Comp 2'),
 ('SUBMIT', 'Submit'),
 ('field.comp.c2.b.b2', 'B2'),
 ('field.comp.c1.description', 'New description'),
 ('field.comp.c1.description', u'Comp one'),
 ('field.comp.c1.b.b1', 'New behaviour')]

If there are many differences, sorting the list is a good idea

(Pdb) sorted = lambda l: (l.sort(), l)[1]
(Pdb) pp sorted(list(Set(first.items()) ^ Set(second.items())))

Python 2.4 makes this simpler (builtin set, builtin sorted).

posted at 12:46 | tags: | permanent link to this entry | 0 comments

Name (required)


E-mail (will not be shown)


URL


Comment (some HTML allowed)