data.fetch_data.get_cache_home

data.fetch_data.get_cache_home(cache_home=None, create_dir=True)

Return the location where persistent models are to be cached.

By default the cache directory is set to a folder named .spotforecast2_cache in the user home folder. Alternatively, it can be set by the SPOTFORECAST2_CACHE environment variable or programmatically by giving an explicit folder path. The ~ symbol is expanded to the user home folder. When create_dir is True (the default) the directory is created automatically if it does not already exist.

This directory is used to store pickled trained models for quick reuse across forecasting runs, following scikit-learn model persistence conventions.

Parameters

Name Type Description Default
cache_home Optional[Union[str, Path]] Path to the spotforecast cache directory. If None, the value of the SPOTFORECAST2_CACHE environment variable is used when set, otherwise the default path ~/.spotforecast2_cache is used. None
create_dir bool Whether to create the cache directory if it does not exist. When True (the default), the directory and any missing parent directories are created automatically. When False, the resolved path is returned without touching the filesystem. True

Returns

Name Type Description
Path Absolute path to the spotforecast cache directory.

Raises

Name Type Description
OSError If create_dir is True and the directory cannot be created due to a permissions error or other OS-level failure.

Examples

from spotforecast2_safe.data.fetch_data import get_cache_home
cache_dir = get_cache_home()
print(cache_dir.name)
print(cache_dir.parent.name)
.spotforecast2_cache
runner
# Using custom path
from spotforecast2_safe.data.fetch_data import get_cache_home
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
    custom_cache = get_cache_home(Path(tmp) / 'my_cache')
    print(custom_cache.exists())
True
# Resolve path without creating the directory
from spotforecast2_safe.data.fetch_data import get_cache_home
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
    resolved = get_cache_home(Path(tmp) / 'not_yet', create_dir=False)
    print(resolved.exists())
False
# Using environment variable
from spotforecast2_safe.data.fetch_data import get_cache_home
import os
os.environ['SPOTFORECAST2_CACHE'] = '/tmp/spotforecast2_cache_env'
cache_dir = get_cache_home()
cache_dir.as_posix()
del os.environ['SPOTFORECAST2_CACHE']