Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"