22
33from __future__ import annotations
44
5+ import json
56import os
67import random
78import sys
@@ -69,6 +70,27 @@ def _visit(node: AuxNode, depth: int = 0) -> None:
6970 _visit (root )
7071 return root
7172
73+ @staticmethod
74+ def collections (data : JSONLikeData ) -> AuxNode :
75+ def _visit (node : AuxNode , depth : int = 0 ) -> None :
76+ if isinstance (node .value , dict ):
77+ for val in node .value .values ():
78+ if isinstance (val , (list , dict )):
79+ _node = AuxNode (depth + 1 , val )
80+ _visit (_node , depth + 1 )
81+ node .children .append (_node )
82+
83+ elif isinstance (node .value , list ):
84+ for val in node .value :
85+ if isinstance (val , (list , dict )):
86+ _node = AuxNode (depth + 1 , val )
87+ _visit (_node , depth + 1 )
88+ node .children .append (_node )
89+
90+ root = AuxNode (0 , data )
91+ _visit (root )
92+ return root
93+
7294
7395def pptree (
7496 node : AuxNode ,
@@ -115,13 +137,14 @@ def nondeterministic_visit(root: AuxNode) -> Iterable[AuxNode]:
115137 while queue :
116138 _node = queue .popleft ()
117139 yield _node
140+ # Visit child nodes now or queue them for later?
141+ visit_children = random .choice ([True , False ])
118142 for child in _node .children :
119- # Queue the child node or visit it now?
120- if random .choice ([True , False ]): # noqa: S311
121- queue .append (child )
122- else :
143+ if visit_children :
123144 yield child
124145 queue .extend (child .children )
146+ else :
147+ queue .append (child )
125148
126149
127150def get_perms (root : AuxNode ) -> List [Tuple [AuxNode , ...]]:
@@ -130,22 +153,43 @@ def get_perms(root: AuxNode) -> List[Tuple[AuxNode, ...]]:
130153 return sorted (perms , key = lambda t : str (t ))
131154
132155
133- def pp_json_path_data (data : JSONLikeData ) -> None :
156+ def pp_json_path_perms (data : JSONLikeData ) -> None :
157+ print ("Input data" )
158+ print (f"\033 [92m{ data } \033 [0m" )
134159 aux_tree = AuxNode .from_ (data )
160+ print ("\n Tree view" )
135161 pptree (aux_tree )
136162
137- print ("\n Pre order\n " )
163+ print ("\n Pre order" )
138164 print (", " .join (str (n ) for n in pre_order_visit (aux_tree )))
139165
140- print ("\n Level order\n " )
166+ print ("\n Level order" )
141167 print (", " .join (str (n ) for n in breadth_first_visit (aux_tree )))
142168
143- print ("\n Nondeterministic order\n " )
169+ print ("\n Nondeterministic order" )
170+ for perm in get_perms (aux_tree ):
171+ print (", " .join (str (node ) for node in perm ))
172+
173+ print ("\n ---\n \n Collections only" )
174+ aux_tree = AuxNode .collections (data )
175+ pptree (aux_tree )
176+
177+ print ("\n Pre order" )
178+ print (", " .join (str (n ) for n in pre_order_visit (aux_tree )))
179+
180+ print ("\n Level order" )
181+ print (", " .join (str (n ) for n in breadth_first_visit (aux_tree )))
182+
183+ print ("\n Nondeterministic order" )
144184 for perm in get_perms (aux_tree ):
145185 print (", " .join (str (node ) for node in perm ))
146186
147187
148188if __name__ == "__main__" :
149- # basic, descendant segment, name shorthand
150- data = {"o" : [{"a" : "b" }, {"a" : "c" }]}
151- pp_json_path_data (data )
189+ if len (sys .argv ) < 2 : # noqa: PLR2004
190+ print ("error: no data to process" )
191+ print (f"usage: { sys .argv [0 ]} <JSON string>" )
192+ sys .exit (1 )
193+
194+ data = json .loads (sys .argv [1 ])
195+ pp_json_path_perms (data )
0 commit comments