Skip to content

Commit 59bea3f

Browse files
committed
Major fixes in Tree, to allow threads treatment
1 parent a4365eb commit 59bea3f

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

CHANGELOG.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
Changelog
22
=========
33

4+
v1.13.1 (2025-05-22)
5+
--------------------
6+
7+
- Improved Tree.to_lol so it avoids _thread_ attributes.
8+
- Fixed Tree.__hash__ and Tree.__eq__ for annotated trees.
9+
- Fixed Tree.with_threads.
10+
411
v1.13.0 (2025-04-10)
512
--------------------
613

7-
- AnnotatedTreeWalker vist accepts an Itrable, now
8-
visit(trees) returns tuple(vist(tree) for tree in trees)
14+
- AnnotatedTreeWalker visit accepts an Iterable: now
15+
visit(trees) returns tuple(visit(tree) for tree in trees).
916

1017
v1.12.2 (2025-04-09)
1118
--------------------

src/liblet/antlr.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from collections.abc import Iterable
12
from contextlib import redirect_stderr
23
from importlib import util as imputil
34
from io import StringIO
@@ -20,7 +21,6 @@
2021

2122
from liblet.display import Tree
2223
from liblet.utils import warn
23-
from collections.abc import Iterable
2424

2525
if 'READTHEDOCS' not in environ: # pragma: nocover
2626
if 'ANTLR4_JAR' not in environ:
@@ -387,8 +387,7 @@ def __call__(self, tree_or_trees):
387387
tree = tree_or_trees
388388
visitor = self.dispatch_table.get(tree.root[self.key], self.catchall_func)
389389
return visitor(self.__call__, tree)
390-
elif isinstance(tree_or_trees, Iterable):
390+
if isinstance(tree_or_trees, Iterable):
391391
trees = list(tree_or_trees)
392392
return tuple(self.__call__(t) for t in trees)
393-
else:
394-
raise TypeError(f'Expected Tree or Iterable, got {type(tree_or_trees)}')
393+
raise TypeError(f'Expected Tree or Iterable, got {type(tree_or_trees)}')

src/liblet/display.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,23 @@ def child(self):
318318
raise ValueError('No children available')
319319
raise ValueError('More than one child present')
320320

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+
322332
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))
324338

325339
return walk(self)
326340

@@ -334,7 +348,7 @@ def __eq__(self, other):
334348
return isinstance(other, Tree) and self.to_lol() == other.to_lol()
335349

336350
def __hash__(self):
337-
return hash(self.to_lol())
351+
return hash(self.to_lol(True))
338352

339353
def _gv_graph_(self):
340354
G = GVWrapper(
@@ -392,19 +406,23 @@ def with_threads(self, threads):
392406

393407
for node in threads:
394408
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)
396410

397411
for node, info in threads.items():
398412
for nxt in info:
399413
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)
401415
else:
402416
G.node(
403-
(nxt, (1, node)),
417+
(nxt, (1, node.__instance_count)),
404418
gv_args={'color': 'red', 'fontcolor': 'red', 'fontsize': '10', 'width': '.04', 'height': '.04'},
405419
)
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)
408426

409427
return G
410428

0 commit comments

Comments
 (0)