From bbc05ca1645d622ab27d144d1b36d81231e31f15 Mon Sep 17 00:00:00 2001 From: Karim shoair Date: Mon, 17 Mar 2025 06:24:42 +0200 Subject: [PATCH] fix(importing): Lazy Loading submodules to lower memory usage on importing Load what you use instead of all submodules together --- scrapling/__init__.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/scrapling/__init__.py b/scrapling/__init__.py index 0877054..e8461b5 100644 --- a/scrapling/__init__.py +++ b/scrapling/__init__.py @@ -1,12 +1,41 @@ -# Declare top-level shortcuts -from scrapling.core.custom_types import AttributesHandler, TextHandler -from scrapling.fetchers import (AsyncFetcher, CustomFetcher, Fetcher, - PlayWrightFetcher, StealthyFetcher) -from scrapling.parser import Adaptor, Adaptors __author__ = "Karim Shoair (karim.shoair@pm.me)" __version__ = "0.2.97" __copyright__ = "Copyright (c) 2024 Karim Shoair" +# A lightweight approach to create lazy loader for each import for backward compatibility +# This will reduces initial memory footprint significantly (only loads what's used) +def __getattr__(name): + if name == 'Fetcher': + from scrapling.fetchers import Fetcher as cls + return cls + elif name == 'Adaptor': + from scrapling.parser import Adaptor as cls + return cls + elif name == 'Adaptors': + from scrapling.parser import Adaptors as cls + return cls + elif name == 'AttributesHandler': + from scrapling.core.custom_types import AttributesHandler as cls + return cls + elif name == 'TextHandler': + from scrapling.core.custom_types import TextHandler as cls + return cls + elif name == 'AsyncFetcher': + from scrapling.fetchers import AsyncFetcher as cls + return cls + elif name == 'StealthyFetcher': + from scrapling.fetchers import StealthyFetcher as cls + return cls + elif name == 'PlayWrightFetcher': + from scrapling.fetchers import PlayWrightFetcher as cls + return cls + elif name == 'CustomFetcher': + from scrapling.fetchers import CustomFetcher as cls + return cls + else: + raise AttributeError(f"module 'scrapling' has no attribute '{name}'") + + __all__ = ['Adaptor', 'Fetcher', 'AsyncFetcher', 'StealthyFetcher', 'PlayWrightFetcher']