Skip to content

Commit

Permalink
feat(workflows): dict_pop function (#1048)
Browse files Browse the repository at this point in the history
Signed-off-by: Test <tal@keephq.dev>
  • Loading branch information
talboren authored Apr 3, 2024
1 parent 1e015b1 commit d90579d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
10 changes: 10 additions & 0 deletions keep/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import datetime
import json
import urllib.parse
Expand Down Expand Up @@ -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
13 changes: 13 additions & 0 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit d90579d

Please sign in to comment.