Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. from collections import deque
  2.  
  3.  
  4. def search(lines, pattern, history=5):
  5.  
  6.     previous_lines = deque(maxlen=history)
  7.     for line in lines:
  8.         if pattern in line:
  9.             yield line, previous_lines
  10.         previous_lines.append(line)
  11.  
  12.  
  13. if __name__ == 'main':
  14.     with open('some_text_file.txt') as f:
  15.         for line, previous_line in search(f, 'python', 5):
  16.             for p_line in previous_line:
  17.                 print(p_line, end='')
  18.             print(line, end='')
  19.             print('-' * 20)
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement