In [1]:
from collections import deque
q = deque(maxlen=3)
q.append(1)
q.append(2)
q.append(3)
q
Out[1]:
In [7]:
q.append(4)
q
Out[7]:
In [8]:
q.append(5)
q
Out[8]:
In [34]:
import os
file_dir = os.path.dirname(os.path.realpath('__file__'))
filename = os.path.abspath(os.path.join(file_dir, "..", "code/src/1/keeping_the_last_n_items/somefile.txt"))
In [36]:
!head $filename
In [37]:
from collections import deque
def search(lines, pattern, history=5):
previous_lines = deque(maxlen=history)
for line in lines:
if pattern in line:
yield line, previous_lines
previous_lines.append(line)
# Example use on a file
if __name__ == '__main__':
with open(filename) as f:
for line, prevlines in search(f, 'python', 5):
for pline in prevlines:
print(pline, end='')
print(line, end='')
print('-'*20)