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 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")

When registering multiple modules, you can specify more than one names:

>>> lazyr.register("pandas", "scipy")
[LazyModule(pandas), LazyModule(scipy)]

Check if a module is lazy

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

>>> scipy = lazyr.register("scipy")
>>> lazyr.islazy(scipy)
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(scipy) # scipy is woken up and loaded
>>> lazyr.islazy(scipy)
False

Lazy submodules

When registering a lazy module, you may also register some of its submodules by specifying them in the submodules parameter of the register() function:

>>> lazyr.register("pandas", submodules=["DataFrame", "Series"]) # make DataFrame and Series lazy modules
LazyModule(pandas, submodules=['DataFrame', 'Series'])

The statement above has roughly the same effect as the following code piece:

>>> lazyr.register("pandas.DataFrame", "pandas.Series")
[LazyModule(pandas.DataFrame), LazyModule(pandas.Series)]

List all lazy modules

Use listall() to check all the inactivated lazy modules in the system:

>>> lazyr.listall()
[LazyModule(pandas, submodules=['Series', 'DataFrame']), LazyModule(pandas.DataFrame), LazyModule(pandas.Series)]

Logging

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

>>> with lazyr.setverbose(2):
...     _ = lazyr.register("matplotlib.pyplot")
...
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)

See Also

Github repository

PyPI project

License

This project falls under the BSD 3-Clause License.

History

v0.1.1

  • Replaced internal logging implementation with the loggings package.

v0.1.0

  • Requires python>=3.13 from now on.
  • New function setverbose() to return a context manager for setting the default verbose value for register().
  • Updated register():
    • from now on, parameter name will be positional only, and other parameter will be key-word only;
    • parameter name can accept multiple module names now;
    • renamed parameter ignore to submodules.

v0.0.24

  • Updated register(): verbosity will be set to the argument verbose if lazy module exists.
  • Beautified the logging messages.

v0.0.23

  • Updated register(): now error will be raised when attempting relative import with no known parent package.

v0.0.21

  • Updated the logging function.

v0.0.19

  • New global variable VERBOSE.

v0.0.18

  • 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 as register("foo.bar") now.
  • New function list(), for checking all the inactivated lazy modules in the system.

v0.0.17

  • Updated README.

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:
    • creates 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 Distribution

lazyr-0.1.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

lazyr-0.1.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: lazyr-0.1.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for lazyr-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4081002b239d45a1e81765e5f88b64aeda53c7c07aa07c75570d46be55125940
MD5 cd10187b204e6c05e56c2182a025dece
BLAKE2b-256 8d19316e15f610689463e72424d882a4baa85052bfa734fc4d7507cff5180350

See more details on using hashes here.

File details

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

File metadata

  • Download URL: lazyr-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for lazyr-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1830d598abd46f141aba66c848498b5f9c3b7e02db3e8dfc64b43394a3afa82
MD5 611ac3060e461567141258e80f2ec024
BLAKE2b-256 85280fdc47a07c37bda0f190aacf317a2b411415d1896e2b88a81b0c0de1b358

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