@@ -66,11 +66,16 @@ def get(self, path: str) -> Union[DictPath, Any]:
6666 return self
6767 current = self .data
6868 for attr in path :
69- if not isinstance (current , dict ):
70- raise Exception (f"Your path is not a path of dicts (value at key { attr } is of type { type (current )} )" )
71- if attr not in current :
69+ if isinstance (current , dict ):
70+ print (current , attr )
71+ current = current .get (attr )
72+ elif isinstance (current , list ):
73+ print (current , attr )
74+ current = current [int (attr )]
75+ elif current is None :
7276 return None
73- current = current [attr ]
77+ else :
78+ raise Exception (f"Your path is not a path of dicts (value at key { attr } is of type { type (current )} )" )
7479 if isinstance (current , dict ):
7580 return DictPath (current )
7681 return current
@@ -109,27 +114,30 @@ def __setitem__(self, path, value):
109114 self .set (path , value = value )
110115
111116def extract_dict (dictionary , path ):
112- path = path [1 :] if path .startswith ('/' ) else path
113- paths = path .split ('/' )
114- active_dict = dictionary
115- for p in paths :
116- if active_dict .get (p ) != None :
117- active_dict = active_dict .get (p )
118- else :
119- return None
120- return active_dict
117+ path = path [1 :] if path .startswith ('/' ) else path
118+ paths = path .split ('/' )
119+ active_dict = dictionary
120+ for p in paths :
121+ if active_dict is None :
122+ return None
123+ if isinstance (active_dict , dict ):
124+ active_dict = active_dict .get (p )
125+ elif isinstance (active_dict , list ):
126+ active_dict = active_dict [int (p )]
127+ return active_dict
121128
122129def inject_dict (dictionary , path , value ):
123- path = path [1 :] if path .startswith ('/' ) else path
124- paths = path .split ('/' )
125- path_len = len (paths )
126- _active_dict = dictionary
127- for i , p in enumerate (paths ):
128- if i == path_len - 1 :
129- _active_dict [p ] = value
130- else :
131- if _active_dict .get (p ) == None :
132- _active_dict [p ] = {}
133- _active_dict = _active_dict .get (p )
134- return dictionary
135-
130+ path = path [1 :] if path .startswith ('/' ) else path
131+ paths = path .split ('/' )
132+ path_len = len (paths )
133+ _active_dict = dictionary
134+ for i , p in enumerate (paths ):
135+
136+ if i == path_len - 1 :
137+ _active_dict [p ] = value
138+ continue
139+
140+ if _active_dict .get (p ) == None :
141+ _active_dict [p ] = {}
142+ _active_dict = _active_dict .get (p )
143+ return dictionary
0 commit comments