-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
35 lines (26 loc) · 842 Bytes
/
search.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
# Standard Library
import re
from dataclasses import dataclass
from typing import Self, TextIO
@dataclass
class SearchConfig:
patterns: list[re.Pattern]
input: str
@classmethod
def new(cls, patterns: list[str], input: TextIO) -> Self:
pats = [re.compile(pat) for pat in patterns]
return cls(pats, input.read())
class Search:
__slots__ = ("__config",)
Config = SearchConfig
def __init__(self, config: SearchConfig) -> None:
self.__config = config
def run(self) -> list[str]:
return [
line
for line in self.__config.input.splitlines()
if any(pat.search(line) for pat in self.__config.patterns)
]
@classmethod
def new(cls, patterns: list[str], input: TextIO) -> Self:
return cls(cls.Config.new(patterns, input))