From 687a6299654bdd28580ad738c2ae35264e9faeb3 Mon Sep 17 00:00:00 2001 From: Adriaan Date: Tue, 27 Jun 2017 11:28:06 +0200 Subject: [PATCH] Instr param (#649) * added instrument par * added test * Addressed comments --- qcodes/instrument/parameter.py | 55 +++++++++++++++++++++++++++++++--- qcodes/tests/test_parameter.py | 15 +++++++++- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/qcodes/instrument/parameter.py b/qcodes/instrument/parameter.py index 4822903c1b42..612a72ba3532 100644 --- a/qcodes/instrument/parameter.py +++ b/qcodes/instrument/parameter.py @@ -67,7 +67,7 @@ full_class, named_repr, warn_units) from qcodes.utils.metadata import Metadatable from qcodes.utils.command import Command, NoCommandError -from qcodes.utils.validators import Validator, Numbers, Ints, Enum +from qcodes.utils.validators import Validator, Numbers, Ints, Enum, Strings from qcodes.instrument.sweep_values import SweepFixedValues from qcodes.data.data_array import DataArray @@ -1073,9 +1073,9 @@ class ManualParameter(Parameter): instrument (Optional[Instrument]): the instrument this applies to, if any. - initial_value (Optional[str]): starting value, the - only invalid value allowed, and None is only allowed as an initial - value, it cannot be set later + initial_value (Optional[str]): starting value, may be None even if + None does not pass the validator. None is only allowed as an + initial value and cannot be set after initiation. **kwargs: Passed to Parameter parent class """ @@ -1288,3 +1288,50 @@ def snapshot_base(self, update=False): meta_data[param.full_name] = param.snapshot() return meta_data + + +class InstrumentRefParameter(ManualParameter): + """ + An InstrumentRefParameter + + Args: + name (string): the name of the parameter that one wants to add. + + instrument (Optional[Instrument]): the "parent" instrument this + parameter is attached to, if any. + + initial_value (Optional[str]): starting value, may be None even if + None does not pass the validator. None is only allowed as an + initial value and cannot be set after initiation. + + **kwargs: Passed to InstrumentRefParameter parent class + + This parameter is useful when one needs a reference to another instrument + from within an instrument, e.g., when creating a meta instrument that + sets parameters on instruments it contains. + """ + + def get_instr(self): + """ + Returns the instance of the instrument with the name equal to the + value of this parameter. + """ + ref_instrument_name = self.get() + # note that _instrument refers to the instrument this parameter belongs + # to, while the ref_instrument_name is the instrument that is the value + # of this parameter. + return self._instrument.find_instrument(ref_instrument_name) + + def set_validator(self, vals): + """ + Set a validator `vals` for this parameter. + + Args: + vals (Validator): validator to set + """ + if vals is None: + self._vals = Strings() + elif isinstance(vals, Validator): + self._vals = vals + else: + raise TypeError('vals must be a Validator') diff --git a/qcodes/tests/test_parameter.py b/qcodes/tests/test_parameter.py index 4fb2ac839c7c..f709eaab9a0e 100644 --- a/qcodes/tests/test_parameter.py +++ b/qcodes/tests/test_parameter.py @@ -7,9 +7,10 @@ from qcodes import Function from qcodes.instrument.parameter import ( Parameter, ArrayParameter, MultiParameter, - ManualParameter, StandardParameter) + ManualParameter, StandardParameter, InstrumentRefParameter) from qcodes.utils.helpers import LogCapture from qcodes.utils.validators import Numbers +from qcodes.tests.instrument_mocks import DummyInstrument class GettableParam(Parameter): @@ -636,3 +637,15 @@ def test_val_mapping_with_parsers(self): self._p = 'PVAL: 1' self.assertEqual(p(), 'on') + + +class TestInstrumentRefParameter(TestCase): + def test_get_instr(self): + a = DummyInstrument('dummy_holder') + d = DummyInstrument('dummy') + a.add_parameter('test', parameter_class=InstrumentRefParameter) + + a.test.set(d.name) + + self.assertEqual(a.test.get(), d.name) + self.assertEqual(a.test.get_instr(), d)