# Lazy Import
[![Supported Python Versions](https://img.shields.io/pypi/pyversions/lz-import/0.1.2)](https://pypi.org/project/lz-import/) [![PyPI version](https://badge.fury.io/py/lz-import.svg)](https://badge.fury.io/py/lz-import)
### Installation
```
pip install lz-import
# Or
poetry add lz-import
```
### Usage 1
```python
# File: file_takes_long_time_to_import.py
init = initializer()
class Module:
...
print("imported!")
```
```python
from lazy_import import lazy_import
with lazy_import():
from file_takes_long_time_to_import import Module # Not imported yet
def run():
Module()
run() # Now Module is imported.
# print imported!
```
Module will be imported when the `__call__` or `__getattr__` methods are called.
See [`tests/test_load_later.py`](https://github.com/Ja-sonYun/lazy-import/blob/main/tests/test_load_later.py)
### Usage 2
```python
# File: company.py
from lazy_import import lazy_import
with lazy_import():
from user import User
class Company:
name = "company"
def get_user(self) -> User:
return User()
```
```python
# File: user.py
from lazy_import import lazy_import
with lazy_import():
from company import Company
class User:
name = "user"
def get_company(self) -> Company:
return Company()
if __name__ == "__main__":
company = User.get_company()
```
User will be imported when the `__call__` or `__getattr__` methods are called.
This example codes are implemented in the tests folder. See [`tests/test_user.py`](https://github.com/Ja-sonYun/lazy-import/blob/main/tests/test_user.py) and [`tests/test_company.py`](https://github.com/Ja-sonYun/lazy-import/blob/main/tests/test_company.py).
#### NOTE
- Keep in mind that the class of lazy imported is not the same class with your original `User` class. It is wrapped by another class inside of `lazy_import()`.
- Only work for module or class.
#### TODO
This library currently doesn't support follow syntax:
```python
with lazy_import():
# these are actually possible but currently not implmented...
import user
from user import User as user
```
### How it works?
We can find out which files to import by parsing bytecodes inside the `with` syntax. After that, just wrap the value to be imported.
Raw data
{
"_id": null,
"home_page": "https://github.com/Ja-sonYun/lazy_import",
"name": "lz-import",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9,<3.12",
"maintainer_email": "",
"keywords": "import,lazy,circular",
"author": "Ja-sonYun",
"author_email": "killa30867@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/9b/3e/2e5d4eb3825c076b50a8e595d9dc58c1d138e26fb71e72642957187b0bb4/lz_import-0.1.2.tar.gz",
"platform": null,
"description": "# Lazy Import\n[![Supported Python Versions](https://img.shields.io/pypi/pyversions/lz-import/0.1.2)](https://pypi.org/project/lz-import/) [![PyPI version](https://badge.fury.io/py/lz-import.svg)](https://badge.fury.io/py/lz-import)\n\n\n### Installation\n```\npip install lz-import\n# Or\npoetry add lz-import\n```\n\n### Usage 1\n```python\n# File: file_takes_long_time_to_import.py\ninit = initializer()\n\nclass Module:\n ...\n\nprint(\"imported!\")\n```\n\n```python\nfrom lazy_import import lazy_import\nwith lazy_import():\n from file_takes_long_time_to_import import Module # Not imported yet\n\ndef run():\n Module()\n\nrun() # Now Module is imported.\n# print imported!\n```\nModule will be imported when the `__call__` or `__getattr__` methods are called.\nSee [`tests/test_load_later.py`](https://github.com/Ja-sonYun/lazy-import/blob/main/tests/test_load_later.py)\n\n\n### Usage 2\n```python\n# File: company.py\nfrom lazy_import import lazy_import\n\nwith lazy_import():\n from user import User\n\nclass Company:\n name = \"company\"\n\n def get_user(self) -> User:\n return User()\n\n```\n\n```python\n# File: user.py\nfrom lazy_import import lazy_import\n\nwith lazy_import():\n from company import Company\n\nclass User:\n name = \"user\"\n\n def get_company(self) -> Company:\n return Company()\n\nif __name__ == \"__main__\":\n company = User.get_company()\n```\n\nUser will be imported when the `__call__` or `__getattr__` methods are called. \nThis example codes are implemented in the tests folder. See [`tests/test_user.py`](https://github.com/Ja-sonYun/lazy-import/blob/main/tests/test_user.py) and [`tests/test_company.py`](https://github.com/Ja-sonYun/lazy-import/blob/main/tests/test_company.py).\n\n#### NOTE\n- Keep in mind that the class of lazy imported is not the same class with your original `User` class. It is wrapped by another class inside of `lazy_import()`.\n- Only work for module or class.\n\n#### TODO\nThis library currently doesn't support follow syntax:\n```python\nwith lazy_import():\n # these are actually possible but currently not implmented...\n import user \n from user import User as user\n```\n\n\n### How it works?\nWe can find out which files to import by parsing bytecodes inside the `with` syntax. After that, just wrap the value to be imported.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/Ja-sonYun/lazy_import",
"Repository": "https://github.com/Ja-sonYun/lazy_import"
},
"split_keywords": [
"import",
"lazy",
"circular"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "af23e7e58bdbc82f18466f798afd7c346cbc42a387ef33cbb7a70aa18da3b8d9",
"md5": "2d030ea3d37b41996397ded0ffa4c832",
"sha256": "5355893168d32a083c10c6a2fd604153e34173d37e320cd8b2bf787298b02262"
},
"downloads": -1,
"filename": "lz_import-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2d030ea3d37b41996397ded0ffa4c832",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9,<3.12",
"size": 5103,
"upload_time": "2023-05-16T14:13:29",
"upload_time_iso_8601": "2023-05-16T14:13:29.628351Z",
"url": "https://files.pythonhosted.org/packages/af/23/e7e58bdbc82f18466f798afd7c346cbc42a387ef33cbb7a70aa18da3b8d9/lz_import-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "9b3e2e5d4eb3825c076b50a8e595d9dc58c1d138e26fb71e72642957187b0bb4",
"md5": "4fb9435876ee6248a78bb8b419340263",
"sha256": "33a7c266e89804c3120c8faebe82e750edf5aa135f1525596d5852422e7a3407"
},
"downloads": -1,
"filename": "lz_import-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "4fb9435876ee6248a78bb8b419340263",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9,<3.12",
"size": 4559,
"upload_time": "2023-05-16T14:13:32",
"upload_time_iso_8601": "2023-05-16T14:13:32.895519Z",
"url": "https://files.pythonhosted.org/packages/9b/3e/2e5d4eb3825c076b50a8e595d9dc58c1d138e26fb71e72642957187b0bb4/lz_import-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-05-16 14:13:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Ja-sonYun",
"github_project": "lazy_import",
"github_not_found": true,
"lcname": "lz-import"
}