-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrace.py
More file actions
77 lines (57 loc) · 1.62 KB
/
trace.py
File metadata and controls
77 lines (57 loc) · 1.62 KB
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
from abc import ABCMeta, abstractmethod
class Trace():
__metaclass__ = ABCMeta
def __init__(self,tr):
self.trace = tr
@abstractmethod
def __str__(self):
pass
def __repr__(self):
return self.__str__()
def get_curr(self):
return self.curr
def get_trace(self):
return self.trace
def __iter__(self):
return self
@abstractmethod
def __next__(self):
pass
def next(self):
return self.__next__()
class ForwardTrace(Trace):
def __init__(self,tr):
super(ForwardTrace, self).__init__(tr)
self.curr = 1
def __str__(self):
res = ""
i = 1
for stmt in self.trace:
res = res + str(i) + ". " + str(self.trace[i - 1]) + "\n"
i = i + 1
return res
def __next__(self):
if self.curr == len(self.trace) + 1:
self.curr = 1
raise StopIteration
else:
self.curr += 1
return self.trace[self.curr - 1 - 1] # previous value, zero based
class BackwardTrace(Trace):
def __init__(self,tr):
super(BackwardTrace, self).__init__(tr)
self.curr = len(tr)
def __str__(self):
res = ""
i = len(self.trace)
while i >= 1:
res = res + str(i) + ". " + str(self.trace[i - 1]) + "\n"
i = i - 1
return res
def __next__(self):
if self.curr == 0:
self.curr = len(self.trace)
raise StopIteration
else:
self.curr -= 1
return self.trace[self.curr + 1 - 1] # previous value, zero based