Skip to content

Commit

Permalink
Fix for non-existing keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio Salluzzo committed May 4, 2016
1 parent 1fc991c commit 415f727
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
24 changes: 23 additions & 1 deletion flask_breathalyzer/utils.py
Expand Up @@ -83,6 +83,22 @@ class NestedDict(dict):
Allows data access via extended slice notation.
http://stackoverflow.com/questions/15077973/how-can-i-access-a-deeply-nested-dictionary-using-tuples
"""
def __getitem__(self, keys):
# Let's assume *keys* is a list or tuple.
if isinstance(keys, (tuple, list)):
try:
node = self
for key in keys:
node = dict.__getitem__(node, key)
return node
except TypeError:
# *keys* is not a list or tuple.
pass
try:
return dict.__getitem__(self, keys)
except KeyError:
raise KeyError(keys)

def __setitem__(self, keys, value):
# Let's assume *keys* is a list or tuple.
if isinstance(keys, (tuple, list)):
Expand All @@ -101,5 +117,11 @@ def __setitem__(self, keys, value):
dict.__setitem__(self, keys, value)
nd = NestedDict(dic)
for l in lis:
nd[l.split(separator)[1:]] = value_to_replace
keys = l.split(separator)[1:]
try:
nd[keys]
except KeyError:
pass
else:
nd[keys] = value_to_replace
return nd
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -8,7 +8,7 @@

setup(
name='Flask-Breathalyzer',
version='0.2.1',
version='0.2.2',
license='BSD',
url='https://github.com/mindflayer/flask-breathalyzer',
author='Giorgio Salluzzo',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_breathalyzer.py
Expand Up @@ -115,6 +115,6 @@ def get_id(self):
def test_apply_blacklist():
v = 'foobar'
d1 = dict(a=1, b=dict(c=3, d=dict(e=4)))
d2 = apply_blacklist(d1, ('/a', '/b/d/e'), value_to_replace=v)
d2 = apply_blacklist(d1, ('/a', '/b/d/e', '/z'), value_to_replace=v)
assert d2 == dict(a=v, b=dict(c=3, d=dict(e=v)))
assert id(d1) != id(d2)

0 comments on commit 415f727

Please sign in to comment.