@@ -318,9 +318,23 @@ def child(self):
318
318
raise ValueError ('No children available' )
319
319
raise ValueError ('More than one child present' )
320
320
321
- def to_lol (self ):
321
+ def to_lol (self , undict = False ):
322
+ """
323
+ Converts the tree to a *list of lists*.
324
+
325
+ Args:
326
+ undict (bool): if True and the root is a Mapping, it will be converted to a sorted tuple of key-value pairs.
327
+
328
+ Returns:
329
+ A list of lists representing the tree.
330
+ """
331
+
322
332
def walk (T ):
323
- return (T .root , * tuple (walk (child ) for child in T .children ))
333
+ if undict and isinstance (T .root , Mapping ):
334
+ root = tuple ((k , v ) for k , v in sorted (T .root .items ()) if not k .startswith ('_thread_' ))
335
+ else :
336
+ root = T .root
337
+ return (root , * tuple (walk (child ) for child in T .children ))
324
338
325
339
return walk (self )
326
340
@@ -334,7 +348,7 @@ def __eq__(self, other):
334
348
return isinstance (other , Tree ) and self .to_lol () == other .to_lol ()
335
349
336
350
def __hash__ (self ):
337
- return hash (self .to_lol ())
351
+ return hash (self .to_lol (True ))
338
352
339
353
def _gv_graph_ (self ):
340
354
G = GVWrapper (
@@ -392,19 +406,23 @@ def with_threads(self, threads):
392
406
393
407
for node in threads :
394
408
if 'type' in node .root and node .root ['type' ] in ('<BEGIN>' , '<JOIN>' , '<END>' ):
395
- G .node ((node .root , node ), gv_args = node_args )
409
+ G .node ((node .root , node . __instance_count ), gv_args = node_args )
396
410
397
411
for node , info in threads .items ():
398
412
for nxt in info :
399
413
if nxt == 'next' :
400
- G .edge ((node .root , node ), (info [nxt ].root , info [nxt ]), gv_args = edge_args )
414
+ G .edge ((node .root , node . __instance_count ), (info [nxt ].root , info [nxt ]. __instance_count ), gv_args = edge_args )
401
415
else :
402
416
G .node (
403
- (nxt , (1 , node )),
417
+ (nxt , (1 , node . __instance_count )),
404
418
gv_args = {'color' : 'red' , 'fontcolor' : 'red' , 'fontsize' : '10' , 'width' : '.04' , 'height' : '.04' },
405
419
)
406
- G .edge ((node .root , node ), (nxt , (1 , node )), gv_args = edge_args | {'arrowhead' : 'none' })
407
- G .edge ((nxt , (1 , node )), (info [nxt ].root , info [nxt ]), gv_args = edge_args )
420
+ G .edge (
421
+ (node .root , node .__instance_count ),
422
+ (nxt , (1 , node .__instance_count )),
423
+ gv_args = edge_args | {'arrowhead' : 'none' },
424
+ )
425
+ G .edge ((nxt , (1 , node .__instance_count )), (info [nxt ].root , info [nxt ].__instance_count ), gv_args = edge_args )
408
426
409
427
return G
410
428
0 commit comments