Skip to content

Commit 46ec3ab

Browse files
PGijsbersmfeurer
authored andcommitted
Easy access test server (#680)
* Provide easier switching to an example configuration, so that users can perform the examples. * Fixed as use of local variables did not work. * Refactor into class to avoid introducing new global variables. * Prevent users from accidentally discarding old configurations. Unit tests to check proper behavior. * Rename to ConfigurationForExamples * Renamed class as it the same due to copy pasta. * rename functions
1 parent e4e385b commit 46ec3ab

2 files changed

Lines changed: 103 additions & 1 deletion

File tree

openml/config.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,54 @@
3838
connection_n_retries = _defaults['connection_n_retries']
3939

4040

41+
class ConfigurationForExamples:
42+
""" Allows easy switching to and from a test configuration, used for examples. """
43+
_last_used_server = None
44+
_last_used_key = None
45+
_start_last_called = False
46+
_test_server = "https://test.openml.org/api/v1/xml"
47+
_test_apikey = "c0c42819af31e706efe1f4b88c23c6c1"
48+
49+
@classmethod
50+
def start_using_configuration_for_example(cls):
51+
""" Sets the configuration to connect to the test server with valid apikey.
52+
53+
To configuration as was before this call is stored, and can be recovered
54+
by using the `stop_use_example_configuration` method.
55+
"""
56+
global server
57+
global apikey
58+
59+
if cls._start_last_called and server == cls._test_server and apikey == cls._test_apikey:
60+
# Method is called more than once in a row without modifying the server or apikey.
61+
# We don't want to save the current test configuration as a last used configuration.
62+
return
63+
64+
cls._last_used_server = server
65+
cls._last_used_key = apikey
66+
cls._start_last_called = True
67+
68+
# Test server key for examples
69+
server = cls._test_server
70+
apikey = cls._test_apikey
71+
72+
@classmethod
73+
def stop_using_configuration_for_example(cls):
74+
""" Return to configuration as it was before `start_use_example_configuration`. """
75+
if not cls._start_last_called:
76+
# We don't want to allow this because it will (likely) result in the `server` and
77+
# `apikey` variables being set to None.
78+
raise RuntimeError("`stop_use_example_configuration` called without a saved config."
79+
"`start_use_example_configuration` must be called first.")
80+
81+
global server
82+
global apikey
83+
84+
server = cls._last_used_server
85+
apikey = cls._last_used_key
86+
cls._start_last_called = False
87+
88+
4189
def _setup():
4290
"""Setup openml package. Called on first import.
4391
@@ -140,8 +188,18 @@ def set_cache_directory(cachedir):
140188
cache_directory = cachedir
141189

142190

191+
start_using_configuration_for_example = (
192+
ConfigurationForExamples.start_using_configuration_for_example
193+
)
194+
stop_using_configuration_for_example = (
195+
ConfigurationForExamples.stop_using_configuration_for_example
196+
)
197+
143198
__all__ = [
144-
'get_cache_directory', 'set_cache_directory'
199+
'get_cache_directory',
200+
'set_cache_directory',
201+
'start_using_configuration_for_example',
202+
'stop_using_configuration_for_example',
145203
]
146204

147205
_setup()

tests/test_openml/test_config.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,47 @@ class TestConfig(openml.testing.TestBase):
99
def test_config_loading(self):
1010
self.assertTrue(os.path.exists(openml.config.config_file))
1111
self.assertTrue(os.path.isdir(os.path.expanduser('~/.openml')))
12+
13+
14+
class TestConfigurationForExamples(openml.testing.TestBase):
15+
16+
def test_switch_to_example_configuration(self):
17+
""" Verifies the test configuration is loaded properly. """
18+
# Below is the default test key which would be used anyway, but just for clarity:
19+
openml.config.apikey = "610344db6388d9ba34f6db45a3cf71de"
20+
openml.config.server = self.production_server
21+
22+
openml.config.start_using_configuration_for_example()
23+
24+
self.assertEqual(openml.config.apikey, "c0c42819af31e706efe1f4b88c23c6c1")
25+
self.assertEqual(openml.config.server, self.test_server)
26+
27+
def test_switch_from_example_configuration(self):
28+
""" Verifies the previous configuration is loaded after stopping. """
29+
# Below is the default test key which would be used anyway, but just for clarity:
30+
openml.config.apikey = "610344db6388d9ba34f6db45a3cf71de"
31+
openml.config.server = self.production_server
32+
33+
openml.config.start_using_configuration_for_example()
34+
openml.config.stop_using_configuration_for_example()
35+
36+
self.assertEqual(openml.config.apikey, "610344db6388d9ba34f6db45a3cf71de")
37+
self.assertEqual(openml.config.server, self.production_server)
38+
39+
def test_example_configuration_stop_before_start(self):
40+
""" Verifies an error is raised is `stop_...` is called before `start_...`. """
41+
error_regex = ".*stop_use_example_configuration.*start_use_example_configuration.*first"
42+
self.assertRaisesRegex(RuntimeError, error_regex,
43+
openml.config.stop_using_configuration_for_example)
44+
45+
def test_example_configuration_start_twice(self):
46+
""" Checks that the original config can be returned to if `start..` is called twice. """
47+
openml.config.apikey = "610344db6388d9ba34f6db45a3cf71de"
48+
openml.config.server = self.production_server
49+
50+
openml.config.start_using_configuration_for_example()
51+
openml.config.start_using_configuration_for_example()
52+
openml.config.stop_using_configuration_for_example()
53+
54+
self.assertEqual(openml.config.apikey, "610344db6388d9ba34f6db45a3cf71de")
55+
self.assertEqual(openml.config.server, self.production_server)

0 commit comments

Comments
 (0)