From 4ea0ddbd2696411e4f717e30d6846d881ae66af5 Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Mon, 8 May 2023 09:47:24 -0400 Subject: [PATCH] feat(deps): load examples lazily --- ibis/examples/__init__.py | 35 +++++++++++++++------------- ibis/examples/tests/test_examples.py | 1 + 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/ibis/examples/__init__.py b/ibis/examples/__init__.py index 1cc394b54ba4..b771dce18c36 100644 --- a/ibis/examples/__init__.py +++ b/ibis/examples/__init__.py @@ -4,8 +4,6 @@ import os from typing import TYPE_CHECKING, Any, Optional -import pooch - import ibis from ibis.common.grounds import Concrete @@ -17,18 +15,9 @@ if TYPE_CHECKING: import ibis.expr.types as ir - -_EXAMPLES = pooch.create( - path=pooch.os_cache("ibis-framework"), - # the trailing slash matters here - base_url="https://storage.googleapis.com/ibis-examples/data/", - version=ibis.__version__, -) -with resources.files(__name__).joinpath("registry.txt").open(mode="r") as _f: - _EXAMPLES.load_registry(_f) +_EXAMPLES = None _METADATA = json.loads(resources.files(__name__).joinpath("metadata.json").read_text()) - _READER_FUNCS = {"csv": "read_csv", "csv.gz": "read_csv", "parquet": "read_parquet"} @@ -42,10 +31,6 @@ def fetch(self, **kwargs: Any) -> ir.Table: return reader(_EXAMPLES.fetch(self.key, progressbar=True), **kwargs) -def __dir__() -> list[str]: - return sorted(_METADATA.keys()) - - def _make_fetch_docstring(*, name: str, reader: str): return f"""Fetch the {name} example. Parameters @@ -65,7 +50,25 @@ def _make_fetch_docstring(*, name: str, reader: str): """ +def __dir__() -> list[str]: + return sorted(_METADATA.keys()) + + def __getattr__(name: str) -> Example: + global _EXAMPLES # noqa: PLW0603 + + if _EXAMPLES is None: + import pooch + + _EXAMPLES = pooch.create( + path=pooch.os_cache("ibis-framework"), + # the trailing slash matters here + base_url="https://storage.googleapis.com/ibis-examples/data/", + version=ibis.__version__, + ) + with resources.files(__name__).joinpath("registry.txt").open(mode="r") as _f: + _EXAMPLES.load_registry(_f) + spec = _METADATA.get(name, {}) if (key := spec.get("key")) is None: diff --git a/ibis/examples/tests/test_examples.py b/ibis/examples/tests/test_examples.py index e292385f67e6..59a7533a2049 100644 --- a/ibis/examples/tests/test_examples.py +++ b/ibis/examples/tests/test_examples.py @@ -9,6 +9,7 @@ pytestmark = pytest.mark.examples duckdb = pytest.importorskip("duckdb") +pytest.importorskip("pooch") # large files ignored = frozenset(