{ "info": { "author": "Manuel Nuno Melo", "author_email": "manuel.nuno.melo@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)", "Operating System :: OS Independent", "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "lazy_import\n===========\n\n|Build Status|\n\n``lazy_import`` provides a set of functions that load modules, and related\nattributes, in a lazy fashion. This allows deferring of ``ImportErrors`` to\nactual module use-time. Likewise, actual module initialization only takes place\nat use-time. This is useful when using optional dependencies with heavy loading\ntimes and/or footprints, since that cost is only paid if the module is actually\nused.\n\nFor minimal impact to other code running in the same session ``lazy_import``\nfunctionality is implemented without the use of import hooks.\n\n``lazy_import`` is compatible with Python \u2265 2.7 or \u2265 3.4.\n\nExamples: lazy module loading\n-----------------------------\n\n.. code:: python\n\n import lazy_import\n np = lazy_import.lazy_module(\"numpy\")\n # np is now available in the namespace and is listed in sys.modules under\n # the 'numpy' key:\n import sys\n sys.modules['numpy']\n # The module is present as \"Lazily-loaded module numpy\"\n\n # Subsequent imports of the same module return the lazy version present\n # in sys.modules\n import numpy # At this point numpy and np point to the same lazy module.\n # This is true for any import of 'numpy', even if from other modules!\n\n # Accessing attributes causes the full loading of the module ...\n np.pi\n # ... and the module is changed in place. np and numpy are now \n # \"\"\n\n # Lazy-importing a module that's already fully loaded returns the full\n # module instead (even if it was loaded elsewhere in the current session)\n # because there's no point in being lazy in this case:\n os = lazy_import.lazy_module(\"os\")\n # \"\"\n\nIn the above code it can be seen that issuing\n``lazy_import.lazy_module(\"numpy\")`` registers the lazy module in the\nsession-wide ``sys.modules`` registry. This means that *any* subsequent import\nof ``numpy`` in the same session, while the module is still not fully loaded,\nwill get served a lazy version of the ``numpy`` module. This will happen also\noutside the code that calls ``lazy_module``:\n\n.. code:: python\n \n import lazy_import\n np = lazy_import.lazy_module(\"numpy\")\n import module_that_uses_numpy # This module will get a lazy module upon\n # 'import numpy'\n\nNormally this is ok because the lazy module will behave pretty much as the real\nthing once fully-loaded. Still, it might be a good practice to document that\nyou're lazily importing modules so-and-so, so that users are warned.\n\nFurther uses are to delay ``ImportErrors``:\n\n.. code:: python\n\n import lazy_import\n # The following succeeds even when asking for a module that's not available\n missing = lazy_import.lazy_module(\"missing_module\")\n\n missing.some_attr # This causes the full loading of the module, which now fails.\n \"ImportError: __main__ attempted to use a functionality that requires module\n missing_module, but it couldn't be loaded. Please install missing_module and retry.\"\n\n\nSubmodules work too:\n\n.. code:: python\n\n import lazy_import\n mod = lazy_import.lazy_module(\"some.sub.module\")\n # mod now points to the some.sub.module lazy module\n # equivalent to \"from some.sub import module as mod\"\n\n # Alternatively the returned reference can be made to point to the\n # base module:\n some = lazy_import.lazy_module(\"some.sub.module\", level=\"base\")\n\n # This is equivalent to \"import some.sub.module\" in that only the base\n # module's name is added to the namespace. All submodules must be accessed\n # via that:\n some.sub # Returns lazy module 'some.sub' without triggering full loading.\n some.sub.attr # Triggers full loading of 'some' and 'some.sub'.\n some.sub.module.function() # Triggers loading also of 'some.sub.module'.\n\n\nFinally, if you want to mark some modules and submodules your package imports\nas always being lazy, it is as simple as lazily importing them at the root\n`__init__.py` level. Other files can then import all modules normally, and\nthose that have already been loaded as lazy in `__init__.py` will remain so:\n\n.. code:: python\n\n # in __init__.py:\n\n import lazy_import\n lazy_import.lazy_module(\"numpy\")\n lazy_import.lazy_module(\"scipy.stats\")\n\n\n # then, in any other file in the package just use the imports normally:\n\n import requests # This one is not lazy.\n import numpy # This one is lazy, as long as no other code caused its\n # loading in the meantime.\n import scipy # This one is also lazy. It was lazily loaded as part of the\n # lazy loading of scipy.stats.\n import scipy.stats # Also lazy.\n import scipy.linalg # Uh-oh, we didn't lazily import the 'linalg' submodule\n # earlier, and importing it like this here will cause\n # both scipy and scipy.linalg (but not scipy.stats) to\n # immediately become fully loaded.\n\n\nExamples: lazy callable loading\n-------------------------------\n\nTo emulate the ``from some.module import function`` syntax ``lazy_module``\nprovides ``lazy_callable``. It returns a wrapper function. Only upon being\ncalled will it trigger the loading of the target module and the calling of the\ntarget callable (function, class, etc.).\n\n.. code:: python\n\n import lazy_import\n fn = lazy_import.lazy_callable(\"numpy.arange\")\n # 'numpy' is now in sys.modules and is 'Lazily-loaded module numpy'\n\n fn(10)\n # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n\n``lazy_callable`` is only useful when the target callable is going to be called:\n\n.. code:: python\n\n import lazy_import\n cl = lazy_import.lazy_callable(\"numpy.ndarray\") # a class\n\n obj = cl([1, 2]) # This works OK (and also triggers the loading of numpy)\n\n class MySubclass(cl): # This fails because cl is just a wrapper,\n pass # not an actual class.\n\n\nInstallation\n------------\n\n.. code:: bash\n\n pip install lazy_import\n\nOr, to include dependencies needed to run regression tests:\n\n.. code:: bash\n\n pip install lazy_import[test]\n\nTests\n-----\n\nThe ``lazy_module`` module comes with a series of tests. If you install with\ntest dependencies (see above), just run\n\n.. code:: python\n\n import lazy_import.test_lazy\n lazy_import.test_lazy.run()\n # This will automatically parallelize over the available number of cores\n\nAlternatively, tests can be run from the command line:\n\n.. code:: bash\n\n pytest -n 4 --boxed -v --pyargs lazy_import\n # (replace '4' with the number of cores in your machine, or set to 1 if\n # you'd rather test in serial)\n\nTests depend only on |pytest|_ and |pytest-xdist|_, so if you didn't install\nthem along ``lazy_import`` (as described under `Installation`_) just run\n\n.. code:: bash\n\n pip install pytest pytest-xdist\n\nNote that ``pytest-xdist`` is required even for serial testing because of its\n``--boxed`` functionality.\n\nLicense\n-------\n\n``lazy_import`` is released under GPL v3. It was based on code from the\n|importing|_ module from the PEAK_ package. The licenses for both\n``lazy_import`` and the PEAK package are included in the ``LICENSE`` file. The\nrespective license notices are reproduced here:\n\n lazy_import \u2014 a module to allow lazy importing of python modules\n\n Copyright (C) 2017-2018 Manuel Nuno Melo \n\n lazy_import is free software: you can redistribute it and/or modify\n it under the terms of the GNU General Public License as published by\n the Free Software Foundation, either version 3 of the License, or\n (at your option) any later version.\n\n lazy_import is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n GNU General Public License for more details.\n\n You should have received a copy of the GNU General Public License\n along with lazy_import. If not, see .\n\n\nThe PEAK ``importing`` code is\n\n Copyright (C) 1996-2004 by Phillip J. Eby and Tyler C. Sarna.\n All rights reserved. This software may be used under the same terms\n as Zope or Python. THERE ARE ABSOLUTELY NO WARRANTIES OF ANY KIND.\n Code quality varies between modules, from \"beta\" to \"experimental\n pre-alpha\". :)\n \nCode pertaining to lazy loading from PEAK ``importing`` was included in\n``lazy_import``, modified in a number of ways. These are detailed in the\n``CHANGELOG`` file of ``lazy_import``. Changes mainly involved Python 3\ncompatibility, extension to allow customizable behavior, and added\nfunctionality (lazy importing of callable objects).\n\n\n.. |Build Status| image:: https://api.travis-ci.org/mnmelo/lazy_import.svg\n :target: https://travis-ci.org/mnmelo/lazy_import\n\n.. |importing| replace:: ``importing``\n.. |pytest| replace:: ``pytest``\n.. |pytest-xdist| replace:: ``pytest-xdist``\n\n.. _importing: http://peak.telecommunity.com/DevCenter/Importing\n.. _PEAK: http://peak.telecommunity.com/DevCenter/FrontPage\n.. _pytest: https://docs.pytest.org/en/latest/\n.. _pytest-xdist: https://pypi.python.org/pypi/pytest-xdist", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/mnmelo/lazy_import", "keywords": "", "license": "GPL", "maintainer": "", "maintainer_email": "", "name": "lazy-import", "package_url": "https://pypi.org/project/lazy-import/", "platform": "any", "project_url": "https://pypi.org/project/lazy-import/", "project_urls": { "Homepage": "https://github.com/mnmelo/lazy_import" }, "release_url": "https://pypi.org/project/lazy-import/0.2.2/", "requires_dist": null, "requires_python": "", "summary": "A module for lazy loading of Python modules", "version": "0.2.2" }, "last_serial": 3509931, "releases": { "0.2": [ { "comment_text": "", "digests": { "md5": "5698fa1bbfff309a0d238a2c84c5008f", "sha256": "1fafd8016bb652cc55e04b25d732f1de6cc31e1d15fa1b22ab236c3a85ae6b62" }, "downloads": -1, "filename": "lazy_import-0.2.tar.gz", "has_sig": false, "md5_digest": "5698fa1bbfff309a0d238a2c84c5008f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14519, "upload_time": "2018-01-17T14:43:34", "url": "https://files.pythonhosted.org/packages/48/5a/5fcd8d0708435b00aee85ecbbb2acf942ada1880fbb2ba9f9f4bbb2f0499/lazy_import-0.2.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "c7432383c598033c842741b9dd0373af", "sha256": "83ab2c1c26a838dbd5732f4d48b730e4552d0f3a33f171133a05bbf6f5cb6d95" }, "downloads": -1, "filename": "lazy_import-0.2.1.tar.gz", "has_sig": false, "md5_digest": "c7432383c598033c842741b9dd0373af", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14528, "upload_time": "2018-01-17T18:00:34", "url": "https://files.pythonhosted.org/packages/28/00/e782668b7798ac2dcc90032c1d6fface94f795d3e4c7a8c6bba6956e1fed/lazy_import-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "564449119705a15eb288fa94d21b018a", "sha256": "2149aef8579459407c62cfeccf118527939c9931ace124f355236360644f8a3d" }, "downloads": -1, "filename": "lazy_import-0.2.2.tar.gz", "has_sig": false, "md5_digest": "564449119705a15eb288fa94d21b018a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15265, "upload_time": "2018-01-22T02:19:32", "url": "https://files.pythonhosted.org/packages/44/2e/5378f9b9cbc893826c2ecb022646c97ece9efbaad351adf89425fff33990/lazy_import-0.2.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "564449119705a15eb288fa94d21b018a", "sha256": "2149aef8579459407c62cfeccf118527939c9931ace124f355236360644f8a3d" }, "downloads": -1, "filename": "lazy_import-0.2.2.tar.gz", "has_sig": false, "md5_digest": "564449119705a15eb288fa94d21b018a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15265, "upload_time": "2018-01-22T02:19:32", "url": "https://files.pythonhosted.org/packages/44/2e/5378f9b9cbc893826c2ecb022646c97ece9efbaad351adf89425fff33990/lazy_import-0.2.2.tar.gz" } ] }