Skip to content

Commit

Permalink
Add search module, re-export it
Browse files Browse the repository at this point in the history
  • Loading branch information
H1rono committed Jun 12, 2023
1 parent a463552 commit 05e1399
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion minigrep_py/__init__.py
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"]
36 changes: 36 additions & 0 deletions minigrep_py/search.py
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))

0 comments on commit 05e1399

Please sign in to comment.