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.
- en English
- zh_CN 简体ä¸ć–‡
$ pip install lazyrMake numpy become a lazy module, for example:
>>> import lazyr
>>> lazyr.register("numpy") # numpy becomes a lazy module
LazyModule(numpy) # this is the LazyModule object
>>> import numpy as np # numpy is not loaded since it's lazy
>>> np
LazyModule(numpy) # np is assigned the LazyModule object instead
>>> arr = np.array([]) # numpy is actually loaded now
>>> np
<module 'numpy' from '/../..'>There is also a simpler way to create a lazy module, but it may cause type hints to lose efficacy:
>>> np = lazyr.register("numpy")Use islazy() to check if a module is lazy or not:
>>> scipy = lazyr.register("scipy")
>>> lazyr.islazy(scipy)
TrueThe 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(scipy) # scipy is woken up and loaded
>>> lazyr.islazy(scipy)
FalseYou can make a module even lazier by setting the ignore parameter of register(), which specifies the names of submodules to be ignored. The ignored submodules will become lazy modules, too.
>>> lazyr.register("pandas", ignore=["DataFrame", "Series"]) # make DataFrame and Series lazy modules
LazyModule(pandas, ignore=['DataFrame', 'Series'])The statement above has roughly the same effect as the following code piece:
>>> _, _ = lazyr.register("pandas.DataFrame"), lazyr.register("pandas.Series")Use listall() to check all the inactivated lazy modules in the system:
>>> lazyr.listall()
[LazyModule(pandas, ignore=['Series', 'DataFrame']), LazyModule(pandas.DataFrame), LazyModule(pandas.Series)]Specify the verbose parameter when calling register() to see what exactly will happen to a lazy module during the runtime:
>>> _ = lazyr.register("matplotlib.pyplot", verbose=2)
INFO:lazyr:register --> matplotlib.pyplot
INFO:lazyr:register --> matplotlib
>>> import matplotlib.pyplot as plt
DEBUG:lazyr:access --> matplotlib.pyplot.__spec__
DEBUG:lazyr:access --> matplotlib.__spec__
DEBUG:lazyr:access --> matplotlib.pyplot
>>> plot = plt.plot
DEBUG:lazyr:access --> matplotlib.pyplot.plot
INFO:lazyr:load --> matplotlib.pyplot(.plot)This project falls under the BSD 3-Clause License.
- Updated
register(): verbosity will be set to the argumentverboseif lazy module exists. - Beautified the logging messages.
- Updated
register(): now error will be raised when attempting relative import with no known parent package.
- Updated the logging function.
- New global variable
VERBOSE.
- Even objects that are not modules can be registered as lazy-modules now, e.g.,
pandas.DataFrame,numpy.array, etc. - The statement
register("foo", ignore=["bar"])will have the same effect asregister("foo.bar")now. - New function
list(), for checking all the inactivated lazy modules in the system.
- Updated README.
- New function
islazy(), for checking the status of a module. - Improved the representational strings of lazy modules.
- Various improvements.
- Prettier logs.
- Fixed the meta-data.
- Performance enhancements.
- Updated LICENSE.
- Removed unnecessary objects from the main
lazyrnamespace.
- Improved logging:
- Created a separate logger named 'lazyr' for lazy modules;
- More detailed logs when
verbose> 0.
LazyModuleno longer activated by_ipython_*()or_repr_*()methods.
- Various improvements.
- New function
wakeup(), for compulsively activating modules.
- Initial release.