Skip to content
Snippets Groups Projects
Commit 051d0577 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

add has_keys

parent 5c96eaa7
No related branches found
No related tags found
No related merge requests found
......@@ -4,3 +4,5 @@
* changed typing for `for_argument`
* added `source_to_function`
* added `clip`
* added predicate `has_keys`, deprecated
existing `has_keys`
......@@ -28,6 +28,7 @@ Predicates
.. autofunction:: satella.coding.predicates.not_equal
.. autofunction:: satella.coding.predicates.has_keys
Decorators
......@@ -57,3 +58,7 @@ Decorators are used to extend given predicates. Eg:
.. autofunction:: satella.coding.predicates.attribute
.. autofunction:: satella.coding.predicates.item
.. autofunction:: satella.coding.predicates.p_all
.. autofunction:: satella.coding.predicates.p_any
__version__ = '2.10.1_a5'
__version__ = '2.10.1_a6'
import queue
import typing as tp
import warnings
from satella.exceptions import PreconditionError
......@@ -172,6 +173,8 @@ def has_keys(keys: tp.List[str]):
:param keys: list of keys to expect
"""
warnings.warn('This is deprecated and will be removed in Satella 3.0. '
'Use satella.coding.predicates.has_keys instead', DeprecationWarning)
def inner(dictionary: dict) -> bool:
for key in keys:
......
......@@ -2,7 +2,8 @@ from .number import between
from .generic import one_of, equals, is_not_none, not_equal, has_attr
from .sequences import length_is, length_multiple_of, shorter_than, longer_than
from .decorators import attribute, item, p_all, p_any
from .dictionaries import has_keys
__all__ = ['between', 'one_of', 'length_is', 'shorter_than', 'length_multiple_of',
'equals', 'attribute', 'item', 'is_not_none', 'not_equal', 'longer_than',
'has_attr', 'p_all', 'p_any']
'has_attr', 'p_all', 'p_any', 'has_keys']
import typing as tp
def has_keys(*keys: tp.Any) -> tp.Callable[[tp.Dict], bool]:
"""
Return a predicate to check if your dictionary has all of given keys
"""
def predicate(v: tp.Dict) -> bool:
for key in keys:
if key not in v:
return False
return True
return predicate
......@@ -8,12 +8,12 @@ def one_of(*args) -> tp.Callable[[tp.Any], bool]:
:param args: a list of arguments on which the predicate will return True
:param attribute: if given, then it will first try to access given attribute of v
"""
def predicate(v):
def predicate(v) -> bool:
return v in args
return predicate
def _is_not_none(v):
def _is_not_none(v) -> bool:
return v is not None
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment