Skip to content

Commit ba9daac

Browse files
committed
Move ChilrenList out of Tree class
It can be cumbersome to deal with this class if we want to instantiate if from the outside.
1 parent a472144 commit ba9daac

1 file changed

Lines changed: 64 additions & 60 deletions

File tree

src/ast/tree.py

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Iterable
77
from typing import List
88
from typing import Optional
9+
from typing import Union
910

1011
from src.api.errors import Error
1112

@@ -29,77 +30,20 @@ class Tree:
2930
"""
3031
parent: Optional['Tree'] = None
3132

32-
class ChildrenList:
33-
def __init__(self, node: 'Tree'):
34-
assert isinstance(node, Tree)
35-
self.parent = node # Node having this children
36-
self._children: List['Tree'] = []
37-
38-
def __getitem__(self, key):
39-
if isinstance(key, int):
40-
return self._children[key]
41-
42-
result = Tree.ChildrenList(self.parent)
43-
for x in self._children[key]:
44-
result.append(x)
45-
return result
46-
47-
def __setitem__(self, key, value):
48-
assert value is None or isinstance(value, Tree)
49-
if value is not None:
50-
value.parent = self.parent
51-
self._children[key] = value
52-
53-
def __delitem__(self, key):
54-
self._children[key].parent = None
55-
del self._children[key]
56-
57-
def append(self, value):
58-
assert isinstance(value, Tree)
59-
value.parent = self.parent
60-
self._children.append(value)
61-
62-
def insert(self, pos, value):
63-
assert isinstance(value, Tree)
64-
value.parent = self.parent
65-
self._children.insert(pos, value)
66-
67-
def pop(self, pos=-1):
68-
result = self._children.pop(pos)
69-
result.parent = None
70-
return result
71-
72-
def __len__(self):
73-
return len(self._children)
74-
75-
def __add__(self, other):
76-
if not isinstance(other, Tree.ChildrenList):
77-
assert isinstance(other, collections.Container)
78-
79-
result = Tree.ChildrenList(self.parent)
80-
for x in self:
81-
result.append(x)
82-
for x in other:
83-
result.append(x)
84-
return result
85-
86-
def __repr__(self):
87-
return "%s:%s" % (self.parent.__repr__(), str([x.__repr__() for x in self._children]))
88-
8933
def __init__(self):
90-
self._children = Tree.ChildrenList(self)
34+
self._children = ChildrenList(self)
9135

9236
@property
9337
def children(self):
9438
return self._children
9539

9640
@children.setter
9741
def children(self, value: Iterable):
98-
assert isinstance(value, collections.abc.Iterable)
42+
assert isinstance(value, collections.Iterable)
9943
while len(self.children):
10044
self.children.pop()
10145

102-
self._children = Tree.ChildrenList(self)
46+
self._children = ChildrenList(self)
10347
for x in value:
10448
self.children.append(x)
10549

@@ -136,3 +80,63 @@ def prependChild(self, node: 'Tree'):
13680
""" Inserts the given node at the beginning of the children list
13781
"""
13882
self.children.insert(0, node)
83+
84+
85+
class ChildrenList:
86+
parent: Tree
87+
88+
def __init__(self, node: Tree):
89+
assert isinstance(node, Tree)
90+
self.parent = node # Node having this children
91+
self._children: List[Tree] = []
92+
93+
def __getitem__(self, key: Union[int, slice]):
94+
if isinstance(key, int):
95+
return self._children[key]
96+
97+
result = ChildrenList(self.parent)
98+
for x in self._children[key]:
99+
result.append(x)
100+
return result
101+
102+
def __setitem__(self, key, value: Optional[Tree]):
103+
assert value is None or isinstance(value, Tree)
104+
if value is not None:
105+
value.parent = self.parent
106+
self._children[key] = value
107+
108+
def __delitem__(self, key):
109+
self._children[key].parent = None
110+
del self._children[key]
111+
112+
def append(self, value: Tree):
113+
assert isinstance(value, Tree)
114+
value.parent = self.parent
115+
self._children.append(value)
116+
117+
def insert(self, pos: int, value: Tree):
118+
assert isinstance(value, Tree)
119+
value.parent = self.parent
120+
self._children.insert(pos, value)
121+
122+
def pop(self, pos: int = -1):
123+
result = self._children.pop(pos)
124+
result.parent = None
125+
return result
126+
127+
def __len__(self):
128+
return len(self._children)
129+
130+
def __add__(self, other):
131+
if not isinstance(other, ChildrenList):
132+
assert isinstance(other, collections.Container)
133+
134+
result = ChildrenList(self.parent)
135+
for x in self:
136+
result.append(x)
137+
for x in other:
138+
result.append(x)
139+
return result
140+
141+
def __repr__(self):
142+
return f"{self.parent.__repr__()}:{str([x.__repr__() for x in self._children])}"

0 commit comments

Comments
 (0)