Skip to content

Commit 58d4859

Browse files
committed
Adding parse_path
1 parent 888ca77 commit 58d4859

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

deepdiff/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
from .search import DeepSearch, grep
1212
from .deephash import DeepHash
1313
from .delta import Delta
14-
from .path import extract
14+
from .path import extract, parse_path

deepdiff/path.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,41 @@ def extract(obj, path):
185185
"""
186186
elements = _path_to_elements(path, root_element=None)
187187
return _get_nested_obj(obj, elements)
188+
189+
190+
def parse_path(path, root_element=DEFAULT_FIRST_ELEMENT, include_actions=False):
191+
"""
192+
Parse a path to a format that is machine readable
193+
194+
**Parameters**
195+
196+
path : A string
197+
The path string such as "root[1][2]['age']"
198+
199+
root_element: string, default='root'
200+
What the root is called in the path.
201+
202+
include_actions: boolean, default=False
203+
If True, we return the action required to retrieve the item at each element of the path.
204+
205+
**Examples**
206+
207+
>>> from deepdiff import parse_path
208+
>>> parse_path("root[1][2]['age']")
209+
[1, 2, 'age']
210+
>>> parse_path("root[1][2]['age']", include_actions=True)
211+
[{'element': 1, 'action': 'GET'}, {'element': 2, 'action': 'GET'}, {'element': 'age', 'action': 'GET'}]
212+
>>>
213+
>>> parse_path("root['joe'].age")
214+
['joe', 'age']
215+
>>> parse_path("root['joe'].age", include_actions=True)
216+
[{'element': 'joe', 'action': 'GET'}, {'element': 'age', 'action': 'GETATTR'}]
217+
218+
"""
219+
220+
result = _path_to_elements(path, root_element=root_element)
221+
result = iter(result)
222+
next(result) # We don't want the root item
223+
if include_actions is False:
224+
return [i[0] for i in result]
225+
return [{'element': i[0], 'action': i[1]} for i in result]

docs/faq.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,23 @@ In order to serialize DeepDiff results into json, use to_json()
8888
>>> diff.to_json()
8989
'{"type_changes": {"root": {"old_type": "int", "new_type": "str", "old_value": 1, "new_value": "a"}}}'
9090

91+
92+
Q: How do I parse DeepDiff result paths?
93+
----------------------------------------
94+
95+
**Answer**
96+
97+
Use parse_path:
98+
99+
>>> from deepdiff import parse_path
100+
>>> parse_path("root[1][2]['age']")
101+
[1, 2, 'age']
102+
>>> parse_path("root[1][2]['age']", include_actions=True)
103+
[{'element': 1, 'action': 'GET'}, {'element': 2, 'action': 'GET'}, {'element': 'age', 'action': 'GET'}]
104+
>>>
105+
>>> parse_path("root['joe'].age")
106+
['joe', 'age']
107+
>>> parse_path("root['joe'].age", include_actions=True)
108+
[{'element': 'joe', 'action': 'GET'}, {'element': 'age', 'action': 'GETATTR'}]
109+
91110
Back to :doc:`/index`

tests/test_path.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from deepdiff.path import _path_to_elements, GET, GETATTR, extract
2+
from deepdiff.path import _path_to_elements, GET, GETATTR, extract, parse_path
33

44

55
@pytest.mark.parametrize('path, expected', [
@@ -32,3 +32,14 @@ def test_path_to_elements(path, expected):
3232
def test_get_item(obj, path, expected):
3333
result = extract(obj, path)
3434
assert expected == result
35+
36+
37+
def test_parse_path():
38+
result = parse_path("root[1][2]['age']")
39+
assert [1, 2, 'age'] == result
40+
result2 = parse_path("root[1][2]['age']", include_actions=True)
41+
assert [{'element': 1, 'action': 'GET'}, {'element': 2, 'action': 'GET'}, {'element': 'age', 'action': 'GET'}] == result2
42+
result3 = parse_path("root['joe'].age")
43+
assert ['joe', 'age'] == result3
44+
result4 = parse_path("root['joe'].age", include_actions=True)
45+
assert [{'element': 'joe', 'action': 'GET'}, {'element': 'age', 'action': 'GETATTR'}] == result4

0 commit comments

Comments
 (0)