Comparing Dictionaries

Kenneth Love klove at tax.ok.gov
Thu Jul 26 23:18:46 CEST 2007


Hello,

I am new to Python, but not programming.  I would like to start my
Python career by developing programs according to the "best practices"
of the industry.  Right now, that appears to be unit tests, patterns,
and source code control.

So, I am trying to write a unit test for some code that reads a Windows
style INI file and stores the section, key, and values in a dictionary
(recipe originally found on ASPN, but heavily modified for my needs).

------------------------------
class LoadIni(unittest.TestCase):
     knownDefault = {
         "database$dbms":            "mysql",
         "database$name":            "thelarch",
         "database$user":            "nobody",
         "database$password":        "special",
         "database$host":            "127.0.0.1"
         }

     def testNoIniFilename(self):
         """A default configuration list should be returned."""
         result = ini.load()
         # These next two print statement show the same key/value pairs,
         # but in a different order.
         #print knownDefault
         #print result
         self.assertEqual(result, self.knownDefault)  # does not work
-------------------------------

In this case, I need to compare the returned dictionary (result) with
the known default dictionary (knownDefault).  This needs to be a
comparison of the dictionary key/value pairs.  If all of the key/value
pairs are an exact match and no extra key/value pairs are in either
dictionary then I consider the two dictionaries to be equivalent.

In other words, I consider these two dictionaries to be equivalent:

     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'mickey' }
     { 'mouse' : 'mickey', 'dog' : 'bone', 'cat' : 'fever' }

while these two are not:

     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'mickey' }
     { 'dog' : 'bone', 'cat' : 'fever', 'mouse' : 'michael' }

Any suggestions on how to compare these via some assert statement?

Am I even on the right track with the INI file storing results in a
dictionary?  When I found the code, it just "felt right".

NOTE: The code works, I can load the INI file, programmatically change
a few settings, save the results to a new INI file, and manually compare
the resulting files.  I get the expected results.  It is the automation
of this testing process that appears to be the stumbling block.

adTHANKSvance,
Kenneth Love


-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Kenneth Love                   |         Oklahoma Tax Commission
DP Programmer/Analyst          |         Information Technology
(405) 522 - 5864               |         http://www.tax.ok.gov/
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- 





More information about the Python-list mailing list