-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# lazimport | ||
# LazImp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 = "[email protected]" }, | ||
] | ||
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |