-
Notifications
You must be signed in to change notification settings - Fork 4
/
grep2.py
executable file
·57 lines (40 loc) · 1.41 KB
/
grep2.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
#!/usr/bin/env python
import re
class Grep(object):
def __init__(self, filename, pattern):
self._file = open(filename, 'r')
self._pattern = pattern
def match(self, line):
return re.search(self._pattern, line)
def result(self, match, line):
return line
def next(self):
line = next(self._file)
match = self.match(line)
while not match:
line = next(self._file)
match = self.match(line)
return self.result(match, line)
def __next__(self):
"""Compatibility method for Python 3"""
return self.next()
class GrepOnlyMatching(Grep):
def result(self, match, line):
return match.group(0)
class GrepExactly(Grep):
def match(self, line):
return (self._pattern in line)
if __name__ == '__main__':
# test Grep
grep = Grep(__file__, 'class Grep[A-Za-z]+')
assert 'class GrepOnlyMatching(Grep):\n' == next(grep)
assert 'class GrepExactly(Grep):\n' == next(grep)
# test GrepOnlyMatching
grep = GrepOnlyMatching(__file__, 'class Grep[A-Za-z]+')
assert 'class GrepOnlyMatching' == next(grep)
assert 'class GrepExactly' == next(grep)
# test GrepExactly
grep = GrepExactly(__file__, 'class Grep')
assert 'class Grep(object):\n' == next(grep)
assert 'class GrepOnlyMatching(Grep):\n' == next(grep)
assert 'class GrepExactly(Grep):\n' == next(grep)