-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStackTree.py
69 lines (55 loc) · 1.67 KB
/
StackTree.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
"""
(c) 2014 Arts Alliance Media
Tree like representation of a stack trace.
"""
def count_spaces(string):
"""Counts the number of initial spaces in a string.
Returns:
A tuple with the number of spaces and the string excluding the spaces.
"""
count = 0
for ch in string:
if ch == ' ':
count += 1
else:
break
return (count, string[count:])
class StackTree(object):
"""Represents a stack trace in a tree."""
def __init__(self, level, value):
self._children = []
self._level = level
self._value = value
self._store = {}
def append(self, level, value):
if level == self._level + 1:
node = StackTree(level, value)
self._children.append(node)
else:
self._children[-1].append(level, value)
def get(self, name):
return self._store[name]
def level(self):
return self._level
def reverse_traverse(self, function):
function(self)
for c in reversed(self._children):
c.reverse_traverse(function)
def store(self, name, value):
self._store[name] = value
def traverse(self, function):
function(self)
for c in self._children:
c.traverse(function)
def value(self):
return self._value
def build_from_file(trace):
line = trace.readline().rstrip()
(top, value) = count_spaces(line)
assert top == 0, "build_from_file requires a top level stack trace."
root = StackTree(top, value)
for line in trace:
line = line.rstrip()
(level, value) = count_spaces(line)
root.append(level, value)
return root