Karim shoair commited on
Commit
556b34c
·
1 Parent(s): 98bc2b6

fix(importing): Lazy Loading submodules to lower memory usage on importing

Browse files
Files changed (1) hide show
  1. scrapling/__init__.py +34 -5
scrapling/__init__.py CHANGED
@@ -1,12 +1,41 @@
1
- # Declare top-level shortcuts
2
- from scrapling.core.custom_types import AttributesHandler, TextHandler
3
- from scrapling.fetchers import (AsyncFetcher, CustomFetcher, Fetcher,
4
- PlayWrightFetcher, StealthyFetcher)
5
- from scrapling.parser import Adaptor, Adaptors
6
 
7
  __author__ = "Karim Shoair (karim.shoair@pm.me)"
8
  __version__ = "0.2.97"
9
  __copyright__ = "Copyright (c) 2024 Karim Shoair"
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  __all__ = ['Adaptor', 'Fetcher', 'AsyncFetcher', 'StealthyFetcher', 'PlayWrightFetcher']
 
 
 
 
 
 
1
 
2
  __author__ = "Karim Shoair (karim.shoair@pm.me)"
3
  __version__ = "0.2.97"
4
  __copyright__ = "Copyright (c) 2024 Karim Shoair"
5
 
6
 
7
+ # A lightweight approach to create lazy loader for each import for backward compatibility
8
+ # This will reduces initial memory footprint significantly (only loads what's used)
9
+ def __getattr__(name):
10
+ if name == 'Fetcher':
11
+ from scrapling.fetchers import Fetcher as cls
12
+ return cls
13
+ elif name == 'Adaptor':
14
+ from scrapling.parser import Adaptor as cls
15
+ return cls
16
+ elif name == 'Adaptors':
17
+ from scrapling.parser import Adaptors as cls
18
+ return cls
19
+ elif name == 'AttributesHandler':
20
+ from scrapling.core.custom_types import AttributesHandler as cls
21
+ return cls
22
+ elif name == 'TextHandler':
23
+ from scrapling.core.custom_types import TextHandler as cls
24
+ return cls
25
+ elif name == 'AsyncFetcher':
26
+ from scrapling.fetchers import AsyncFetcher as cls
27
+ return cls
28
+ elif name == 'StealthyFetcher':
29
+ from scrapling.fetchers import StealthyFetcher as cls
30
+ return cls
31
+ elif name == 'PlayWrightFetcher':
32
+ from scrapling.fetchers import PlayWrightFetcher as cls
33
+ return cls
34
+ elif name == 'CustomFetcher':
35
+ from scrapling.fetchers import CustomFetcher as cls
36
+ return cls
37
+ else:
38
+ raise AttributeError(f"module 'scrapling' has no attribute '{name}'")
39
+
40
+
41
  __all__ = ['Adaptor', 'Fetcher', 'AsyncFetcher', 'StealthyFetcher', 'PlayWrightFetcher']