diff --git a/keep/functions/__init__.py b/keep/functions/__init__.py index 58686f2453..5bc598b2fa 100644 --- a/keep/functions/__init__.py +++ b/keep/functions/__init__.py @@ -1,3 +1,4 @@ +import copy import datetime import json import urllib.parse @@ -102,3 +103,12 @@ def slice(str_to_slice: str, start: int = 0, end: int = 0) -> str: if end == 0 or end == "0": return str_to_slice[int(start) :] return str_to_slice[int(start) : int(end)] + + +def dict_pop(data: str | dict, *args) -> dict: + if isinstance(data, str): + data = json.loads(data) + dict_copy = copy.deepcopy(data) + for arg in args: + dict_copy.pop(arg, None) + return dict_copy diff --git a/tests/test_functions.py b/tests/test_functions.py index d686b6e837..00d45ad8d9 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -227,9 +227,22 @@ def test_dict_to_key_value_list(): assert functions.dict_to_key_value_list({"a": 1, "b": "test"}) == ["a:1", "b:test"] +def test_dict_pop(): + d = {"a": 1, "b": 2} + d2 = functions.dict_pop(d, "a") + assert d2 == {"b": 2} + + +def test_dict_pop_str(): + d = '{"a": 1, "b": 2}' + d2 = functions.dict_pop(d, "a") + assert d2 == {"b": 2} + + def test_slice(): assert functions.slice("long string", 0, 4) == "long" def test_slice_no_end(): assert functions.slice("long string", 5) == "string" +