-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparsers.py
34 lines (30 loc) · 977 Bytes
/
parsers.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
from collections import deque
class Parser(object):
def __init__(self, file_path):
self.trace_file = open(file_path, 'r')
self.items = deque()
def __iter__(self):
return self
def __next__(self):
if not self.items:
line = self.trace_file.readline()
if not line:
self.trace_file.close()
raise StopIteration()
self.parse(line)
return self.items.pop()
def parse(self, line):
pass
class ArcParser(Parser):
def parse(self, line):
line = line.split()
start_block = int(line[0])
self.items.extend(range(start_block, start_block + int(line[1])))
class LirsParser(Parser):
def parse(self, line):
if line and line[0] != '*':
self.items.append(int(line.strip()))
class SizedParser(Parser):
def parse(self, line):
line = line.split()
self.items.append((int(line[1]), int(line[2])))