From 390bd51cb18bba5267daca5d37b31b89ff884c03 Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Sat, 22 Feb 2025 15:20:11 +0200 Subject: [PATCH] fix(TextHandler): correcting `case_sensitive` argument logic This is awkward, I can't believe how this slipped from me! --- scrapling/core/custom_types.py | 22 +++++++++++----------- scrapling/parser.py | 16 ++++++++-------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/scrapling/core/custom_types.py b/scrapling/core/custom_types.py index 6e4ef50..30f61e1 100644 --- a/scrapling/core/custom_types.py +++ b/scrapling/core/custom_types.py @@ -134,7 +134,7 @@ class TextHandler(str): check_match: Literal[True], replace_entities: bool = True, clean_match: bool = False, - case_sensitive: bool = False, + case_sensitive: bool = True, ) -> bool: ... @@ -144,26 +144,26 @@ class TextHandler(str): regex: Union[str, Pattern[str]], replace_entities: bool = True, clean_match: bool = False, - case_sensitive: bool = False, + case_sensitive: bool = True, check_match: Literal[False] = False, ) -> "TextHandlers[TextHandler]": ... def re( self, regex: Union[str, Pattern[str]], replace_entities: bool = True, clean_match: bool = False, - case_sensitive: bool = False, check_match: bool = False + case_sensitive: bool = True, check_match: bool = False ) -> Union["TextHandlers[TextHandler]", bool]: """Apply the given regex to the current text and return a list of strings with the matches. :param regex: Can be either a compiled regular expression or a string. :param replace_entities: if enabled character entity references are replaced by their corresponding character :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching - :param case_sensitive: if enabled, function will set the regex to ignore letters case while compiling it + :param case_sensitive: if disabled, function will set the regex to ignore letters case while compiling it :param check_match: used to quickly check if this regex matches or not without any operations on the results """ if isinstance(regex, str): - if not case_sensitive: + if case_sensitive: regex = re.compile(regex, re.UNICODE) else: regex = re.compile(regex, flags=re.UNICODE | re.IGNORECASE) @@ -182,14 +182,14 @@ class TextHandler(str): return TextHandlers(typing.cast(List[_TextHandlerType], [TextHandler(_replace_entities(s)) for s in results])) def re_first(self, regex: Union[str, Pattern[str]], default=None, replace_entities: bool = True, - clean_match: bool = False, case_sensitive: bool = False) -> "TextHandler": + clean_match: bool = False, case_sensitive: bool = True) -> "TextHandler": """Apply the given regex to text and return the first match if found, otherwise return the default value. :param regex: Can be either a compiled regular expression or a string. :param default: The default value to be returned if there is no match :param replace_entities: if enabled character entity references are replaced by their corresponding character :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching - :param case_sensitive: if enabled, function will set the regex to ignore letters case while compiling it + :param case_sensitive: if disabled, function will set the regex to ignore letters case while compiling it """ result = self.re(regex, replace_entities, clean_match=clean_match, case_sensitive=case_sensitive) @@ -218,14 +218,14 @@ class TextHandlers(List[TextHandler]): return typing.cast(_TextHandlerType, TextHandler(lst)) def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True, clean_match: bool = False, - case_sensitive: bool = False) -> 'TextHandlers[TextHandler]': + case_sensitive: bool = True) -> 'TextHandlers[TextHandler]': """Call the ``.re()`` method for each element in this list and return their results flattened as TextHandlers. :param regex: Can be either a compiled regular expression or a string. :param replace_entities: if enabled character entity references are replaced by their corresponding character :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching - :param case_sensitive: if enabled, function will set the regex to ignore letters case while compiling it + :param case_sensitive: if disabled, function will set the regex to ignore letters case while compiling it """ results = [ n.re(regex, replace_entities, clean_match, case_sensitive) for n in self @@ -233,7 +233,7 @@ class TextHandlers(List[TextHandler]): return TextHandlers(flatten(results)) def re_first(self, regex: Union[str, Pattern[str]], default=None, replace_entities: bool = True, - clean_match: bool = False, case_sensitive: bool = False) -> TextHandler: + clean_match: bool = False, case_sensitive: bool = True) -> TextHandler: """Call the ``.re_first()`` method for each element in this list and return the first result or the default value otherwise. @@ -241,7 +241,7 @@ class TextHandlers(List[TextHandler]): :param default: The default value to be returned if there is no match :param replace_entities: if enabled character entity references are replaced by their corresponding character :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching - :param case_sensitive: if enabled, function will set the regex to ignore letters case while compiling it + :param case_sensitive: if disabled, function will set the regex to ignore letters case while compiling it """ for n in self: for result in n.re(regex, replace_entities, clean_match, case_sensitive): diff --git a/scrapling/parser.py b/scrapling/parser.py index 5a4d4f5..e6b138a 100644 --- a/scrapling/parser.py +++ b/scrapling/parser.py @@ -763,25 +763,25 @@ class Adaptor(SelectorsGeneration): return self.get_all_text(strip=True).json() def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True, - clean_match: bool = False, case_sensitive: bool = False) -> TextHandlers: + clean_match: bool = False, case_sensitive: bool = True) -> TextHandlers: """Apply the given regex to the current text and return a list of strings with the matches. :param regex: Can be either a compiled regular expression or a string. :param replace_entities: if enabled character entity references are replaced by their corresponding character :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching - :param case_sensitive: if enabled, function will set the regex to ignore letters case while compiling it + :param case_sensitive: if disabled, function will set the regex to ignore letters case while compiling it """ return self.text.re(regex, replace_entities, clean_match, case_sensitive) def re_first(self, regex: Union[str, Pattern[str]], default=None, replace_entities: bool = True, - clean_match: bool = False, case_sensitive: bool = False) -> TextHandler: + clean_match: bool = False, case_sensitive: bool = True) -> TextHandler: """Apply the given regex to text and return the first match if found, otherwise return the default value. :param regex: Can be either a compiled regular expression or a string. :param default: The default value to be returned if there is no match :param replace_entities: if enabled character entity references are replaced by their corresponding character :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching - :param case_sensitive: if enabled, function will set the regex to ignore letters case while compiling it + :param case_sensitive: if disabled, function will set the regex to ignore letters case while compiling it """ return self.text.re_first(regex, default, replace_entities, clean_match, case_sensitive) @@ -1009,14 +1009,14 @@ class Adaptors(List[Adaptor]): return self.__class__(flatten(results)) def re(self, regex: Union[str, Pattern[str]], replace_entities: bool = True, - clean_match: bool = False, case_sensitive: bool = False) -> TextHandlers[TextHandler]: + clean_match: bool = False, case_sensitive: bool = True) -> TextHandlers[TextHandler]: """Call the ``.re()`` method for each element in this list and return their results flattened as List of TextHandler. :param regex: Can be either a compiled regular expression or a string. :param replace_entities: if enabled character entity references are replaced by their corresponding character :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching - :param case_sensitive: if enabled, function will set the regex to ignore letters case while compiling it + :param case_sensitive: if disabled, function will set the regex to ignore letters case while compiling it """ results = [ n.text.re(regex, replace_entities, clean_match, case_sensitive) for n in self @@ -1024,7 +1024,7 @@ class Adaptors(List[Adaptor]): return TextHandlers(flatten(results)) def re_first(self, regex: Union[str, Pattern[str]], default=None, replace_entities: bool = True, - clean_match: bool = False, case_sensitive: bool = False) -> TextHandler: + clean_match: bool = False, case_sensitive: bool = True) -> TextHandler: """Call the ``.re_first()`` method for each element in this list and return the first result or the default value otherwise. @@ -1032,7 +1032,7 @@ class Adaptors(List[Adaptor]): :param default: The default value to be returned if there is no match :param replace_entities: if enabled character entity references are replaced by their corresponding character :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching - :param case_sensitive: if enabled, function will set the regex to ignore letters case while compiling it + :param case_sensitive: if disabled, function will set the regex to ignore letters case while compiling it """ for n in self: for result in n.re(regex, replace_entities, clean_match, case_sensitive):