Skip to main content

No project description provided

Project description

Lazy Import

Installation

pip install lz-import
# Or
poetry add lz-import

Usage

# File: company.py
from user import User

class Company:
    name = "company"

    def get_user(self) -> User:
        return User()
# File: user.py
from company import Company

class User:
    name = "user"

    def get_company(self) -> Company:
        return Company()

if __name__ == "__main__":
    company = User.get_company()

This file layout will throw a circular import error, but if we can do a lazy import, it's perfectly fine. Just wrap the block of code like follow snippets.

from lazy_import import lazy_import

with lazy_import():
    from user import User

...

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 and 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_imported(). Also, this library works with mypy.

TODO

This library currently doesn't support follow syntax:

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lz_import-0.1.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

lz_import-0.1.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file lz_import-0.1.1.tar.gz.

File metadata

  • Download URL: lz_import-0.1.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1037-azure

File hashes

Hashes for lz_import-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d0d9b90a1c02b7c1a5f105be81f599886f49c057774d36cb174540666a9e94c4
MD5 9e83e43a46c4d83b9ed3e8282bbc86db
BLAKE2b-256 d8fa49f22f22b65a3c3cff50b4184b18990eb8d8801c215a1225fa2d9f0e4705

See more details on using hashes here.

File details

Details for the file lz_import-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: lz_import-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1037-azure

File hashes

Hashes for lz_import-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 88e70e3c2dcb971ec29c3982aed2889ea22b3a870c2fe6f5fe2aa7ee15c7c87b
MD5 50b0b51038be27c4322537b0264bcdbc
BLAKE2b-256 42af1931495ac105272e3c9e7eef314b96dd7b588aebc35d7d6177bd33210b8d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page