File tree Expand file tree Collapse file tree 4 files changed +58
-1
lines changed
Expand file tree Collapse file tree 4 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 1- # lazimport
1+ # LazImp
Original file line number Diff line number Diff line change 1+ [build-system ]
2+ requires = [" setuptools>=61.0" ]
3+ build-backend = " setuptools.build_meta"
4+
5+ [project ]
6+ name = " lazimp"
7+ version = " 0.0.1"
8+ authors = [
9+ { name = " Dorian Turba" , email = " dturba.pro@protonmail.com" },
10+ ]
11+ description = " Lazy import of packages and modules"
12+ readme = " README.md"
13+ requires-python = " >=3.10"
14+ classifiers = [
15+ " Programming Language :: Python :: 3" ,
16+ " License :: OSI Approved :: MIT License" ,
17+ " Operating System :: OS Independent" ,
18+ " Development Status :: 2 - Pre-Alpha" ,
19+ " Programming Language :: Python :: 3.10" ,
20+ " Programming Language :: Python :: 3.11" ,
21+ ]
22+
23+ [project .urls ]
24+ "Homepage" = " https://github.com/Vikka/lazimport"
25+ "Bug Tracker" = " https://github.com/Vikka/lazimport/issues"
Original file line number Diff line number Diff line change 1+
Original file line number Diff line number Diff line change 1+ import collections .abc
2+
3+
4+ def lazy_import (
5+ bare_import : collections .abc .Container | None = None ,
6+ sub_import : collections .abc .Mapping [str , str | None ] | None = None ,
7+ ):
8+ if bare_import is None :
9+ bare_import = set ()
10+ if sub_import is None :
11+ sub_import = {}
12+
13+ import functools
14+ import importlib
15+
16+ @functools .cache
17+ def getattr_ (name : str ):
18+ if name not in bare_import and name not in sub_import :
19+ raise AttributeError (
20+ f'module { __name__ !r} has no attribute { name !r} '
21+ ) from None
22+
23+ try :
24+ return importlib .import_module (f'.{ name } ' ,
25+ sub_import .get (name , str ))
26+ except ModuleNotFoundError :
27+ raise AttributeError (
28+ f'module { __name__ !r} has no attribute { name !r} '
29+ ) from None
30+
31+ return getattr_
You can’t perform that action at this time.
0 commit comments