Skip to content

Commit 0a9ee5c

Browse files
committed
fix: allow to use / in the last keys
1 parent 6c02da4 commit 0a9ee5c

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="dict-path",
8-
version="1.0.2",
8+
version="1.1.1",
99
author="Muhamad Tohir",
1010
author_email="maztohir@gmail.com",
1111
description="Python library to work with complicated nested dict",

src/dict_path/__init__.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,14 @@ def get(self, path: str) -> Union[DictPath, Any]:
6666
return self
6767
current = self.data
6868
for attr in path:
69+
if current is None:
70+
return None
71+
6972
if isinstance(current, dict):
70-
print(current, attr)
7173
current = current.get(attr)
7274
elif isinstance(current, list):
73-
print(current, attr)
7475
current = current[int(attr)]
75-
elif current is None:
76-
return None
77-
else:
78-
raise Exception(f"Your path is not a path of dicts (value at key {attr} is of type {type(current)})")
76+
7977
if isinstance(current, dict):
8078
return DictPath(current)
8179
return current

test.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def test_inject_value():
1616
def test_extract_array():
1717
test_dict = {'foo1':{'foo2':[{'foo3':'bar'},{'foo4':[[{'foo5': 'bar'}]]}]}}
1818
assert extract_dict(test_dict, 'foo1/foo2/1/foo4/0/0/foo5') == 'bar'
19+
assert extract_dict(test_dict, 'foo1/foo2/1/foo4/0/0/foo5/') == 'bar'
1920

2021
test_dict = {'foo1':{'foo2':{'foo3':{'foo4':'bar'}}}}
2122
data = DictPath(test_dict)
@@ -57,9 +58,9 @@ def test_equalness_with_normal_dict():
5758
def test_extract_array_with_dict():
5859
test_dict = {'foo1':{'foo2':[{'foo3':'bar'},{'foo4':[[{'foo5': 'bar'}]]}]}}
5960
dic_path = DictPath(test_dict)
60-
assert dic_path.get('foo1/foo2/1/foo4/0/0/foo5') == 'bar'
61+
assert dic_path.get('foo1/foo2/1/foo4/0/0/foo5/') == 'bar'
6162

62-
def test_extract_array_with_dict():
63+
def test_extract_array_with_dict_none():
6364
test_dict = {'foo1':{'foo2':[{'foo3':'bar'},{'foo4':[[{'foo5': 'bar'}]]}]}}
6465
dic_path = DictPath(test_dict)
65-
assert dic_path.get('foo1/foo2/1/foo4/0/0/foo6/foo7') == None
66+
assert dic_path.get('foo1/foo2/1/foo4/0/0/foo6/foo7/') == None

0 commit comments

Comments
 (0)