Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
Removed dict_subset
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackevansevo committed Jun 13, 2017
1 parent 5a3a71d commit 300736c
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 28 deletions.
16 changes: 0 additions & 16 deletions basic_utils/dict_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from basic_utils.seq_helpers import butlast, last

__all__ = [
'dict_subset',
'get_in_dict',
'get_keys',
'prune_dict',
Expand All @@ -26,21 +25,6 @@ def get_keys(d: dict, keys: Sequence[Any], default: Callable=None) -> Tuple:
return tuple(d.get(key, default) for key in keys)


def dict_subset(d, keys, prune=False, default=None):
# type: (dict, Sequence[str], bool, Callable) -> dict
"""
Returns a new dictionary with a subset of key value pairs from the original
>>> d = {'a': 1, 'b': 2}
>>> dict_subset(d, ('c',), True, 'missing')
{'c': 'missing'}
"""
new = {k: d.get(k, default) for k in keys}
if prune:
return prune_dict(new)
return new


def get_in_dict(d: dict, keys: Sequence[str]) -> Any:
"""
Retrieve nested key from dictionary
Expand Down
12 changes: 0 additions & 12 deletions tests/test_dict_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest # type: ignore

from basic_utils.dict_helpers import (
dict_subset,
filter_keys,
filter_values,
get_in_dict,
Expand All @@ -16,17 +15,6 @@
from basic_utils.primitives import even


@pytest.mark.parametrize("args, expected", [
((('a', 'b', 'c'), False, None), {'a': 1, 'b': 2, 'c': 3}),
((('a', 'b', 'z'), True, None), {'a': 1, 'b': 2}),
((('a', 'b', 'z'), False, None), {'a': 1, 'b': 2, 'z': None}),
((('a', 'b', 'z'), False, 'Missing'), {'a': 1, 'b': 2, 'z': 'Missing'}),
])
def test_dict_subset(args: Any, expected: Any) -> None:
d = {'a': 1, 'b': 2, 'c': 3}
assert dict_subset(d, *args) == expected


@pytest.mark.parametrize("data, keys, expected", [
({'a': {'b': {'c': 3}}}, 'a', {'b': {'c': 3}}),
({'a': {'b': {'c': 3}}}, ('a', 'b', 'c'), 3)
Expand Down

0 comments on commit 300736c

Please sign in to comment.