Skip to content

Commit 27696bb

Browse files
committed
req changes
1 parent bde5942 commit 27696bb

3 files changed

Lines changed: 67 additions & 69 deletions

File tree

openml/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
if TYPE_CHECKING:
5555
from ._config import OpenMLConfigManager
5656

57-
config: OpenMLConfigManager = _config_module._config
57+
config: OpenMLConfigManager = _config_module.__config
5858

5959

6060
def populate_cache(

openml/_config.py

Lines changed: 65 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ def _resolve_default_cache_dir() -> Path:
2828
return Path(user_defined_cache_dir)
2929

3030
if platform.system().lower() != "linux":
31-
return Path("~", ".openml")
31+
return Path("~", ".openml").expanduser()
3232

3333
xdg_cache_home = os.environ.get("XDG_CACHE_HOME")
3434
if xdg_cache_home is None:
35-
return Path("~", ".cache", "openml")
35+
return Path("~", ".cache", "openml").expanduser()
3636

3737
cache_dir = Path(xdg_cache_home) / "openml"
3838
if cache_dir.exists():
@@ -57,7 +57,7 @@ def _resolve_default_cache_dir() -> Path:
5757
class OpenMLConfig:
5858
"""Dataclass storing the OpenML configuration."""
5959

60-
apikey: str = ""
60+
apikey: str | None = ""
6161
server: str = "https://www.openml.org/api/v1/xml"
6262
cachedir: Path = field(default_factory=_resolve_default_cache_dir)
6363
avoid_duplicate_runs: bool = False
@@ -83,8 +83,6 @@ def __init__(self) -> None:
8383
self.OPENML_SKIP_PARQUET_ENV_VAR = "OPENML_SKIP_PARQUET"
8484
self._TEST_SERVER_NORMAL_USER_KEY = "normaluser"
8585

86-
self._user_path = Path("~").expanduser().absolute()
87-
8886
self._config: OpenMLConfig = OpenMLConfig()
8987
# for legacy test `test_non_writable_home`
9088
self._defaults: dict[str, Any] = OpenMLConfig().__dict__.copy()
@@ -93,7 +91,7 @@ def __init__(self) -> None:
9391
self.logger = logger
9492
self.openml_logger = openml_logger
9593

96-
self._examples = self.ConfigurationForExamples(self)
94+
self._examples = ConfigurationForExamples(self)
9795

9896
self._setup()
9997

@@ -125,7 +123,6 @@ def __setattr__(self, name: str, value: Any) -> None:
125123
"OPENML_CACHE_DIR_ENV_VAR",
126124
"OPENML_SKIP_PARQUET_ENV_VAR",
127125
"_TEST_SERVER_NORMAL_USER_KEY",
128-
"_user_path",
129126
}:
130127
return object.__setattr__(self, name, value)
131128

@@ -397,70 +394,71 @@ def overwrite_config_context(self, config: dict[str, Any]) -> Iterator[dict[str,
397394
yield merged_config
398395
self._setup(existing_config)
399396

400-
class ConfigurationForExamples:
401-
"""Allows easy switching to and from a test configuration, used for examples."""
402-
403-
_last_used_server = None
404-
_last_used_key = None
405-
_start_last_called = False
406-
407-
def __init__(self, manager: OpenMLConfigManager):
408-
self._manager = manager
409-
self._test_apikey = manager._TEST_SERVER_NORMAL_USER_KEY
410-
self._test_server = "https://test.openml.org/api/v1/xml"
411-
412-
def start_using_configuration_for_example(self) -> None:
413-
"""Sets the configuration to connect to the test server with valid apikey.
414-
415-
To configuration as was before this call is stored, and can be recovered
416-
by using the `stop_use_example_configuration` method.
417-
"""
418-
if (
419-
self._start_last_called
420-
and self._manager._config.server == self._test_server
421-
and self._manager._config.apikey == self._test_apikey
422-
):
423-
# Method is called more than once in a row without modifying the server or apikey.
424-
# We don't want to save the current test configuration as a last used configuration.
425-
return
426-
427-
self._last_used_server = self._manager._config.server
428-
self._last_used_key = self._manager._config.apikey
429-
type(self)._start_last_called = True
430-
431-
# Test server key for examples
432-
self._manager._config = replace(
433-
self._manager._config,
434-
server=self._test_server,
435-
apikey=self._test_apikey,
436-
)
437-
warnings.warn(
438-
f"Switching to the test server {self._test_server} to not upload results to "
439-
"the live server. Using the test server may result in reduced performance of the "
440-
"API!",
441-
stacklevel=2,
442-
)
443397

444-
def stop_using_configuration_for_example(self) -> None:
445-
"""Return to configuration as it was before `start_use_example_configuration`."""
446-
if not type(self)._start_last_called:
447-
# We don't want to allow this because it will (likely) result in the `server` and
448-
# `apikey` variables being set to None.
449-
raise RuntimeError(
450-
"`stop_use_example_configuration` called without a saved config."
451-
"`start_use_example_configuration` must be called first.",
452-
)
453-
454-
self._manager._config = replace(
455-
self._manager._config,
456-
server=cast("str", self._last_used_server),
457-
apikey=cast("str", self._last_used_key),
398+
class ConfigurationForExamples:
399+
"""Allows easy switching to and from a test configuration, used for examples."""
400+
401+
_last_used_server = None
402+
_last_used_key = None
403+
_start_last_called = False
404+
405+
def __init__(self, manager: OpenMLConfigManager):
406+
self._manager = manager
407+
self._test_apikey = manager._TEST_SERVER_NORMAL_USER_KEY
408+
self._test_server = "https://test.openml.org/api/v1/xml"
409+
410+
def start_using_configuration_for_example(self) -> None:
411+
"""Sets the configuration to connect to the test server with valid apikey.
412+
413+
To configuration as was before this call is stored, and can be recovered
414+
by using the `stop_use_example_configuration` method.
415+
"""
416+
if (
417+
self._start_last_called
418+
and self._manager._config.server == self._test_server
419+
and self._manager._config.apikey == self._test_apikey
420+
):
421+
# Method is called more than once in a row without modifying the server or apikey.
422+
# We don't want to save the current test configuration as a last used configuration.
423+
return
424+
425+
self._last_used_server = self._manager._config.server
426+
self._last_used_key = self._manager._config.apikey
427+
type(self)._start_last_called = True
428+
429+
# Test server key for examples
430+
self._manager._config = replace(
431+
self._manager._config,
432+
server=self._test_server,
433+
apikey=self._test_apikey,
434+
)
435+
warnings.warn(
436+
f"Switching to the test server {self._test_server} to not upload results to "
437+
"the live server. Using the test server may result in reduced performance of the "
438+
"API!",
439+
stacklevel=2,
440+
)
441+
442+
def stop_using_configuration_for_example(self) -> None:
443+
"""Return to configuration as it was before `start_use_example_configuration`."""
444+
if not type(self)._start_last_called:
445+
# We don't want to allow this because it will (likely) result in the `server` and
446+
# `apikey` variables being set to None.
447+
raise RuntimeError(
448+
"`stop_use_example_configuration` called without a saved config."
449+
"`start_use_example_configuration` must be called first.",
458450
)
459-
type(self)._start_last_called = False
451+
452+
self._manager._config = replace(
453+
self._manager._config,
454+
server=cast("str", self._last_used_server),
455+
apikey=cast("str", self._last_used_key),
456+
)
457+
type(self)._start_last_called = False
460458

461459

462-
_config = OpenMLConfigManager()
460+
__config = OpenMLConfigManager()
463461

464462

465463
def __getattr__(name: str) -> Any:
466-
return getattr(_config, name)
464+
return getattr(__config, name)

tests/test_openml/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_example_configuration_stop_before_start(self):
135135
error_regex = ".*stop_use_example_configuration.*start_use_example_configuration.*first"
136136
# Tests do not reset the state of this class. Thus, we ensure it is in
137137
# the original state before the test.
138-
openml.config.ConfigurationForExamples._start_last_called = False
138+
openml.config._examples._start_last_called = False
139139
self.assertRaisesRegex(
140140
RuntimeError,
141141
error_regex,

0 commit comments

Comments
 (0)