diff --git a/.travis.yml b/.travis.yml index 57d5b0503b5..08e44761a5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -101,4 +101,3 @@ after_success: - cd $TRAVIS_BUILD_DIR - python-codacy-coverage -r qcodes/coverage.xml - codecov - diff --git a/qcodes/configuration/config.py b/qcodes/configuration/config.py index da7136f9077..fec12f1792e 100644 --- a/qcodes/configuration/config.py +++ b/qcodes/configuration/config.py @@ -70,14 +70,14 @@ class Config: schema_file_name) """Filename of cwd schema""" - current_schema: Optional[dict] = None + current_schema: Optional['DotDict'] = None """Validators and descriptions of config values""" - current_config: Optional[dict] = None + current_config: Optional['DotDict'] = None """Valid config values""" - defaults: dict + defaults: 'DotDict' """The default configuration""" - defaults_schema: dict + defaults_schema: 'DotDict' """The default schema""" _diff_config: Dict[str, Any] = {} @@ -93,7 +93,7 @@ def __init__(self, path: Optional[str] = None) -> None: self.defaults, self.defaults_schema = self.load_default() self.update_config() - def load_default(self) -> Tuple[dict, dict]: + def load_default(self) -> Tuple['DotDict', 'DotDict']: defaults = self.load_config(self.default_file_name) defaults_schema = self.load_config(self.schema_default_file_name) self.validate(defaults, defaults_schema) diff --git a/qcodes/dataset/guids.py b/qcodes/dataset/guids.py index 65ed415fc18..7830cbf3991 100644 --- a/qcodes/dataset/guids.py +++ b/qcodes/dataset/guids.py @@ -4,7 +4,7 @@ import numpy as np -from qcodes.configuration import Config +import qcodes as qc _guid_pattern = re.compile(r'^[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}$') @@ -25,7 +25,7 @@ def generate_guid(timeint: Union[int, None]=None, timeint: An integer of miliseconds since unix epoch time sampleint: A code for the sample """ - cfg = Config() + cfg = qc.config try: guid_comp = cfg['GUID_components'] @@ -81,7 +81,7 @@ def set_guid_location_code() -> None: """ Interactive function to set the location code. """ - cfg = Config() + cfg = qc.config old_loc = cfg['GUID_components']['location'] print(f'Updating GUID location code. Current location code is: {old_loc}') if old_loc != 0: @@ -106,7 +106,7 @@ def set_guid_work_station_code() -> None: """ Interactive function to set the work station code """ - cfg = Config() + cfg = qc.config old_ws = cfg['GUID_components']['work_station'] print('Updating GUID work station code. ' f'Current work station code is: {old_ws}') diff --git a/qcodes/dataset/sqlite/queries.py b/qcodes/dataset/sqlite/queries.py index 9f25df51a92..bbdbd507f6d 100644 --- a/qcodes/dataset/sqlite/queries.py +++ b/qcodes/dataset/sqlite/queries.py @@ -1593,7 +1593,7 @@ def update_GUIDs(conn: ConnectionPlus) -> None: log.info('Commencing update of all GUIDs in database') - cfg = Config() + cfg = qc.config location = cfg['GUID_components']['location'] work_station = cfg['GUID_components']['work_station'] diff --git a/qcodes/tests/dataset/test_database_creation_and_upgrading.py b/qcodes/tests/dataset/test_database_creation_and_upgrading.py index 0908cf31ad4..9f87c169bdf 100644 --- a/qcodes/tests/dataset/test_database_creation_and_upgrading.py +++ b/qcodes/tests/dataset/test_database_creation_and_upgrading.py @@ -9,7 +9,6 @@ import qcodes as qc import qcodes.dataset.descriptions.versioning.serialization as serial import qcodes.tests.dataset -from qcodes.configuration import Config from qcodes import new_data_set, new_experiment from qcodes.dataset.descriptions.dependencies import InterDependencies_ from qcodes.dataset.descriptions.param_spec import ParamSpecBase @@ -48,19 +47,18 @@ @contextmanager def location_and_station_set_to(location: int, work_station: int): - cfg = Config() - old_cfg = deepcopy(cfg.current_config) + cfg = qc.config.current_config + if cfg is None: + raise RuntimeError("Expected config to be not None.") + old_cfg = deepcopy(cfg) cfg['GUID_components']['location'] = location cfg['GUID_components']['work_station'] = work_station - cfg.save_to_home() try: yield finally: - cfg.current_config = old_cfg - cfg.save_to_home() - + qc.config.current_config = old_cfg LATEST_VERSION = _latest_available_version() VERSIONS = tuple(range(LATEST_VERSION + 1)) @@ -574,13 +572,8 @@ def test_update_existing_guids(caplog): ds2.mark_started() ds2.add_results([{'x': 2}]) - guid_comps_1 = parse_guid(ds1.guid) - assert guid_comps_1['location'] == 0 - assert guid_comps_1['work_station'] == 0 - - guid_comps_2 = parse_guid(ds2.guid) - assert guid_comps_2['location'] == 0 - assert guid_comps_2['work_station'] == 0 + _assert_loc_station(ds1, 0, 0) + _assert_loc_station(ds2, 0, 0) with location_and_station_set_to(0, old_ws): ds3 = new_data_set('ds_three') @@ -588,18 +581,24 @@ def test_update_existing_guids(caplog): ds3.mark_started() ds3.add_results([{'x': 3}]) + _assert_loc_station(ds3, 0, old_ws) + with location_and_station_set_to(old_loc, 0): ds4 = new_data_set('ds_four') ds4.set_interdependencies(idps) ds4.mark_started() ds4.add_results([{'x': 4}]) + _assert_loc_station(ds4, old_loc, 0) + with location_and_station_set_to(old_loc, old_ws): ds5 = new_data_set('ds_five') ds5.set_interdependencies(idps) ds5.mark_started() ds5.add_results([{'x': 5}]) + _assert_loc_station(ds5, old_loc, old_ws) + with location_and_station_set_to(new_loc, new_ws): caplog.clear() @@ -614,27 +613,20 @@ def test_update_existing_guids(caplog): update_GUIDs(ds1.conn) for record, lvl in zip(caplog.records, expected_levels): + print(record) assert record.levelname == lvl - guid_comps_1 = parse_guid(ds1.guid) - assert guid_comps_1['location'] == new_loc - assert guid_comps_1['work_station'] == new_ws - - guid_comps_2 = parse_guid(ds2.guid) - assert guid_comps_2['location'] == new_loc - assert guid_comps_2['work_station'] == new_ws - - guid_comps_3 = parse_guid(ds3.guid) - assert guid_comps_3['location'] == 0 - assert guid_comps_3['work_station'] == old_ws + _assert_loc_station(ds1, new_loc, new_ws) + _assert_loc_station(ds2, new_loc, new_ws) + _assert_loc_station(ds3, 0, old_ws) + _assert_loc_station(ds4, old_loc, 0) + _assert_loc_station(ds5, old_loc, old_ws) - guid_comps_4 = parse_guid(ds4.guid) - assert guid_comps_4['location'] == old_loc - assert guid_comps_4['work_station'] == 0 - guid_comps_5 = parse_guid(ds5.guid) - assert guid_comps_5['location'] == old_loc - assert guid_comps_5['work_station'] == old_ws +def _assert_loc_station(ds, expected_loc, expected_station): + guid_dict = parse_guid(ds.guid) + assert guid_dict["location"] == expected_loc + assert guid_dict["work_station"] == expected_station @pytest.mark.parametrize('db_file', diff --git a/qcodes/tests/dataset/test_database_extract_runs.py b/qcodes/tests/dataset/test_database_extract_runs.py index c7406334a78..e891b1b853e 100644 --- a/qcodes/tests/dataset/test_database_extract_runs.py +++ b/qcodes/tests/dataset/test_database_extract_runs.py @@ -9,7 +9,7 @@ import pytest import numpy as np -from qcodes.configuration import Config +import qcodes as qc import qcodes.tests.dataset from qcodes.dataset.experiment_container import Experiment,\ load_experiment_by_name @@ -511,7 +511,7 @@ def test_combine_runs(two_empty_temp_db_connections, lines = table.split('\n') headers = re.split(r'\s+', lines[0].strip()) - cfg = Config() + cfg = qc.config guid_comp = cfg['GUID_components'] # borrowed fallback logic from generate_guid diff --git a/qcodes/tests/dataset/test_guids.py b/qcodes/tests/dataset/test_guids.py index 14c791c6d55..e4d26e2b2af 100644 --- a/qcodes/tests/dataset/test_guids.py +++ b/qcodes/tests/dataset/test_guids.py @@ -8,12 +8,13 @@ import hypothesis.strategies as hst import numpy as np +import qcodes as qc from qcodes.dataset.guids import (generate_guid, parse_guid, set_guid_location_code, set_guid_work_station_code, validate_guid_format, filter_guids_by_parts) -from qcodes.configuration import Config, DotDict +from qcodes.configuration import DotDict @contextmanager def protected_config(): @@ -21,15 +22,13 @@ def protected_config(): Context manager to be used in all tests that modify the config to ensure that the config is left untouched even if the tests fail """ - ocfg: DotDict = Config().current_config + ocfg: DotDict = qc.config.current_config original_config = deepcopy(ocfg) try: yield finally: - cfg = Config() - cfg.current_config = original_config - cfg.save_to_home() + qc.config.current_config = original_config @settings(max_examples=50, deadline=1000) @@ -38,11 +37,10 @@ def protected_config(): def test_generate_guid(loc, stat, smpl): # update config to generate a particular guid. Read it back to verify with protected_config(): - cfg = Config() + cfg = qc.config cfg['GUID_components']['location'] = loc cfg['GUID_components']['work_station'] = stat cfg['GUID_components']['sample'] = smpl - cfg.save_to_home() guid = generate_guid() gen_time = int(np.round(time.time()*1000)) @@ -63,14 +61,14 @@ def test_generate_guid(loc, stat, smpl): def test_set_guid_location_code(loc, monkeypatch): monkeypatch.setattr('builtins.input', lambda x: str(loc)) - orig_cfg = Config().current_config + orig_cfg = qc.config original_loc = orig_cfg['GUID_components']['location'] with protected_config(): set_guid_location_code() - cfg = Config().current_config + cfg = qc.config if 257 > loc > 0: assert cfg['GUID_components']['location'] == loc @@ -83,14 +81,14 @@ def test_set_guid_location_code(loc, monkeypatch): def test_set_guid_workstatio_code(ws, monkeypatch): monkeypatch.setattr('builtins.input', lambda x: str(ws)) - orig_cfg = Config().current_config + orig_cfg = qc.config original_ws = orig_cfg['GUID_components']['work_station'] with protected_config(): set_guid_work_station_code() - cfg = Config().current_config + cfg = qc.config if 16777216 > ws > 0: assert cfg['GUID_components']['work_station'] == ws @@ -99,9 +97,12 @@ def test_set_guid_workstatio_code(ws, monkeypatch): @settings(max_examples=50, deadline=1000) -@given(locs=hst.lists(hst.integers(0, 255), min_size=2, max_size=2, unique=True), - stats=hst.lists(hst.integers(0, 65535), min_size=2, max_size=2, unique=True), - smpls=hst.lists(hst.integers(0, 4294967295), min_size=2, max_size=2, unique=True), +@given(locs=hst.lists(hst.integers(0, 255), min_size=2, max_size=2, + unique=True), + stats=hst.lists(hst.integers(0, 65535), min_size=2, max_size=2, + unique=True), + smpls=hst.lists(hst.integers(0, 4294967295), min_size=2, max_size=2, + unique=True), ) def test_filter_guid(locs, stats, smpls): @@ -109,7 +110,6 @@ def make_test_guid(cfg, loc: int, smpl: int, stat: int): cfg['GUID_components']['location'] = loc cfg['GUID_components']['work_station'] = stat cfg['GUID_components']['sample'] = smpl - cfg.save_to_home() guid = generate_guid() gen_time = int(np.round(time.time() * 1000)) @@ -126,7 +126,7 @@ def make_test_guid(cfg, loc: int, smpl: int, stat: int): with protected_config(): guids = [] - cfg = Config() + cfg = qc.config corrected_smpls = [smpl if smpl != 0 else int('a' * 8, base=16) for smpl in smpls] diff --git a/qcodes/tests/test_config.py b/qcodes/tests/test_config.py index 7ce1071c26f..626e796a76f 100644 --- a/qcodes/tests/test_config.py +++ b/qcodes/tests/test_config.py @@ -14,7 +14,7 @@ import tempfile import qcodes -from qcodes.configuration import Config +from qcodes.configuration import Config, DotDict VALID_JSON = "{}" ENV_KEY = "/dev/random" @@ -173,7 +173,8 @@ def default_config(user_config: Optional[str] = None): Config.cwd_file_name = '' Config.schema_cwd_file_name = '' - default_config_obj = copy.deepcopy(qcodes.config) + default_config_obj: Optional[DotDict] = copy.\ + deepcopy(qcodes.config.current_config) qcodes.config = Config() try: @@ -186,7 +187,7 @@ def default_config(user_config: Optional[str] = None): Config.cwd_file_name = cwd_file_name Config.schema_cwd_file_name = schema_cwd_file_name - qcodes.config = default_config_obj + qcodes.config.current_config = default_config_obj def side_effect(map, name): @@ -319,7 +320,7 @@ def test_update_and_validate_user_config(self, config, schema): def test_update_from_path(path_to_config_file_on_disk): with default_config(): - cfg = Config() + cfg = qcodes.config # check that the default is still the default assert cfg["core"]["db_debug"] is False @@ -338,7 +339,7 @@ def test_update_from_path(path_to_config_file_on_disk): def test_repr(): - cfg = Config() + cfg = qcodes.config rep = cfg.__repr__() expected_rep = (f"Current values: \n {cfg.current_config} \n" @@ -360,7 +361,7 @@ def test_add_and_describe(): description ='A test' default = 'testdefault' - cfg = Config() + cfg = qcodes.config cfg.add(key=key, value=value, value_type=value_type, description=description, default=default)