31 lines
749 B
Python
31 lines
749 B
Python
|
|
|
|
def patch(state,p):
|
|
for k in p.keys():
|
|
if p[k] is None:
|
|
del state[k]
|
|
elif k in state and isinstance(state[k], dict) and isinstance(p[k], dict):
|
|
patch(state[k], p[k])
|
|
else:
|
|
state[k] = p[k]
|
|
|
|
def diff(state, newstate):
|
|
d = {}
|
|
|
|
for k in state.keys():
|
|
if not k in newstate:
|
|
d[k] = None
|
|
|
|
for k in newstate.keys():
|
|
if k in state:
|
|
if isinstance(state[k], dict) and isinstance(newstate[k], dict):
|
|
dc = diff(state[k], newstate[k])
|
|
if dc:
|
|
d[k] = dc
|
|
elif state[k] != newstate[k]:
|
|
d[k] = newstate[k]
|
|
else:
|
|
d[k] = newstate[k]
|
|
|
|
return d
|