slothy


Nameslothy JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/bswck/slothy
SummarySuper-easy lazy importing in Python.
upload_time2024-07-05 10:05:43
maintainerNone
docs_urlNone
authorbswck
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # <div align="center">slothy<br>[![Supported Python versions](https://img.shields.io/pypi/pyversions/slothy.svg?logo=python&label=Python)](https://pypi.org/project/slothy/) [![Package version](https://img.shields.io/pypi/v/slothy?label=PyPI)](https://pypi.org/project/slothy/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/slothy)](https://pypistats.org/packages/slothy)</div>

[![Tests](https://github.com/bswck/slothy/actions/workflows/test.yml/badge.svg)](https://github.com/bswck/slothy/actions/workflows/test.yml)
[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/bswck/slothy.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/bswck/slothy)

Super-easy lazy importing in Python.

Intended to be used as a guard for expensive or type-checking imports.

> [!tip]
> In case you're just looking around, feel free to install the latest **beta**
> version of _slothy_ from PyPI and have some fun experimenting.<br>
> Don't forget to share your feedback in [the issue tracker](/issues)!

# Usage

```pycon
>>> from slothy import lazy_importing
>>> 
>>> with lazy_importing():
...     from asyncio import get_event_loop, run, erroneous_import
...     print(get_event_loop)
...     print(run)
...     print(erroneous_import)
... 
<from asyncio import get_event_loop, ... ("<stdin>", line 2)>
<from asyncio import ..., run, ... ("<stdin>", line 2)>
<from asyncio import ..., erroneous_import ("<stdin>", line 2)>
>>>
>>> get_event_loop
<built-in function get_event_loop>
>>>
>>> globals()["run"]
<function run at 0xdead1337beef>
>>>
>>> erroneous_import
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./slothy/_importing.py", line 423, in __eq__
    self._import(their_import)
  File "./slothy/_importing.py", line 304, in __import
    raise exc from None
  File "./slothy/_importing.py", line 286, in __import
    obj = _import_item_from_list(
  File "./slothy/_importing.py", line 203, in _import_item_from_list
    raise ImportError(msg) from None
ImportError: cannot import name 'erroneous_import' from 'asyncio'
(caused by delayed execution of "<stdin>", line 2)
```

By default, `with lazy_importing()` statements fail immediately on unsupported Python
implementations, i.e. those that don't define [`sys._getframe`](https://docs.python.org/3/library/sys.html#sys._getframe). To disable this behavior,
which might be particularly useful in libraries, use `with lazy_importing(prevent_eager=False)`.

# Credits
Many thanks to Jelle Zijlstra [@JelleZijlstra](https://github.com/JelleZijlstra) who wrote a [basic
dict key lookup-based lazy importing implementation](https://gist.github.com/JelleZijlstra/23c01ceb35d1bc8f335128f59a32db4c)
that is now the core solution of slothy.

Kudos to Carl Meyer [@carljm](https://github.com/carljm) who willingly sacrificed his time
to consult the project with me and share his deep knowledge of the problem at the bigger picture.
His experience with [PEP 690](https://peps.python.org/pep-0690) as a Meta software engineer
significantly helped me.

I'm very grateful to [@jimfulton](https://github.com/jimfulton) for [making the library possible in the first place](https://github.com/python/cpython/commit/d47a0a86b4ae4afeb17d8e64e1c447e4d4025f10) almost 30 years ago.

Special thanks to Alex Waygood [@AlexWaygood](https://github.com/AlexWaygood) who made this project possible
by sharing his knowledge of CPython implementation details regarding name lookup behavior.

Shoutout to Will McGugan [@willmcgugan](https://github.com/willmcgugan) who supported the idea of slothy
from the very beginning and [promoted the project on Twitter](https://twitter.com/willmcgugan/status/1781327396773208427).

# Installation
You might simply install it with pip:

```shell
pip install slothy
```

If you use [Poetry](https://python-poetry.org/), then you might want to run:

```shell
poetry add slothy
```

## For Contributors
[![Rye](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/rye/main/artwork/badge.json)](https://rye.astral.sh)
[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)
<!--
This section was generated from scaffops/python@0.0.2rc-238-g68b0ab8.
Instead of changing this particular file, you might want to alter the template:
https://github.com/scaffops/python/tree/0.0.2rc-238-g68b0ab8/project/README.md.jinja
-->
> [!Note]
> If you use Windows, it is highly recommended to complete the installation in the way presented below through [WSL2](https://learn.microsoft.com/en-us/windows/wsl/install).
1.  Fork the [slothy repository](https://github.com/bswck/slothy) on GitHub.

1.  [Install Poetry](https://python-poetry.org/docs/#installation).<br/>
    Poetry is an amazing tool for managing dependencies & virtual environments, building packages and publishing them.
    You might use [pipx](https://github.com/pypa/pipx#readme) to install it globally (recommended):

    ```shell
    pipx install poetry
    ```

    <sub>If you encounter any problems, refer to [the official documentation](https://python-poetry.org/docs/#installation) for the most up-to-date installation instructions.</sub>

    Be sure to have Python 3.8 installed—if you use [Rye](https://rye.astral.sh/guide/basics/), simply run:

    ```bash
    rye toolchain fetch 3.8
    ```

1.  Clone your fork locally and install dependencies.

    ```shell
    git clone https://github.com/your-username/slothy path/to/slothy
    cd path/to/slothy
    poetry env use $(cat .python-version)
    poetry install
    ```

    Next up, simply activate the virtual environment and install pre-commit hooks:

    ```shell
    poetry shell
    pre-commit install
    ```

For more information on how to contribute, check out [CONTRIBUTING.md](https://github.com/bswck/slothy/blob/HEAD/CONTRIBUTING.md).<br/>
Always happy to accept contributions! ❤️

# Legal Info
© Copyright by Bartosz Sławecki ([@bswck](https://github.com/bswck)).
<br />This software is licensed under the terms of [MIT License](https://github.com/bswck/slothy/blob/HEAD/LICENSE).


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/bswck/slothy",
    "name": "slothy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "bswck",
    "author_email": "bartoszpiotrslawecki@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/3d/08/28d16cafa1aba870a483a320c1fef42f398e7f91b98cbccb21771f58ae66/slothy-1.0.0.tar.gz",
    "platform": null,
    "description": "# <div align=\"center\">slothy<br>[![Supported Python versions](https://img.shields.io/pypi/pyversions/slothy.svg?logo=python&label=Python)](https://pypi.org/project/slothy/) [![Package version](https://img.shields.io/pypi/v/slothy?label=PyPI)](https://pypi.org/project/slothy/) [![PyPI - Downloads](https://img.shields.io/pypi/dm/slothy)](https://pypistats.org/packages/slothy)</div>\n\n[![Tests](https://github.com/bswck/slothy/actions/workflows/test.yml/badge.svg)](https://github.com/bswck/slothy/actions/workflows/test.yml)\n[![Coverage](https://coverage-badge.samuelcolvin.workers.dev/bswck/slothy.svg)](https://coverage-badge.samuelcolvin.workers.dev/redirect/bswck/slothy)\n\nSuper-easy lazy importing in Python.\n\nIntended to be used as a guard for expensive or type-checking imports.\n\n> [!tip]\n> In case you're just looking around, feel free to install the latest **beta**\n> version of _slothy_ from PyPI and have some fun experimenting.<br>\n> Don't forget to share your feedback in [the issue tracker](/issues)!\n\n# Usage\n\n```pycon\n>>> from slothy import lazy_importing\n>>> \n>>> with lazy_importing():\n...     from asyncio import get_event_loop, run, erroneous_import\n...     print(get_event_loop)\n...     print(run)\n...     print(erroneous_import)\n... \n<from asyncio import get_event_loop, ... (\"<stdin>\", line 2)>\n<from asyncio import ..., run, ... (\"<stdin>\", line 2)>\n<from asyncio import ..., erroneous_import (\"<stdin>\", line 2)>\n>>>\n>>> get_event_loop\n<built-in function get_event_loop>\n>>>\n>>> globals()[\"run\"]\n<function run at 0xdead1337beef>\n>>>\n>>> erroneous_import\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\n  File \"./slothy/_importing.py\", line 423, in __eq__\n    self._import(their_import)\n  File \"./slothy/_importing.py\", line 304, in __import\n    raise exc from None\n  File \"./slothy/_importing.py\", line 286, in __import\n    obj = _import_item_from_list(\n  File \"./slothy/_importing.py\", line 203, in _import_item_from_list\n    raise ImportError(msg) from None\nImportError: cannot import name 'erroneous_import' from 'asyncio'\n(caused by delayed execution of \"<stdin>\", line 2)\n```\n\nBy default, `with lazy_importing()` statements fail immediately on unsupported Python\nimplementations, i.e. those that don't define [`sys._getframe`](https://docs.python.org/3/library/sys.html#sys._getframe). To disable this behavior,\nwhich might be particularly useful in libraries, use `with lazy_importing(prevent_eager=False)`.\n\n# Credits\nMany thanks to Jelle Zijlstra [@JelleZijlstra](https://github.com/JelleZijlstra) who wrote a [basic\ndict key lookup-based lazy importing implementation](https://gist.github.com/JelleZijlstra/23c01ceb35d1bc8f335128f59a32db4c)\nthat is now the core solution of slothy.\n\nKudos to Carl Meyer [@carljm](https://github.com/carljm) who willingly sacrificed his time\nto consult the project with me and share his deep knowledge of the problem at the bigger picture.\nHis experience with [PEP 690](https://peps.python.org/pep-0690) as a Meta software engineer\nsignificantly helped me.\n\nI'm very grateful to [@jimfulton](https://github.com/jimfulton) for [making the library possible in the first place](https://github.com/python/cpython/commit/d47a0a86b4ae4afeb17d8e64e1c447e4d4025f10) almost 30 years ago.\n\nSpecial thanks to Alex Waygood [@AlexWaygood](https://github.com/AlexWaygood) who made this project possible\nby sharing his knowledge of CPython implementation details regarding name lookup behavior.\n\nShoutout to Will McGugan [@willmcgugan](https://github.com/willmcgugan) who supported the idea of slothy\nfrom the very beginning and [promoted the project on Twitter](https://twitter.com/willmcgugan/status/1781327396773208427).\n\n# Installation\nYou might simply install it with pip:\n\n```shell\npip install slothy\n```\n\nIf you use [Poetry](https://python-poetry.org/), then you might want to run:\n\n```shell\npoetry add slothy\n```\n\n## For Contributors\n[![Rye](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/rye/main/artwork/badge.json)](https://rye.astral.sh)\n[![Poetry](https://img.shields.io/endpoint?url=https://python-poetry.org/badge/v0.json)](https://python-poetry.org/)\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![Pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white)](https://github.com/pre-commit/pre-commit)\n<!--\nThis section was generated from scaffops/python@0.0.2rc-238-g68b0ab8.\nInstead of changing this particular file, you might want to alter the template:\nhttps://github.com/scaffops/python/tree/0.0.2rc-238-g68b0ab8/project/README.md.jinja\n-->\n> [!Note]\n> If you use Windows, it is highly recommended to complete the installation in the way presented below through [WSL2](https://learn.microsoft.com/en-us/windows/wsl/install).\n1.  Fork the [slothy repository](https://github.com/bswck/slothy) on GitHub.\n\n1.  [Install Poetry](https://python-poetry.org/docs/#installation).<br/>\n    Poetry is an amazing tool for managing dependencies & virtual environments, building packages and publishing them.\n    You might use [pipx](https://github.com/pypa/pipx#readme) to install it globally (recommended):\n\n    ```shell\n    pipx install poetry\n    ```\n\n    <sub>If you encounter any problems, refer to [the official documentation](https://python-poetry.org/docs/#installation) for the most up-to-date installation instructions.</sub>\n\n    Be sure to have Python 3.8 installed\u2014if you use [Rye](https://rye.astral.sh/guide/basics/), simply run:\n\n    ```bash\n    rye toolchain fetch 3.8\n    ```\n\n1.  Clone your fork locally and install dependencies.\n\n    ```shell\n    git clone https://github.com/your-username/slothy path/to/slothy\n    cd path/to/slothy\n    poetry env use $(cat .python-version)\n    poetry install\n    ```\n\n    Next up, simply activate the virtual environment and install pre-commit hooks:\n\n    ```shell\n    poetry shell\n    pre-commit install\n    ```\n\nFor more information on how to contribute, check out [CONTRIBUTING.md](https://github.com/bswck/slothy/blob/HEAD/CONTRIBUTING.md).<br/>\nAlways happy to accept contributions! \u2764\ufe0f\n\n# Legal Info\n\u00a9 Copyright by Bartosz S\u0142awecki ([@bswck](https://github.com/bswck)).\n<br />This software is licensed under the terms of [MIT License](https://github.com/bswck/slothy/blob/HEAD/LICENSE).\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Super-easy lazy importing in Python.",
    "version": "1.0.0",
    "project_urls": {
        "Coverage": "https://coverage-badge.samuelcolvin.workers.dev/redirect/bswck/slothy",
        "Distribution": "https://pypi.org/project/slothy/",
        "Documentation": "https://slothy.readthedocs.io/en/latest/",
        "Homepage": "https://github.com/bswck/slothy",
        "Issues": "https://github.com/bswck/slothy/issues"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "149105ff6733c50f88ae544782d1c4ed6cbfd95fd3e587f86f5407281cf43f21",
                "md5": "cd85cc83f6c306f3beaf52a4396399e9",
                "sha256": "0b872fd9c45b6113f2a991985389dcbbded20378d795a9c724885f669dccd798"
            },
            "downloads": -1,
            "filename": "slothy-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cd85cc83f6c306f3beaf52a4396399e9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 10966,
            "upload_time": "2024-07-05T10:05:41",
            "upload_time_iso_8601": "2024-07-05T10:05:41.921085Z",
            "url": "https://files.pythonhosted.org/packages/14/91/05ff6733c50f88ae544782d1c4ed6cbfd95fd3e587f86f5407281cf43f21/slothy-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3d0828d16cafa1aba870a483a320c1fef42f398e7f91b98cbccb21771f58ae66",
                "md5": "50c2739d30109ee58a7026603abb13b8",
                "sha256": "7dc5011edc46e03887e2c6551d282a96ab3fe73d2378ef75be6856e625ad0357"
            },
            "downloads": -1,
            "filename": "slothy-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "50c2739d30109ee58a7026603abb13b8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 13518,
            "upload_time": "2024-07-05T10:05:43",
            "upload_time_iso_8601": "2024-07-05T10:05:43.735059Z",
            "url": "https://files.pythonhosted.org/packages/3d/08/28d16cafa1aba870a483a320c1fef42f398e7f91b98cbccb21771f58ae66/slothy-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-05 10:05:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "bswck",
    "github_project": "slothy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "slothy"
}
        
Elapsed time: 0.52642s