Skip to content

Commit

Permalink
fun remove_paths; allow list of path keys
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Oct 9, 2017
1 parent 2eae20d commit 7a692bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion jsonextended/__init__.py
Expand Up @@ -91,7 +91,7 @@
"""

__version__ = '0.6.2'
__version__ = '0.6.3'

from jsonextended import ejson, units, utils, edict, plugins

Expand Down
11 changes: 8 additions & 3 deletions jsonextended/edict.py
Expand Up @@ -879,7 +879,7 @@ def is_in(a, b):
return unflatten(flatd, list_of_dicts=list_of_dicts)


def remove_paths(d, keys=None, list_of_dicts=False):
def remove_paths(d, keys, list_of_dicts=False):
""" remove paths containing certain keys from dict
Parameters
Expand All @@ -897,6 +897,11 @@ def remove_paths(d, keys=None, list_of_dicts=False):
>>> pprint(remove_paths(d,[6,'a']))
{2: {'b': 'B'}, 4: {5: {7: 'b'}}}
>>> d = {1:{2: 3}, 1:{4: 5}}
>>> pprint(remove_paths(d,[(1, 2)]))
{1: {4: 5}}
>>> d2 = {'a':[{'b':1,'c':{'b':3}},{'b':1,'c':2}]}
>>> pprint(remove_paths(d2,["b"],list_of_dicts=False))
{'a': [{'b': 1, 'c': {'b': 3}}, {'b': 1, 'c': 2}]}
Expand All @@ -905,12 +910,12 @@ def remove_paths(d, keys=None, list_of_dicts=False):
{'a': [{'c': 2}]}
"""
keys = [] if keys is None else keys
keys = [(key,) if not isinstance(key, tuple) else key for key in keys]
list_of_dicts = '__list__' if list_of_dicts else None

def contains(path):
for k in keys:
if k in path:
if set(k).issubset(path):
return True
return False

Expand Down

0 comments on commit 7a692bb

Please sign in to comment.