Skip to content

Abstract Rules

Base Pattern

Bases: BaseModel, abc.ABC

Source code in statute_patterns/components/rule.py
Python
class BasePattern(BaseModel, abc.ABC):
    matches: list[str] = Field(
        default_factory=list,
        description=(
            "When supplied, text included _should_ match regex property."
        ),
    )
    excludes: list[str] = Field(
        default_factory=list,
        description=(
            "When supplied, text included _should not_ match regex property."
        ),
    )

    class Config:
        use_enum_values = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.validate_matches()
        self.validate_excludes()

    @property
    @abc.abstractmethod
    def regex(self) -> str:
        """Combines the group_name with the desired regex string."""
        raise NotImplementedError(
            "Base regex to be later combined with other rules regex strings."
        )

    @property
    @abc.abstractmethod
    def group_name(self) -> str:
        """Added to regex string to identify the `match.lastgroup`"""
        raise NotImplementedError("Used to identify `regex` capture group.")

    @property
    def pattern(self) -> Pattern:
        """Enables use of a unique Pattern object per rule pattern created,
        regardless of it being a SerialPattern or a NamedPattern."""
        return re.compile(self.regex, re.X)

    def validate_matches(self) -> None:
        for example_text in self.matches:
            if not self.pattern.fullmatch(example_text):
                raise ValueError(
                    "Missing match but intended to be included:"
                    f" {example_text}"
                )

    def validate_excludes(self) -> None:
        for example_text in self.excludes:
            if self.pattern.fullmatch(example_text):
                raise ValueError(
                    "Match found even if intended to be excluded:"
                    f" {example_text}."
                )

Attributes

group_name: str abstractmethod property

Added to regex string to identify the match.lastgroup

pattern: Pattern property

Enables use of a unique Pattern object per rule pattern created, regardless of it being a SerialPattern or a NamedPattern.

regex: str abstractmethod property

Combines the group_name with the desired regex string.

Base Collection

Bases: BaseModel, abc.ABC

Whether a collection of Named or Serial patterns are instantiated, a combined_regex property and a pattern propery will be automatically created based on the collection of objects declared on instantiation of the class.

Source code in statute_patterns/components/rule.py
Python
class BaseCollection(BaseModel, abc.ABC):
    """Whether a collection of Named or Serial patterns are instantiated,
    a `combined_regex` property and a `pattern` propery will be automatically
    created based on the collection of objects declared on instantiation
    of the class."""

    collection: list = NotImplemented

    @abc.abstractmethod
    def extract_rules(self, text: str) -> Iterator[Rule]:
        raise NotImplementedError("Need ability to fetch Rule objects.")

    @property
    def combined_regex(self) -> str:
        """Combine the different items in the collection
        (having .regex attribute) to form a single regex string."""
        return "|".join([r.regex for r in self.collection])

    @property
    def pattern(self) -> Pattern:
        return re.compile(self.combined_regex, re.X)

Attributes

combined_regex: str property

Combine the different items in the collection (having .regex attribute) to form a single regex string.