MCPcopy Create free account
hub / github.com/dabeaz/python-cookbook / search

Function search

src/1/keeping_the_last_n_items/example.py:3–8  ·  view source on GitHub ↗
(lines, pattern, history=5)

Source from the content-addressed store, hash-verified

1from collections import deque
2
3def search(lines, pattern, history=5):
4 previous_lines = deque(maxlen=history)
5 for line in lines:
6 if pattern in line:
7 yield line, previous_lines
8 previous_lines.append(line)
9
10# Example use on a file
11if __name__ == '__main__':

Callers 1

example.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected