Recursive word comparison program

I created a program that compares two python dictionaries and displays the differences between them. It works with a dict that has a depth of 2 or less. What should I do to be able to handle dicts with greater depth, also nested dicts?

Another problem I am facing is passing the json array through my get_json () function, which it returns as a list. And the program gets out with a list instead of a dict. How should I solve this?

My program:

#!/usr/bin/env python2

import json

def get_json():
    file_name = raw_input("Enter name of JSON File: ")
    with open(file_name) as json_file:
        json_data = json.load(json_file)
        return json_data

def print_diff(json1, json2):
    for n in json1:
        if n not in json2:
            print('-   "' + str(n) + '":')
    for n in json2:
        if n not in json1:
            print('+   "' + str(n) + '":')
            continue
        if json2[n] != json1[n]:
            if type(json2[n]) not in (dict, list):
                print('-   "' + str(n) + '" : "' + str(json1[n]))
                print('+   "' + str(n) + '" : "' + str(json2[n]))
            else:
                if type(json2[n]) == dict:
                    print_diff(json1[n], json2[n])
                    continue
    return


def main():
    file1 = get_json()
    print(type(file1))
    file2 = get_json()
    print(type(file2))
    print_diff(file1, file2)

if __name__ == "__main__":
    main()

dict 1 example:

{
    "widget": {
        "debug": "on",
        "window": {
            "title": "Sample Konfabulator Widget",
            "name": "main_window",
            "width": 500,
            "height": 500
        },
        "image": {
            "src": "Images/Sun.png",
            "name": "sun1",
            "hOffset": 250,
            "vOffset": 250,
            "alignment": "center"
        },
        "text": {
            "data": "Click Here",
            "size": 36,
            "style": "bold",
            "name": "text1",
            "hOffset": 250,
            "vOffset": 100,
            "alignment": "center",
            "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
    }
}

dict 2 example:

{
    "widget": {
        "debug": "on",
        "window": {
            "title": "Sample Konfabulator Widget",
            "name": "main_window",
            "width": 500,
            "height": 500
        },
        "image": {
            "src": "Images/Sun.png",
            "name": "sun2",
            "hOffset": 100,
            "vOffset": 100,
            "alignment": "center"
        },
        "text": {
            "data": "Click Here",
            "size": 36,
            "style": "bold",
            "name": "text1",
            "hOffset": 250,
            "vOffset": 100,
            "alignment": "center",
            "onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
        }
    }
}

Output Example:

Enter name of JSON File: JSON1.json
<type 'dict'>
Enter name of JSON File: JSON2.json
<type 'dict'>
-   "vOffset" : "250
+   "vOffset" : "100
-   "name" : "sun1
+   "name" : "sun2
-   "hOffset" : "250
+   "hOffset" : "100
+1
source share
2 answers

, . , , ( ). , , . , -.

def findDiff(d1, d2, path=""):
    for k in d1.keys():
        if not d2.has_key(k):
            print path, ":"
            print k + " as key not in d2", "\n"
        else:
            if type(d1[k]) is dict:
                if path == "":
                    path = k
                else:
                    path = path + "->" + k
                findDiff(d1[k],d2[k], path)
            else:
                if d1[k] != d2[k]:
                    print path, ":"
                    print " - ", k," : ", d1[k]
                    print " + ", k," : ", d2[k] 

print "comparing s1 to s2:"
print findDiff(s1,s2)
print "comparing s2 to s1:"
print findDiff(s2,s1)

::

comparing s1 to s2:
widget->text :
data as key not in d2 
widget->text->window->image :
 -  vOffset  :  250
 +  vOffset  :  100
widget->text->window->image :
 -  name  :  sun1
 +  name  :  sun2
widget->text->window->image :
 -  hOffset  :  250
 +  hOffset  :  100
None
comparing s2 to s1:
widget->text->window->image :
 -  vOffset  :  100
 +  vOffset  :  250
widget->text->window->image :
 -  name  :  sun2
 +  name  :  sun1
widget->text->window->image :
 -  hOffset  :  100
 +  hOffset  :  250
None

, , , . . widget- > text . , pls

+2

,

def check_dict(first, second):
    for key in first:
        for keyTwo in second:

            if type(first[key]) == dict and type(second[keyTwo]) == dict:
                return check_dict(first[key], second[keyTwo])
            if not first[key] == second[keyTwo]:
                return false

python , - . , , , , - .

0

Source: https://habr.com/ru/post/1525782/


All Articles