forked from jdortizv/teoria-de-la-compilacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataTree.py
168 lines (128 loc) · 4.7 KB
/
dataTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
###< 2016-08-28 18:12:00.903563 >###
""" DataTree for Python
This module implements a generic tree data structure for storing and
retrieving hierarchical information.
This data structure is used to store the information generated
during the compilation process.
"""
class TreeNode:
def __init__(self, tag, attrib = {}):
self.tag = tag
self.attrib = attrib.copy()
self._children = []
self.text = None
def __repr__(self):
return '<Node {}>'.format(repr(self.tag))
# methods operating over subelements
def __len__(self):
return len(self._children)
def __iter__(self):
return self._children.__iter__()
def __getitem__(self, index):
return self._children[index]
def __setitem__(self, index, value):
self._children[index] = value
def __delitem__(self, index):
del self._children[index]
def __str__(self):
txt = '{0}'.format(self.tag)
if len(self.attrib):
txt += ' '
keys = sorted(list(self.attrib.keys()))
txt += '{'
for k in keys:
txt += k + ': '
txt += str(self.attrib[k])
if k != keys[-1]:
txt += ', '
txt += '}'
if not self.text is None:
txt += ' :' + self.text
return txt
def append(self, subnode):
self._children.append(subnode)
def extend(self, subnodes):
self._children.extend(subnodes)
def insert(self, index, subnode):
self._children.insert(index, subnode)
def find(self, match):
for node in self._children:
if node.tag == match:
return node
return None
def findall(self, match):
result = []
for node in self._children:
if node.tag == match:
result.append(node)
return result
def search(self, match, key, value):
for node in self._children:
if node.tag == match and node.attrib[key] == value:
return node
return None
def searchall(self, match, key, value):
result = []
for node in self._children:
if node.tag == match and node.attrib[key] == value:
result.append(node)
return result
def iter(self, tag=None):
for e in self._children:
yield from e.iter(tag)
# metodos que operan sobre los atributos (dict)
def clear(self):
self.text = None
self.attrib.clear()
self._children = []
def get(self, key, default=None):
return self.attrib.get(key, default)
def set(self, key, value):
self.attrib[key] = value
def keys(self):
return self.attrib.keys()
def values(self):
return self.attrib.values()
def items(self):
return self.attrib.items()
# retorna el nodo creado
def SubNode(parent, tag, attrib = {}):
parent.append(TreeNode(tag, attrib))
return parent._children[-1]
def printTree(node):
def printTreeNode(prefix, node, lastNode):
print(prefix + '+-- ' + str(node))
for i, n in enumerate(node._children):
isLastNode = True if i == len(node._children) - 1 else False
if lastNode == True:
printTreeNode(prefix + ' ', n, isLastNode)
else:
printTreeNode(prefix + '| ', n, isLastNode)
printTreeNode('', node, True)
def compTree(tree1, tree2):
import sys
def printError(node1, node2, counter, text):
print('Error: : ' + text)
print('Expected: [{:03d}] '.format(counter) + ' ' + str(node1) + \
' with ' + str(len(node1._children)) + ' branches.')
print('Found: [{:03d}] '.format(counter) + ' ' + str(node2) + \
' with ' + str(len(node2._children)) + ' branches.')
sys.exit()
def compNode(node1, node2, counter):
if node1.tag != node2.tag:
printError(node1, node2, counter, 'Different tag')
if node1.text != node2.text:
printError(node1, node2, counter, 'Different text')
keys1 = sorted(list(node1.attrib.keys()))
keys2 = sorted(list(node2.attrib.keys()))
if keys1 != keys2:
printError(node1, node2, counter, 'Different keys')
for key in keys1:
if node1.get(key) != node2.get(key):
printError(node1, node2, counter, 'Different values')
if len(node1._children) != len(node2._children):
printError(node1, node2, counter, 'Different number of branches')
for i, (n1, n2) in enumerate(zip(node1._children, node2._children)):
compNode(n1, n2, counter + i + 1)
compNode(tree1, tree2, 0)
return True