Skip to main content

Creates lazily-imported modules in a more readable and safer way.

Project description

lazyr

Creates lazily-imported modules in a more readable and safer way.

A lazily-imported module (or a lazy module, to be short) is not physically loaded in the Python environment until its attributes are being accessed. This could be useful when you are importing some modules that are hardly used but take a lot of time to be loaded.

Installation

$ pip install lazyr

Usage

Make a lazy module

Make pandas become a lazy module, for example:

>>> import lazyr
>>> lazyr.register("pandas") # pandas is a lazy module from now on
LazyModule(pandas)

>>> import pandas as pd
>>> pd
LazyModule(pandas)

>>> df = pd.DataFrame # pandas is actually loaded now
>>> df
<class 'pandas.core.frame.DataFrame'>

There is also a simpler way to create a lazy module, but it may cause type hints to lose efficacy:

>>> import lazyr
>>> pd = lazyr.register("pandas")
>>> pd
LazyModule(pandas)

Check if a module is lazy

Use islazy() to check if a module is lazy or not:

>>> lazyr.islazy(pd)
True

Wake up a module

The lazy modules are not physically loaded until their attrubutes are imported or used, but sometimes you may want to activate a lazy module without accessing any of its attributes. On that purpose, you can 'wake' up the module like this:

>>> lazyr.wakeup(pd) # pandas is woken up and loaded
>>> lazyr.islazy(pd)
False

Ignore attributes

You can make a module even lazier by setting the ignore parameter of register(), which specifies the names of attributes to be ignored. The values of the ignored attributes will be set to None, and a lazy module will no longer be activated by the access to them.

>>> import lazyr
>>> lazyr.register("pandas", ignore=["DataFrame", "Series"]) # Ignoring DataFrame and Series
LazyModule(pandas, ignore=['DataFrame', 'Series'])

>>> from pandas import DataFrame # pandas is not loaded; DataFrame is set to None
>>> from pandas import Series # pandas is not loaded; Series is set to None
>>> from pandas import io # pandas is loaded because 'io' is not an ignored attribute

>>> from pandas import DataFrame # DataFrame is loaded this time 
>>> DataFrame
<class 'pandas.core.frame.DataFrame'>

Logging

Specify the verbose parameter when calling register() to see what exactly will happen to a lazy module during the runtime:

>>> import lazyr
>>> _ = lazyr.register("pandas", verbose=2)
INFO:lazyr:import:pandas

>>> import pandas as pd
DEBUG:lazyr:access:pandas.__spec__

>>> df = pd.DataFrame
DEBUG:lazyr:access:pandas.DataFrame
INFO:lazyr:load:pandas(.DataFrame)

See Also

Github repository

PyPI project

License

This project falls under the BSD 3-Clause License.

History

v0.0.16

  • New function islazy(), for checking the status of a module.
  • Improved the representational strings of lazy modules.

v0.0.15

  • Various improvements.

v0.0.12

  • Prettier logs.

v0.0.11

  • Fixed the meta-data.
  • Performance enhancements.

v0.0.9

  • Updated LICENSE.

v0.0.7

  • Removed unnecessary objects from the main lazyr namespace.

v0.0.6

  • Improved logging:
    • Created a separate logger named 'lazyr' for lazy modules;
    • More detailed logs when verbose > 0.

v0.0.4

  • LazyModule no longer activated by _ipython_*() or _repr_*() methods.

v0.0.3

  • Various improvements.

v0.0.2

  • New function wakeup(), for compulsively activating modules.

v0.0.1

  • Initial release.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazyr-0.0.16-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file lazyr-0.0.16-py2.py3-none-any.whl.

File metadata

  • Download URL: lazyr-0.0.16-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.13

File hashes

Hashes for lazyr-0.0.16-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 60572bae906da6cdd2eb8f66acf40a8a4ddc899a82a327392c343d695b75ee75
MD5 55826e82a7dccb46a143f764f357347b
BLAKE2b-256 3a4983dbc2e6b449f9d65dc82391d655008f9f0ed7714d95a7126a2ccd271dda

See more details on using hashes here.

Supported by

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