Skip to content

Commit 5931dbb

Browse files
committed
[feat] lazy loading ok
1 parent 73c943e commit 5931dbb

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# lazimport
1+
# LazImp

pyproject.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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"

src/lazimp/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/lazimp/main.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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_

0 commit comments

Comments
 (0)