Skip to content

Commit

Permalink
[feat] lazy loading ok
Browse files Browse the repository at this point in the history
  • Loading branch information
p1-dta committed Sep 17, 2022
1 parent 73c943e commit 5931dbb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# lazimport
# LazImp
25 changes: 25 additions & 0 deletions pyproject.toml
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"
1 change: 1 addition & 0 deletions src/lazimp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

31 changes: 31 additions & 0 deletions src/lazimp/main.py
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_

0 comments on commit 5931dbb

Please sign in to comment.