Skip to content

Commit

Permalink
Add decorator function to speed up import (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
paddyroddy authored Nov 28, 2023
1 parent 1f9a510 commit 9745407
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ optional-dependencies = {dev = [
"pytest",
"ruff",
"tox",
"tuna",
"twine",
], docs = [
"pdoc3",
Expand Down
65 changes: 56 additions & 9 deletions src/sleplet/_data/setup_pooch.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,78 @@
import logging
import os
import typing

import platformdirs
import pooch

_logger = logging.getLogger(__name__)


_POOCH = None
_ZENODO_DATA_DOI = "10.5281/zenodo.7767698"
_POOCH = pooch.create(
path=pooch.os_cache("sleplet"),
base_url=f"doi:{_ZENODO_DATA_DOI}/",
registry=None,
)
_POOCH.load_registry_from_doi()


def _lazy_load_registry(
func: typing.Callable[..., os.PathLike[str] | None]
) -> typing.Callable[..., os.PathLike[str] | None]:
"""
Lazily loads POOCH registry before executing a function.
Args:
func: Function to be decorated
Returns:
Decorated function
"""

def wrapper(
*args: typing.Any, # noqa: ANN401
**kwargs: typing.Any, # noqa: ANN401
) -> os.PathLike[str] | None:
"""
Load the POOCH registry if not already loaded and execute the function.
Args:
*args: Positional arguments for the function
**kwargs: Keyword arguments for the function.
Returns:
Result of the decorated function
"""
global _POOCH # noqa: PLW0603
if _POOCH is None:
_POOCH = pooch.create(
path=pooch.os_cache("sleplet"),
base_url=f"doi:{_ZENODO_DATA_DOI}/",
registry=None,
)
_POOCH.load_registry_from_doi()
return func(*args, **kwargs)

return wrapper


@_lazy_load_registry
def find_on_pooch_then_local(filename: str) -> os.PathLike[str] | None:
"""Find a file on POOCH first and if not look in data folder."""
if filename in _POOCH.registry:
"""
Find a file on POOCH first and if not look in data folder.
Args:
filename: Filename to find
Returns:
The sought after file or nothing if not found
"""
if filename in _POOCH.registry: # type: ignore[union-attr]
msg = f"Found {filename} at https://doi.org/{_ZENODO_DATA_DOI}"
_logger.info(msg)
return _POOCH.fetch(filename, progressbar=True)
return _POOCH.fetch(filename, progressbar=True) # type: ignore[union-attr]

if (platformdirs.user_data_path() / filename).exists():
msg = f"Found {filename} at {platformdirs.user_data_path() / filename}"
_logger.info(msg)
return platformdirs.user_data_path() / filename

msg = f"No {filename} found, calculating..."
_logger.info(msg)
return None

0 comments on commit 9745407

Please sign in to comment.