Skip to content

Commit

Permalink
Instr param (microsoft#649)
Browse files Browse the repository at this point in the history
* added instrument par

* added test

* Addressed comments
  • Loading branch information
AdriaanRol authored and Dominik-Vogel committed Aug 7, 2017
1 parent fcd1e6e commit 687a629
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
55 changes: 51 additions & 4 deletions qcodes/instrument/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
"""
Expand Down Expand Up @@ -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')
15 changes: 14 additions & 1 deletion qcodes/tests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

0 comments on commit 687a629

Please sign in to comment.