Skip to content

Commit

Permalink
move dict filter to utils collections
Browse files Browse the repository at this point in the history
  • Loading branch information
shcheklein committed May 28, 2019
1 parent 6728cea commit befb5ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dvc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def is_percent(val):
Returns:
bool: True if 0<=value<=100, False otherwise.
"""
return int(val) >= 0 and int(val) <= 100
return 0 <= int(val) <= 100


class Config(object): # pylint: disable=too-many-instance-attributes
Expand Down
30 changes: 3 additions & 27 deletions dvc/utils/checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

from __future__ import unicode_literals

from dvc.utils.compat import str, builtin_str, open
from dvc.utils.collections import dict_filter
from dvc.utils.compat import str, open

import os
import json
Expand Down Expand Up @@ -116,32 +117,7 @@ def bytes_checksum(byts, checksum_type=CHECKSUM_MD5):
return hasher.hexdigest()


def dict_filter(d, exclude=[]):
"""
Exclude specified keys from a nested dict
"""

if isinstance(d, list):
ret = []
for e in d:
ret.append(dict_filter(e, exclude))
return ret
elif isinstance(d, dict):
ret = {}
for k, v in d.items():
if isinstance(k, builtin_str):
k = str(k)

assert isinstance(k, str)
if k in exclude:
continue
ret[k] = dict_filter(v, exclude)
return ret

return d


def dict_checksum(d, exclude=[], checksum_type=CHECKSUM_MD5):
def dict_checksum(d, exclude=None, checksum_type=CHECKSUM_MD5):
filtered = dict_filter(d, exclude)
byts = json.dumps(filtered, sort_keys=True).encode("utf-8")
return bytes_checksum(byts, checksum_type)
31 changes: 30 additions & 1 deletion dvc/utils/collections.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals
from dvc.utils.compat import Mapping

from dvc.utils.compat import Mapping, builtin_str


# just simple check for Nones and emtpy strings
Expand Down Expand Up @@ -52,3 +53,31 @@ def is_same_type(a, b):
src.__class__.__name__, dest.__class__.__name__
)
)


def dict_filter(d, exclude=None):
"""
Exclude specified keys from a nested dict
"""

if not exclude:
return d

if isinstance(d, list):
ret = []
for e in d:
ret.append(dict_filter(e, exclude))
return ret
elif isinstance(d, dict):
ret = {}
for k, v in d.items():
if isinstance(k, builtin_str):
k = str(k)

assert isinstance(k, str)
if k in exclude:
continue
ret[k] = dict_filter(v, exclude)
return ret

return d

0 comments on commit befb5ee

Please sign in to comment.