diff --git a/README.md b/README.md index 979c4cf..29d981a 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# lazimport \ No newline at end of file +# LazImp \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..ba5ebb4 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,25 @@ +[build-system] +requires = ["setuptools>=61.0"] +build-backend = "setuptools.build_meta" + +[project] +name = "lazimp" +version = "0.0.1" +authors = [ + { name = "Dorian Turba", email = "dturba.pro@protonmail.com" }, +] +description = "Lazy import of packages and modules" +readme = "README.md" +requires-python = ">=3.10" +classifiers = [ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Development Status :: 2 - Pre-Alpha", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", +] + +[project.urls] +"Homepage" = "https://github.com/Vikka/lazimport" +"Bug Tracker" = "https://github.com/Vikka/lazimport/issues" diff --git a/src/lazimp/__init__.py b/src/lazimp/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/lazimp/__init__.py @@ -0,0 +1 @@ + diff --git a/src/lazimp/main.py b/src/lazimp/main.py new file mode 100644 index 0000000..253d212 --- /dev/null +++ b/src/lazimp/main.py @@ -0,0 +1,31 @@ +import collections.abc + + +def lazy_import( + bare_import: collections.abc.Container | None = None, + sub_import: collections.abc.Mapping[str, str | None] | None = None, +): + if bare_import is None: + bare_import = set() + if sub_import is None: + sub_import = {} + + import functools + import importlib + + @functools.cache + def getattr_(name: str): + if name not in bare_import and name not in sub_import: + raise AttributeError( + f'module {__name__!r} has no attribute {name!r}' + ) from None + + try: + return importlib.import_module(f'.{name}', + sub_import.get(name, str)) + except ModuleNotFoundError: + raise AttributeError( + f'module {__name__!r} has no attribute {name!r}' + ) from None + + return getattr_