Skip to content

Commit 41895cc

Browse files
committed
update with functionality to check if the key is not empty
Signed-off-by: Zen <[email protected]>
1 parent c322645 commit 41895cc

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/zenlib/util/check_dict.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
__author__ = "desultory"
2-
__version__ = "0.4.1"
2+
__version__ = "0.5.0"
33

44

55
from functools import wraps
66
from .walk_dict import walk_dict
77

88

9-
def check_dict(key, validate_dict=None, value=None, value_arg=None, contains=False, unset=False,
10-
raise_exception=False, log_level=10, return_val=False, return_arg=None, message=None):
9+
def check_dict(key, validate_dict=None, value=None, value_arg=None,
10+
contains=False, unset=False, not_empty=False,
11+
raise_exception=False, return_val=False, return_arg=None,
12+
log_level=10, message=None):
1113
"""
1214
Adds a check for a dict key to a function.
1315
If the dict is nto passed, uses the first argument of the function (often self).
@@ -53,10 +55,14 @@ def dispatch_msg(msg):
5355
return dispatch_msg("[%s] Key is set when it should be unset: %s." % (func.__name__, key))
5456
else:
5557
dict_val = validate_dict[key]
58+
if not_empty and dict_val:
59+
return dispatch_msg("[%s] Key is not empty: %s." % (func.__name__, key))
5660
elif isinstance(key, dict):
5761
if dict_val := walk_dict(validate_dict, key, fail_safe=not raise_exception):
5862
if unset:
5963
return dispatch_msg("[%s] Key is set when it should be unset: %s." % (func.__name__, dict_val))
64+
if not_empty and dict_val:
65+
return dispatch_msg("[%s] Key is not empty: %s." % (func.__name__, dict_val))
6066
elif not unset:
6167
raise ValueError("Unable to find key: %s." % key)
6268

0 commit comments

Comments
 (0)