Skip to content

Commit

Permalink
❇️ Add hooks to MelusineRegex
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoPerrier committed Oct 4, 2024
1 parent e1d0ca8 commit e115ce8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions melusine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def describe(self, text: str, position: bool = False) -> None:
position: If True, print regex match start and stop positions.
"""

def _describe_match_field(match_field_data: Dict[str, List[Dict[str, Any]]]) -> None:
def _describe_match_field(match_field_data: dict[str, list[dict[str, Any]]]) -> None:
"""
Format and print result description text.
Expand Down Expand Up @@ -609,7 +609,7 @@ def pre_match_hook(self, text: str) -> str:
"""
return text

def post_match_hook(self, match_dict: Dict[str, Any]) -> Dict[str, Any]:
def post_match_hook(self, match_dict: dict[str, Any]) -> dict[str, Any]:
"""
Hook to run after the Melusine regex match.
Expand Down
11 changes: 6 additions & 5 deletions tests/base/test_melusine_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ def no_match_list(self):


class PreMatchHookVirusRegex(VirusRegex):

def pre_match_hook(self, text: str) -> str:
text = text.replace("virrrrus", "virus")
return text
Expand All @@ -168,12 +167,14 @@ def test_pre_match_hook():


class PostMatchHookVirusRegex(VirusRegex):

def post_match_hook(self, match_dict: Dict[str, Any]) -> Dict[str, Any]:
"""Test custom post processing of match data"""
if match_dict[self.MATCH_RESULT] is True:
if "NEUTRAL_MEDICAL_VIRUS" in match_dict[self.NEUTRAL_MATCH_FIELD] and "NEUTRAL_INSECT" in match_dict[self.NEUTRAL_MATCH_FIELD]:
match_dict[self.MATCH_RESULT] = False
if (
match_dict[self.MATCH_RESULT] is True
and "NEUTRAL_MEDICAL_VIRUS" in match_dict[self.NEUTRAL_MATCH_FIELD]
and "NEUTRAL_INSECT" in match_dict[self.NEUTRAL_MATCH_FIELD]
):
match_dict[self.MATCH_RESULT] = False

return match_dict

Expand Down

0 comments on commit e115ce8

Please sign in to comment.