From aa382dec97931b2d2f99a27108dcf673e46c346f Mon Sep 17 00:00:00 2001 From: Dominik Vogel Date: Wed, 11 Sep 2019 14:34:38 +0200 Subject: [PATCH 1/4] Remove `make_unique` --- qcodes/station.py | 8 ++++++-- qcodes/tests/test_helpers.py | 13 +------------ qcodes/utils/helpers.py | 16 ---------------- 3 files changed, 7 insertions(+), 30 deletions(-) diff --git a/qcodes/station.py b/qcodes/station.py index 2ba513dbbfd..4d7f8eec8cf 100644 --- a/qcodes/station.py +++ b/qcodes/station.py @@ -13,7 +13,7 @@ import qcodes from qcodes.utils.metadata import Metadatable from qcodes.utils.helpers import ( - make_unique, DelegateAttributes, YAML, checked_getattr) + DelegateAttributes, YAML, checked_getattr) from qcodes.instrument.base import Instrument, InstrumentBase from qcodes.instrument.parameter import ( @@ -184,7 +184,11 @@ def add_component(self, component: Metadatable, name: str = None, if name is None: name = getattr(component, 'name', 'component{}'.format(len(self.components))) - namestr = make_unique(str(name), self.components) + namestr = str(name) + if namestr in self.components.keys(): + raise RuntimeError( + f'Cannot add component "{namestr}", because a ' + 'component of that name is already registered to the station') self.components[namestr] = component return namestr diff --git a/qcodes/tests/test_helpers.py b/qcodes/tests/test_helpers.py index a47aa819fa2..c6a4edce6bf 100644 --- a/qcodes/tests/test_helpers.py +++ b/qcodes/tests/test_helpers.py @@ -10,7 +10,7 @@ import numpy as np from qcodes.utils.helpers import (is_sequence, permissive_range, wait_secs, - make_unique, DelegateAttributes, + DelegateAttributes, strip_attrs, full_class, named_repr, make_sweep, is_sequence_of, compare_dictionaries, NumpyJSONEncoder, @@ -257,17 +257,6 @@ def test_warning(self): self.assertEqual(logs.value.count('negative delay'), 1, logs.value) -class TestMakeUnique(TestCase): - def test_no_changes(self): - for s, existing in (('a', []), ('a', {}), ('a', ('A', ' a', 'a '))): - self.assertEqual(make_unique(s, existing), s) - - def test_changes(self): - self.assertEqual(make_unique('a', ('a',)), 'a_2') - self.assertEqual(make_unique('a_2', ('a_2',)), 'a_2_2') - self.assertEqual(make_unique('a', ('a', 'a_2', 'a_3')), 'a_4') - - class TestDelegateAttributes(TestCase): def test_delegate_dict(self): class ToDict(DelegateAttributes): diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index cd1748f6ce4..6c7a5e1c344 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -352,22 +352,6 @@ def __exit__(self, type, value, tb): self.logger.addHandler(handler) -def make_unique(s, existing): - """ - make string s unique, able to be added to a sequence `existing` of - existing names without duplication, by appending _ to it if needed - """ - n = 1 - s_out = s - existing = set(existing) - - while s_out in existing: - n += 1 - s_out = '{}_{}'.format(s, n) - - return s_out - - class DelegateAttributes: """ Mixin class to create attributes of this object by From 8cbe6969eaa9698b743a55197baeb517f4c819be Mon Sep 17 00:00:00 2001 From: Dominik Vogel Date: Wed, 11 Sep 2019 15:04:01 +0200 Subject: [PATCH 2/4] Reinstate `make_unique` but deprecate it --- qcodes/utils/helpers.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index 6c7a5e1c344..b6f0a1cef8f 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -352,6 +352,24 @@ def __exit__(self, type, value, tb): self.logger.addHandler(handler) +@deprecate( + reason=("Instrument names are cross process identifiers that should be " + "chosen transparently.") +def make_unique(s, existing): + """ + make string s unique, able to be added to a sequence `existing` of + existing names without duplication, by appending _ to it if needed + """ + n = 1 + s_out = s + existing = set(existing) + + while s_out in existing: + n += 1 + s_out = '{}_{}'.format(s, n) + + return s_out + class DelegateAttributes: """ Mixin class to create attributes of this object by From 721cb067f532e9361033d0a3d3414719e82cec11 Mon Sep 17 00:00:00 2001 From: Dominik Vogel Date: Wed, 11 Sep 2019 15:52:51 +0200 Subject: [PATCH 3/4] Add forgotten paren --- qcodes/utils/helpers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index b6f0a1cef8f..8051d2cd8ca 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -354,7 +354,7 @@ def __exit__(self, type, value, tb): @deprecate( reason=("Instrument names are cross process identifiers that should be " - "chosen transparently.") + "chosen transparently.")) def make_unique(s, existing): """ make string s unique, able to be added to a sequence `existing` of From e8bcf21e07bb9ed28564800c24f716368c5c6afc Mon Sep 17 00:00:00 2001 From: Dominik Vogel Date: Wed, 11 Sep 2019 16:07:42 +0200 Subject: [PATCH 4/4] Reformulate deprecation --- qcodes/utils/helpers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qcodes/utils/helpers.py b/qcodes/utils/helpers.py index 8051d2cd8ca..af9ff01d497 100644 --- a/qcodes/utils/helpers.py +++ b/qcodes/utils/helpers.py @@ -353,8 +353,7 @@ def __exit__(self, type, value, tb): @deprecate( - reason=("Instrument names are cross process identifiers that should be " - "chosen transparently.")) + reason="This method is no longer being used in QCoDeS.") def make_unique(s, existing): """ make string s unique, able to be added to a sequence `existing` of @@ -370,6 +369,7 @@ def make_unique(s, existing): return s_out + class DelegateAttributes: """ Mixin class to create attributes of this object by