1

I'm not the greatest at python, i have been teaching myself slowly on the side so please excuse my lack of knowledge. This code is a slightly modified version of the code found here (Recursive diff of two python dictionaries (keys and values))

I have a few questions about the function listed in its comments.

def dd(d1, d2): 
    for k in d1: 
        if k not in d2: ##if the key from dict1 is not in dict2
            print k + " removed from d2"
    for k in d2:
        if k not in d1: ##if the key from dict2 is not in dict 1
            print k + " added in d2"
            continue
        if d2[k] != d1[k]: ## if dict 2 values are not the same as the values in dict 1
            if type(d2[k]) not in (dict, list): ## ???
                print k + " changed in d2 to " + str(d2[k])
            else:
                if type(d1[k]) != type(d2[k]): ## ???
                    print k + " changed to " + str(d2[k])
                    continue
                else:
                    if type(d2[k]) == dict: ## if dict2 values are in dict format?? 
                        dd(d1[k], d2[k]) ## pass dict1 / dict 2 values through the function again,  what is the purpose of this?
                        continue
    return

I also have another question regarding the output of the code. If I use the following code as d1.

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": [
                            "GML",
                            "XML"
                        ]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

And change the value of ID to "1234" and use that file as d2. The dd(d1,d2) will output:

"ID changed in d2 to 1234"

Which is exactly what i would expect from the code (key + "changed in d2 to " + value)

I want to try to change the output to something similar to when you update a file on github, so its easier to see what was changed and where such as:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
-                   "ID": "SGML",
+                   "ID": "1234",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": [
                            "GML",
                            "XML"
                        ]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

I would have to take the two dictionaries, and compare the two but i have no idea how to approach the output.

Community
  • 1
  • 1
user3230554
  • 81
  • 1
  • 8

1 Answers1

0

Try looking at 'difflib' (http://docs.python.org/2/library/difflib.html) You can run the comparison on a string / pretty-print representation of the dictionaries.

Baruch Oxman
  • 1,616
  • 14
  • 24
  • Might sound stupid, but how do i use the difflib to compare on two strings? I tried "result = list(difflib.Differ.compare(json1_str, json2_str))" but its giving me errors. – user3230554 Jan 25 '14 at 03:11
  • See if the following works for you: `code` list(difflib.ndiff(a, b)) – Baruch Oxman Jan 25 '14 at 13:59