Skip to content

Commit 7cba181

Browse files
committed
[fix & refactor] no more main.py, lazy_import in __init__.py
1 parent 5931dbb commit 7cba181

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "lazimp"
7-
version = "0.0.1"
7+
version = "0.0.2"
88
authors = [
99
{ name = "Dorian Turba", email = "dturba.pro@protonmail.com" },
1010
]
@@ -15,7 +15,7 @@ classifiers = [
1515
"Programming Language :: Python :: 3",
1616
"License :: OSI Approved :: MIT License",
1717
"Operating System :: OS Independent",
18-
"Development Status :: 2 - Pre-Alpha",
18+
"Development Status :: 3 - Alpha",
1919
"Programming Language :: Python :: 3.10",
2020
"Programming Language :: Python :: 3.11",
2121
]

src/lazimp/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1+
import collections.abc
12

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_

src/lazimp/main.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)