""" Lazy loading modules """
import importlib
[docs]
class LazyModule:
"""
A partial implementation of a lazily imported module.
This isn't a general solution, since it doesn't actually
create a module-like object, but the LazyLoader solution
in importlib seems not to cope with submodules like astropy.table,
or pyarrow.parquet, and always imports the full module.
"""
def __init__(self, name):
self.name = name
self._module = None
@property
def module(self):
if self._module is not None:
return self._module
try:
self._module = importlib.import_module(self.name)
except ImportError as err:
raise ImportError(
f"Cannot use selected data format, {self.name} not available"
) from err
return self._module
[docs]
def __dir__(self):
"""
Get the attributes of the module, to support tab-autocomplete.
"""
return dir(self.module)
[docs]
def __getattr__(self, item):
"""Get something from the module"""
return getattr(self.module, item)
[docs]
def lazyImport(modulename):
"""
Lazily load a module
"""
return LazyModule(modulename)
[docs]
tables = lazyImport("tables")
"""The Tables Module"""
[docs]
apTable = lazyImport("astropy.table")
"""The Astropy Tables Module"""
[docs]
apDiffUtils = lazyImport("astropy.utils.diff")
"""The Astropy Utils Diff Module"""
[docs]
fits = lazyImport("astropy.io.fits")
"""The Astropy FITS module"""
[docs]
h5py = lazyImport("h5py")
"""The H5PY Module"""
[docs]
pa = lazyImport("pyarrow")
"""The PyArrow Module"""
[docs]
pd = lazyImport("pandas")
"""The Pandas Module"""
[docs]
pq = lazyImport("pyarrow.parquet")
"""The PyArrow Parquet Module"""
[docs]
ds = lazyImport("pyarrow.dataset")
"""The PyArrow Dataset Module"""
[docs]
jnp = lazyImport("jax.numpy")
"""The JAX Numpy Module"""
[docs]
json = lazyImport("json")
"""The JSON Module"""