perf: imports optimizing

This commit is contained in:
Karim shoair
2025-07-30 00:22:06 +03:00
parent 8ca940d768
commit ba585c0adc
3 changed files with 16 additions and 17 deletions
-1
View File
@@ -15,7 +15,6 @@ from scrapling.fetchers import (
) )
from scrapling.core._types import ( from scrapling.core._types import (
Optional, Optional,
Literal,
Tuple, Tuple,
extraction_types, extraction_types,
Union, Union,
+16 -15
View File
@@ -1,14 +1,15 @@
import re
import typing
from collections.abc import Mapping from collections.abc import Mapping
from types import MappingProxyType from types import MappingProxyType
from re import compile as re_compile, sub, UNICODE, IGNORECASE
from orjson import dumps, loads from orjson import dumps, loads
from scrapling.core._types import ( from scrapling.core._types import (
cast,
Dict, Dict,
List, List,
Union, Union,
overload,
TypeVar, TypeVar,
Literal, Literal,
Pattern, Pattern,
@@ -34,11 +35,11 @@ class TextHandler(str):
def __getitem__(self, key: Union[SupportsIndex, slice]) -> "TextHandler": def __getitem__(self, key: Union[SupportsIndex, slice]) -> "TextHandler":
lst = super().__getitem__(key) lst = super().__getitem__(key)
return typing.cast(_TextHandlerType, TextHandler(lst)) return cast(_TextHandlerType, TextHandler(lst))
def split(self, sep: str = None, maxsplit: SupportsIndex = -1) -> "TextHandlers": def split(self, sep: str = None, maxsplit: SupportsIndex = -1) -> "TextHandlers":
return TextHandlers( return TextHandlers(
typing.cast( cast(
List[_TextHandlerType], List[_TextHandlerType],
[TextHandler(s) for s in super().split(sep, maxsplit)], [TextHandler(s) for s in super().split(sep, maxsplit)],
) )
@@ -119,7 +120,7 @@ class TextHandler(str):
"""Return a new version of the string after removing all white spaces and consecutive spaces""" """Return a new version of the string after removing all white spaces and consecutive spaces"""
trans_table = str.maketrans("\t\r\n", " ") trans_table = str.maketrans("\t\r\n", " ")
data = self.translate(trans_table) data = self.translate(trans_table)
return self.__class__(re.sub(" +", " ", data).strip()) return self.__class__(sub(" +", " ", data).strip())
# For easy copy-paste from Scrapy/parsel code when needed :) # For easy copy-paste from Scrapy/parsel code when needed :)
def get(self, default=None): def get(self, default=None):
@@ -137,7 +138,7 @@ class TextHandler(str):
# Check this out: https://github.com/ijl/orjson/issues/445 # Check this out: https://github.com/ijl/orjson/issues/445
return loads(str(self)) return loads(str(self))
@typing.overload @overload
def re( def re(
self, self,
regex: Union[str, Pattern[str]], regex: Union[str, Pattern[str]],
@@ -147,7 +148,7 @@ class TextHandler(str):
case_sensitive: bool = True, case_sensitive: bool = True,
) -> bool: ... ) -> bool: ...
@typing.overload @overload
def re( def re(
self, self,
regex: Union[str, Pattern[str]], regex: Union[str, Pattern[str]],
@@ -176,9 +177,9 @@ class TextHandler(str):
""" """
if isinstance(regex, str): if isinstance(regex, str):
if case_sensitive: if case_sensitive:
regex = re.compile(regex, re.UNICODE) regex = re_compile(regex, UNICODE)
else: else:
regex = re.compile(regex, flags=re.UNICODE | re.IGNORECASE) regex = re_compile(regex, flags=UNICODE | IGNORECASE)
input_text = self.clean() if clean_match else self input_text = self.clean() if clean_match else self
results = regex.findall(input_text) results = regex.findall(input_text)
@@ -190,13 +191,13 @@ class TextHandler(str):
if not replace_entities: if not replace_entities:
return TextHandlers( return TextHandlers(
typing.cast( cast(
List[_TextHandlerType], [TextHandler(string) for string in results] List[_TextHandlerType], [TextHandler(string) for string in results]
) )
) )
return TextHandlers( return TextHandlers(
typing.cast( cast(
List[_TextHandlerType], List[_TextHandlerType],
[TextHandler(_replace_entities(s)) for s in results], [TextHandler(_replace_entities(s)) for s in results],
) )
@@ -235,11 +236,11 @@ class TextHandlers(List[TextHandler]):
__slots__ = () __slots__ = ()
@typing.overload @overload
def __getitem__(self, pos: SupportsIndex) -> TextHandler: def __getitem__(self, pos: SupportsIndex) -> TextHandler:
pass pass
@typing.overload @overload
def __getitem__(self, pos: slice) -> "TextHandlers": def __getitem__(self, pos: slice) -> "TextHandlers":
pass pass
@@ -249,8 +250,8 @@ class TextHandlers(List[TextHandler]):
lst = super().__getitem__(pos) lst = super().__getitem__(pos)
if isinstance(pos, slice): if isinstance(pos, slice):
lst = [TextHandler(s) for s in lst] lst = [TextHandler(s) for s in lst]
return TextHandlers(typing.cast(List[_TextHandlerType], lst)) return TextHandlers(cast(List[_TextHandlerType], lst))
return typing.cast(_TextHandlerType, TextHandler(lst)) return cast(_TextHandlerType, TextHandler(lst))
def re( def re(
self, self,
-1
View File
@@ -2,7 +2,6 @@ from scrapling.core._types import (
Callable, Callable,
Dict, Dict,
List, List,
Literal,
Optional, Optional,
SelectorWaitStates, SelectorWaitStates,
Union, Union,