Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy load modules #709

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions geoviews/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@

from holoviews import render, save # noqa (API import)

from .annotators import annotate # noqa (API import)
from .element import ( # noqa (API import)
_Element, Feature, Tiles, WMTS, LineContours, FilledContours,
Text, Image, ImageStack, Points, Path, Polygons, Shape, Dataset, RGB,
Contours, Graph, TriMesh, Nodes, EdgePaths, QuadMesh, VectorField,
HexTiles, Labels, Rectangles, Segments, WindBarbs
)
from .util import load_tiff, from_xarray # noqa (API import)
from .operation import project # noqa (API import)
from ._warnings import GeoviewsDeprecationWarning, GeoviewsUserWarning # noqa: F401
from . import data # noqa (API import)
from . import operation # noqa (API import)
from . import plotting # noqa (API import)
from . import feature # noqa (API import)
from . import tile_sources # noqa (API import)
Expand All @@ -42,3 +39,30 @@ def _missing_cmd(*args,**kw): return("install pyct to enable this command (e.g.
def _err(): raise ValueError(_missing_cmd())
fetch_data = copy_examples = examples = _err
del partial, _examples, _copy, _fetch


def __getattr__(attr):
# Lazy loading heavy modules
if attr == 'annotate':
from .annotators import annotate
return annotate
elif attr == 'project':
from .operation import project
return project
elif attr == 'operation':
from . import operation
return operation
raise AttributeError(f"module {__name__} has no attribute {attr!r}")

__all__ = [k for k in locals() if not k.startswith('_')]
__all__ += ['annotate', 'project', 'operation']

def __dir__():
return __all__

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .annotators import annotate
from .operation import project
from . import operation
19 changes: 11 additions & 8 deletions geoviews/element/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
)
from shapely.ops import unary_union

try:
from iris.cube import Cube
except (ImportError, OSError):
# OSError because environment variable $UDUNITS2_XML_PATH
# is sometimes not set. Should be done automatically
# when installing the package.
Cube = None
def _get_iris_cube():
try:
from iris.cube import Cube
except (ImportError, OSError):
# OSError because environment variable $UDUNITS2_XML_PATH
# is sometimes not set. Should be done automatically
# when installing the package.
Cube = None
return Cube


try:
from owslib.wmts import WebMapTileService
Expand Down Expand Up @@ -99,7 +102,7 @@ def __init__(self, data, kdims=None, vdims=None, **kwargs):
crs_data = data.data
else:
crs_data = data
if Cube and isinstance(crs_data, Cube):
if hasattr(crs_data, 'coord_system') and _get_iris_cube() and isinstance(crs_data, _get_iris_cube()):
coord_sys = crs_data.coord_system()
if hasattr(coord_sys, 'as_cartopy_projection'):
crs = coord_sys.as_cartopy_projection()
Expand Down
9 changes: 9 additions & 0 deletions geoviews/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from contextlib import suppress

import geoviews as gv

with suppress(Exception):
gv.extension("bokeh")

with suppress(Exception):
gv.extension("matplotlib")
20 changes: 20 additions & 0 deletions geoviews/tests/test_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import sys
from subprocess import check_output
from textwrap import dedent


def test_no_blocklist_imports():
check = """\
import sys
import geoviews as gv

blocklist = {"panel", "IPython", "datashader", "iris", "dask"}
mods = blocklist & set(sys.modules)

if mods:
print(", ".join(mods), end="")
"""

output = check_output([sys.executable, '-c', dedent(check)])

assert output == b""
Loading