1 '''
说明:
2 1.原来没有 >> 新加入
3 2.原来有 >> 更新
4 3.新无,原来有 >> 删除原来的
5 得到三个列表:
6 要更新的数据
7 要删除的数据
8 要添加的数据
9 '''
10 # 数据库中原有
11 old_dict = {
12 "#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
13 "#2": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
14 "#3": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
15 }
16 # cmdb 新汇报的数据
17 new_dict = {
18 "#1": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 800},
19 "#3": {'hostname': 'c1', 'cpu_count': 2, 'mem_capicity': 80},
20 "#4": {'hostname': 'c2', 'cpu_count': 2, 'mem_capicity': 80},
21 }
22
23 old = set(old_dict)
24 new = set(new_dict)
25 #更新的数据(intersection交集)
26 update_data = old.intersection(new)
27 print(update_data)
28 #删除的数据(difference差集)
29 delete_data = old.difference(new)
30 #或者使用(symmetric_difference对称差分操作)
31 #delete_data = old.symmetric_difference(update_data)
32 print(delete_data)
33 #增加的数据
34 add_data = new.difference(old)
35 #或者使用(symmetric_difference对称差分操作)
36 #add_data = new.symmetric_difference(old)
37 print(update_data)