From ece41f5f971595c5d005c4beaa984c45471a6647 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Wed, 2 Jun 2021 07:54:35 +0300 Subject: Get the diff between 2 dicts and return that as a dict --- gn3/db/__init__.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'gn3') diff --git a/gn3/db/__init__.py b/gn3/db/__init__.py index 1eb7b12..19135fc 100644 --- a/gn3/db/__init__.py +++ b/gn3/db/__init__.py @@ -68,3 +68,19 @@ def fetchone(conn: Any, with conn.cursor() as cursor: cursor.execute(sql) return DATACLASSMAP[table](*cursor.fetchone()) + + +def diff_from_dict(old: Dict, new: Dict) -> Dict: + """Construct a new dict with a specific structure that contains the difference +between the 2 dicts in the structure: + +diff_from_dict({"id": 1, "data": "a"}, {"id": 2, "data": "b"}) + +Should return: + +{"id": {"old": 1, "new": 2}, "data": {"old": "a", "new": "b"}} + """ + dict_ = {} + for key, value in old.items(): + dict_[key] = {"old": old[key], "new": new[key]} + return dict_ -- cgit v1.2.3