Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions openml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,18 @@ def safe_func(*args: P.args, **kwargs: P.kwargs) -> R:
return func


def get_cache_size() -> int:
"""Calculate the size of OpenML cache directory

Returns
-------
cache_size: int
Total size of cache in bytes
"""
path = Path(config.get_cache_directory())
return sum(f.stat().st_size for f in path.rglob("*") if f.is_file())


def _create_lockfiles_dir() -> Path:
path = Path(config.get_cache_directory()) / "locks"
# TODO(eddiebergman): Not sure why this is allowed to error and ignore???
Expand Down
25 changes: 25 additions & 0 deletions tests/test_utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,28 @@ def test_correct_test_server_download_state():
task = openml.tasks.get_task(119)
dataset = task.get_dataset()
assert len(dataset.features) == dataset.get_data()[0].shape[1]

@unittest.mock.patch("openml.config.get_cache_directory")
def test_get_cache_size(config_mock,tmp_path):
"""
Test that the OpenML cache size utility correctly reports the cache directory
size before and after fetching a dataset.

This test uses a temporary directory (tmp_path) as the cache location by
patching the configuration via config_mock. It verifies two conditions:
empty cache and after dataset fetch.

Parameters
----------
config_mock : unittest.mock.Mock
A mock that overrides the configured cache directory to point to tmp_path.
tmp_path : pathlib.Path
A pytest-provided temporary directory used as an isolated cache location.
"""

config_mock.return_value = tmp_path
cache_size = openml.utils.get_cache_size()
assert cache_size == 0
dataset = openml.datasets.get_dataset(dataset_id=3)
cache_size = openml.utils.get_cache_size()
assert cache_size == 2009