From 7300efa77efb1307cbe5c91a0ecd5b49712b4e85 Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Wed, 30 Jul 2025 01:45:26 +0300 Subject: [PATCH] style(parser): optimize selectors instances creation --- scrapling/parser.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/scrapling/parser.py b/scrapling/parser.py index 459286a..ff88de2 100644 --- a/scrapling/parser.py +++ b/scrapling/parser.py @@ -253,14 +253,14 @@ class Selector(SelectorsGeneration): if not len( result ): # Lxml will give a warning if I used something like `not result` - return Selectors([]) + return Selectors() # From within the code, this method will always get a list of the same type, # so we will continue without checks for a slight performance boost if self._is_text_node(result[0]): return TextHandlers(list(map(self.__content_convertor, result))) - return Selectors(list(map(self.__element_convertor, result))) + return Selectors(map(self.__element_convertor, result)) def __getstate__(self) -> Any: # lxml don't like it :) @@ -378,11 +378,9 @@ class Selector(SelectorsGeneration): def children(self) -> "Selectors[Selector]": """Return the children elements of the current element or empty list otherwise""" return Selectors( - [ - self.__element_convertor(child) - for child in self._root.iterchildren() - if type(child) not in html_forbidden - ] + self.__element_convertor(child) + for child in self._root.iterchildren() + if type(child) not in html_forbidden ) @property @@ -390,9 +388,9 @@ class Selector(SelectorsGeneration): """Return other children of the current element's parent or empty list otherwise""" if self.parent: return Selectors( - [child for child in self.parent.children if child._root != self._root] + child for child in self.parent.children if child._root != self._root ) - return Selectors([]) + return Selectors() def iterancestors(self) -> Generator["Selector", None, None]: """Return a generator that loops over all ancestors of the element, starting with the element's parent.""" @@ -734,7 +732,7 @@ class Selector(SelectorsGeneration): attributes = dict() tags, patterns = set(), set() - results, functions, selectors = Selectors([]), [], [] + results, functions, selectors = Selectors(), [], [] # Brace yourself for a wonderful journey! for arg in args: @@ -1134,7 +1132,7 @@ class Selector(SelectorsGeneration): :param clean_match: if enabled, this will ignore all whitespaces and consecutive spaces while matching """ - results = Selectors([]) + results = Selectors() if not case_sensitive: text = text.lower() @@ -1178,7 +1176,7 @@ class Selector(SelectorsGeneration): :param case_sensitive: If enabled, the letters case will be taken into consideration in the regex. :param clean_match: If enabled, this will ignore all whitespaces and consecutive spaces while matching. """ - results = Selectors([]) + results = Selectors() # This selector gets all elements with text content for node in self.__handle_elements(