Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrument convert attrs to properties #4417

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 55 additions & 18 deletions qcodes/instrument/instrument_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,78 @@ def __init__(self, name: str, metadata: Optional[Mapping[Any, Any]] = None) -> N
self._short_name = name
self._is_valid_identifier(self.full_name)

self.parameters: Dict[str, ParameterBase] = {}
self._parameters: Dict[str, ParameterBase] = {}

self._functions: Dict[str, Function] = {}

self._submodules: Dict[str, Union["InstrumentModule", "ChannelTuple"]] = {}

self._instrument_modules: Dict[str, "InstrumentModule"] = {}

self._channel_lists: Dict[str, "ChannelTuple"] = {}
"""
All the ChannelTuples of this instrument
Usually populated via :py:meth:`add_submodule`.
This is private until the correct name has been decided.
"""

super().__init__(metadata)

# This is needed for snapshot method to work
self._meta_attrs = ["name"]

self.log = get_instrument_logger(self, __name__)

@property
def parameters(self) -> Dict[str, ParameterBase]:
"""
All the parameters supported by this instrument.
Usually populated via :py:meth:`add_parameter`.
"""
self.functions: Dict[str, Function] = {}
return self._parameters

@parameters.setter
def parameters(self, value: Dict[str, ParameterBase]) -> None:
self._parameters = value

@property
def functions(self) -> Dict[str, Function]:
"""
All the functions supported by this
instrument. Usually populated via :py:meth:`add_function`.
"""
self.submodules: Dict[str, Union["InstrumentModule", "ChannelTuple"]] = {}
return self._functions

@functions.setter
def functions(self, value: Dict[str, Function]) -> None:
self._functions = value

@property
def submodules(self) -> Dict[str, Union["InstrumentModule", "ChannelTuple"]]:
"""
All the submodules of this instrument
such as channel lists or logical groupings of parameters.
Usually populated via :py:meth:`add_submodule`.
"""
self.instrument_modules: Dict[str, "InstrumentModule"] = {}
"""
All the :class:`InstrumentModule` of this instrument
Usually populated via :py:meth:`add_submodule`.
"""
return self._submodules

self._channel_lists: Dict[str, "ChannelTuple"] = {}
@submodules.setter
def submodules(
self, value: Dict[str, Union["InstrumentModule", "ChannelTuple"]]
) -> None:
self._submodules = value

@property
def instrument_modules(self) -> Dict[str, "InstrumentModule"]:
"""
All the ChannelTuples of this instrument
All the :class:`InstrumentModule` of this instrument
Usually populated via :py:meth:`add_submodule`.
This is private until the correct name has been decided.
"""
return self._instrument_modules

super().__init__(metadata)

# This is needed for snapshot method to work
self._meta_attrs = ["name"]

self.log = get_instrument_logger(self, __name__)
@instrument_modules.setter
def instrument_modules(self, value: Dict[str, "InstrumentModule"]) -> None:
self._instrument_modules = value

def add_parameter(
self,
Expand Down Expand Up @@ -485,7 +522,7 @@ def _is_abstract(self) -> bool:
# instrument.get('someparam') === instrument['someparam'].get() #
# etc... #
#
delegate_attr_dicts = ["parameters", "functions", "submodules"]
delegate_attr_dicts = ["_parameters", "_functions", "_submodules"]

def __getitem__(self, key: str) -> Union[Callable[..., Any], Parameter]:
"""Delegate instrument['name'] to parameter or function 'name'."""
Expand Down
10 changes: 9 additions & 1 deletion qcodes/metadatable/metadatable_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@

class Metadatable:
def __init__(self, metadata: Optional[Mapping[str, Any]] = None):
self.metadata: Dict[str, Any] = {}
self._metadata: Dict[str, Any] = {}
self.load_metadata(metadata or {})

@property
def metadata(self) -> Dict[str, Any]:
return self._metadata

@metadata.setter
def metadata(self, metadata: Dict[str, Any]) -> None:
self._metadata = metadata

def load_metadata(self, metadata: Mapping[str, Any]) -> None:
"""
Load metadata into this classes metadata dictionary.
Expand Down
29 changes: 16 additions & 13 deletions qcodes/tests/sphinx_extension/test_parse_parameter_attr.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pytest
from sphinx.util.inspect import safe_getattr

from qcodes.instrument import InstrumentBase, VisaInstrument
from qcodes.instrument import InstrumentBase
from qcodes.parameters import Parameter
from qcodes.sphinx_extensions.parse_parameter_attr import (
ParameterProxy,
qcodes_parameter_attr_getter,
Expand All @@ -24,8 +25,13 @@ def __init__(self, *args, **kwargs):
An instance attribute
"""

self.my_param = Parameter(
name="my_param", instrument=self, get_cmd=None, set_cmd=None
)
"""A QCoDeS Parameter"""

class DummyNoInitClass(InstrumentBase):

class DummyNoInitClass(DummyTestClass):

myattr: str = "ClassAttribute"
"""
Expand Down Expand Up @@ -87,16 +93,13 @@ def test_extract_instance_attr():
assert repr(b) == '"InstanceAttribute"'


def test_instrument_base_get_attr():
parameters = qcodes_parameter_attr_getter(InstrumentBase, "parameters")
def test_parameter_get_attr():
parameters = qcodes_parameter_attr_getter(DummyTestClass, "my_param")
assert isinstance(parameters, ParameterProxy)
assert repr(parameters) == "{}"


def test_visa_instr_get_attr():
parameters = qcodes_parameter_attr_getter(VisaInstrument, "parameters")
assert isinstance(parameters, ParameterProxy)
assert repr(parameters) == "{}"
assert (
repr(parameters)
== 'Parameter( name="my_param", instrument=self, get_cmd=None, set_cmd=None )'
)


def test_decorated_init_func():
Expand All @@ -113,6 +116,6 @@ def test_decorated_class():

def test_no_init():
"""Test that attribute can be found from a class without an init function."""
attr = qcodes_parameter_attr_getter(DummyNoInitClass, "parameters")
attr = qcodes_parameter_attr_getter(DummyNoInitClass, "other_attr")
assert isinstance(attr, ParameterProxy)
assert repr(attr) == "{}"
assert repr(attr) == '"InstanceAttribute"'