From 076ae8866de4e8b0cccbe92235a8a06a2bf4f90d Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 19 Oct 2024 10:48:21 +0200 Subject: [PATCH] added jay_diff --- jay_diff.py | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 jay_diff.py diff --git a/jay_diff.py b/jay_diff.py new file mode 100644 index 0000000..c8cd26d --- /dev/null +++ b/jay_diff.py @@ -0,0 +1,156 @@ +from copy import deepcopy + +def jay_diff_rev(data, temp_data): + new_data = {} + for k in data.keys(): + if k not in temp_data: + temp_data[k] = data[k] + new_data[k] = data[k] + if isinstance(temp_data[k], dict): + new_data, temp_data = jay_diff_rev(data[k], temp_data[k]) + return new_data, temp_data + + +def jay_diff_plus(data, temp_data): + temp = deepcopy(temp_data) + res, _ = jay_diff_rev(data, temp) + return res + + +def jay_diff_minus(data, temp_data): + temp = deepcopy(data) + res, _ = jay_diff_rev(temp_data, temp) + return res + + +def jay_diff_upd(data, temp_data): + new_data = {} + for k in data.keys(): + if k in temp_data: + if data[k] != temp_data[k]: + new_data[k] = data[k] + return new_data + + +def jay_diff_full(data, temp_data): + update = jay_diff_upd(data, temp_data) + plus = jay_diff_plus(data, temp_data) + minus = jay_diff_minus(data, temp_data) + return {'diff': {'plus': plus, 'minus': minus, 'update': update}} + + +def jay_merge_update(d, u): + for k, v in u.items(): + if isinstance(v, dict): + r = jay_merge_update(d.get(k, {}), v) + d[k] = r + else: + d[k] = u[k] + return d + + +def jay_merge_plus(d, u): + for k, v in u.items(): + if isinstance(v, dict): + if k in d: + d[k].update(v) + else: + d[k] = v + else: + d[k] = u[k] + return d + + +def jay_merge_minus(d, u): + for k, v in u.items(): + if isinstance(v, dict): + jay_merge_minus(d[k], u[k]) + else: + if k in d: + del d[k] + return d + +if __name__ == '__main__': + + a = {'a':'hamburger', 'b':'fries', 'c':'coke'} + b = {'a':'hamburger', 'b':'fries', 'c':'pepsi'} + c = {'a':'hamburger', 'b':'fries', 'c':'pepsi', 'd':'icecream'} + d = {'a':'hamburger', 'c':'pepsi', 'd':'icecream'} + e = {'a':'HotDog', 'b':'fries', 'd':'icecream'} + + last = {} + diffed = jay_diff_plus(a, {}) + print(f"a: Plus: {diffed}") + + diffed = jay_diff_plus(b, a) + print(f"b: Plus: {diffed}") + + diffed = jay_diff_plus(c, b) + print(f"c: Plus: {diffed}") + + diffed = jay_diff_plus(d, c) + print(f"d: Plus: {diffed}") + + diffed = jay_diff_minus(a, {}) + print(f"a: Minus: {diffed}") + + diffed = jay_diff_minus(b, a) + print(f"b: Minus: {diffed}") + + diffed = jay_diff_minus(c, b) + print(f"a: Minus: {diffed}") + + diffed = jay_diff_minus(d, c) + print(f"d: Minus: {diffed}") + + diffed = jay_diff_upd(a, {}) + print(f"a: Update: {diffed}") + + diffed = jay_diff_upd(b, a) + print(f"b: Update: {diffed}") + + diffed = jay_diff_upd(c, b) + print(f"a: Update: {diffed}") + + diffed = jay_diff_upd(d, c) + print(f"d: Update: {diffed}") + + diffed = jay_diff_full(a, {}) + print(f"a: Full: {diffed}") + + diffed = jay_diff_full(b, a) + print(f"b: Full: {diffed}") + + diffed = jay_diff_full(c, b) + print(f"c: Full: {diffed}") + + diffed = jay_diff_full(d, c) + print(f"d: Full: {diffed}") + + diffed = jay_diff_full(e, d) + print(f"e: Full: {diffed}") + + + a = {'a':'hamburger', 'b':'fries', 'c': {'e': 'coke', 'f': 'coffee'}} + b = {'b':'fries', 'c':'pepsi'} + c = {'d':'icecream'} + d = {'c': {'f': 'coffee'}} + + merged = jay_merge_plus({}, a) + print(f"a: Plus: {merged}") + + merged = jay_merge_plus(merged, c) + print(f"c: Plus: {merged}") + + merged = jay_merge_minus(merged, c) + print(f"c: Minus: {merged}") + + merged = jay_merge_minus(merged, d) + print(f"d: Minus: {merged}") + + merged = jay_merge_plus(merged, d) + print(f"d: Plus: {merged}") + + c = {'a':'hamburger', 'b':'fries', 'c': {'e': 'coke', 'f': 'coffee'}, 'd':'icecream'} + d = {'a':'hamburger', 'c':'pepsi', 'd':'icecream'} + e = {'a':'HotDog', 'b':'fries', 'd':'icecream'}