@@ -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 ]
0 commit comments