-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
38 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Local Library | ||
from .parser import ArgumentParser | ||
from .search import Search | ||
|
||
__all__ = ["ArgumentParser"] | ||
__all__ = ["ArgumentParser", "Search"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Standard Library | ||
import re | ||
from dataclasses import dataclass | ||
from io import TextIOBase | ||
from typing import Self | ||
|
||
|
||
@dataclass | ||
class SearchConfig: | ||
patterns: list[re.Pattern] | ||
input: str | ||
|
||
@classmethod | ||
def new(cls, patterns: list[str], input: TextIOBase) -> 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: TextIOBase) -> Self: | ||
return cls(cls.Config.new(patterns, input)) |