Skip to content

Commit

Permalink
Merge pull request #38 from mschmidt87/bug/deeper_level_blacklist
Browse files Browse the repository at this point in the history
Fix two bugs with nested blacklists
  • Loading branch information
jakobj authored Jun 8, 2018
2 parents 0bbf81d + dde57c1 commit 286a8b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
18 changes: 12 additions & 6 deletions dicthash/dicthash.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ def _generate_string_from_dict(d, blacklist, whitelist, prefix=''):
if blacklist is not None:
whitelist = set(whitelist).difference(blacklist)
# Sort whitelist according to the keys converted to str
return ''.join(_unpack_value(d[key],
whitelist=filter_blackwhitelist(whitelist, key),
blacklist=filter_blackwhitelist(blacklist, key),
prefix=prefix + str(key)) for
key in sorted(filter_blackwhitelist(whitelist, None), key=str))
if len(whitelist) > 0:
return ''.join(_unpack_value(d[key],
whitelist=filter_blackwhitelist(whitelist, key),
blacklist=filter_blackwhitelist(blacklist, key),
prefix=prefix + str(key)) for
key in sorted(filter_blackwhitelist(whitelist, None), key=str))
else:
return ''


def generate_hash_from_dict(d, blacklist=None, whitelist=None,
Expand Down Expand Up @@ -243,7 +246,10 @@ def filter_blackwhitelist(l, key):
for k in l:
if isinstance(k, tuple):
if key is not None and k[0] == key:
fl.append(k[1])
if len(k) == 2:
fl.append(k[1])
else:
fl.append(k[1:])
elif key is None:
fl.append(k[0])
elif key is None:
Expand Down
20 changes: 20 additions & 0 deletions dicthash/test/test_dicthash.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ def test_blacklist():
assert(hash0 != hash1)


def test_blacklist_all_keys():
d0 = {'a': 1,
'b': 2}
d1 = {}

hash0 = dicthash.generate_hash_from_dict(d0, blacklist=['a', 'b'])
hash1 = dicthash.generate_hash_from_dict(d1)

assert(hash0 == hash1)


def test_whitelist():
d0 = {
'a': [1, 2, 3],
Expand Down Expand Up @@ -346,6 +357,15 @@ def test_subdir_keys_for_whitelist_blacklist():
label1 = dicthash.generate_hash_from_dict(d1, whitelist=[('a', 'b')])
assert(label0 == label1)

# Test with one more level in the dictionaries
d0 = {'e': {'f': {'g': 2,
'h': 4}}}
d1 = {'e': {'f': {'g': 3,
'h': 4}}}
label0 = dicthash.generate_hash_from_dict(d0, blacklist=[('e', 'f', 'h')])
label1 = dicthash.generate_hash_from_dict(d1, blacklist=[('e', 'f', 'h')])
assert(label0 != label1)


def test_dict_list_lead_to_different_hash():
d0 = {
Expand Down

0 comments on commit 286a8b3

Please sign in to comment.