Skip to content

Commit

Permalink
Speed up CLI execution by lazy importing xarray and cfgrib
Browse files Browse the repository at this point in the history
  • Loading branch information
pmav99 committed Nov 17, 2023
1 parent c13e940 commit 3f909d5
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 479 deletions.
21 changes: 12 additions & 9 deletions inspectds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import enum
import importlib.metadata
import importlib.util
import warnings
from pathlib import Path
from typing import Any
from typing import TYPE_CHECKING

import typer
import xarray as xr

try:
import cfgrib as cfgrib
from . import lazy_imports

if TYPE_CHECKING:
import xarray

if importlib.util.find_spec("cfgrib"):
IS_GRIB_AVAILABLE = True
except ImportError:
IS_GRIB_AVAILABLE = False
except RuntimeError:
else:
IS_GRIB_AVAILABLE = False

if IS_GRIB_AVAILABLE:
Expand Down Expand Up @@ -43,7 +45,7 @@ class DATASET_TYPE(str, enum.Enum): # type: ignore[no-redef]
ZARR = "zarr"


def echo_variable_attributes(ds: xr.Dataset) -> None:
def echo_variable_attributes(ds: xarray.Dataset) -> None:
lines = []
lines.append("Variable Attributes:")
for name, da in ds.variables.items():
Expand All @@ -54,7 +56,7 @@ def echo_variable_attributes(ds: xr.Dataset) -> None:
typer.echo("\n".join(lines))


def echo_global_attributes(ds: xr.Dataset) -> None:
def echo_global_attributes(ds: xarray.Dataset) -> None:
lines = []
lines.append("Global attributes:")
for key, value in ds.attrs.items():
Expand All @@ -63,7 +65,7 @@ def echo_global_attributes(ds: xr.Dataset) -> None:


def echo_dataset(
ds: xr.Dataset,
ds: xarray.Dataset,
dimensions: bool,
coordinates: bool,
variables: bool,
Expand Down Expand Up @@ -158,6 +160,7 @@ def inspect_dataset(
full: bool = typer.Option(default=False, help="Display full output. Overrides any other option"),
version: bool = typer.Option(False, "--version", help="Display the version", callback=version_callback, is_eager=True),
) -> int: # fmt: skip
xr = lazy_imports.import_xarray()
if dataset_type == DATASET_TYPE.AUTO:
dataset_type = infer_dataset_type(path)

Expand Down
11 changes: 11 additions & 0 deletions inspectds/lazy_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import annotations

import functools
import importlib
import types


@functools.cache
def import_xarray() -> types.ModuleType:
xarray = importlib.import_module("xarray")
return xarray
Loading

0 comments on commit 3f909d5

Please sign in to comment.