Skip to content

Commit

Permalink
Merge pull request #4655 from MichaelGoulding/param_metadata_snaphot
Browse files Browse the repository at this point in the history
InstrumentBase and ParameterBase now call snapshot() on _meta_attrs attributes that inherit from Metadatable.
  • Loading branch information
jenshnielsen committed Sep 27, 2022
2 parents 00d4645 + 1e82900 commit 28546e3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/changes/newsfragments/4655.improved
@@ -0,0 +1 @@
InstrumentBase and ParameterBase now call snapshot() on _meta_attrs attributes that inherit from Metadatable.
9 changes: 7 additions & 2 deletions qcodes/instrument/instrument_base.py
Expand Up @@ -293,8 +293,13 @@ def snapshot_base(
snap["parameters"][name] = param.snapshot(update=False)

for attr in set(self._meta_attrs):
if hasattr(self, attr):
snap[attr] = getattr(self, attr)
val = getattr(self, attr, None)
if val is not None:
if isinstance(val, Metadatable):
snap[attr] = val.snapshot(update=update)
else:
snap[attr] = val

return snap

def print_readable_snapshot(
Expand Down
2 changes: 2 additions & 0 deletions qcodes/parameters/parameter_base.py
Expand Up @@ -451,6 +451,8 @@ def snapshot_base(
attr_strip = attr.lstrip("_") # strip leading underscores
if isinstance(val, Validator):
state[attr_strip] = repr(val)
elif isinstance(val, Metadatable):
state[attr_strip] = val.snapshot(update=update)
else:
state[attr_strip] = val

Expand Down
45 changes: 45 additions & 0 deletions qcodes/tests/test_instrument.py
Expand Up @@ -10,6 +10,7 @@

from qcodes.instrument import Instrument, InstrumentBase, find_or_create_instrument
from qcodes.parameters import Function, Parameter
from qcodes.utils.metadata import Metadatable

from .instrument_mocks import (
DummyChannelInstrument,
Expand Down Expand Up @@ -424,3 +425,47 @@ def test_snapshot_and_meta_attrs():

assert '__class__' in snapshot
assert 'InstrumentBase' in snapshot['__class__']


class TestSnapshotType(Metadatable):
def __init__(self, sample_value: int) -> None:
super().__init__()
self.sample_value = sample_value

def snapshot_base(self, update=True, params_to_skip_update=None):
return {"sample_key": self.sample_value}


class TestInstrument(InstrumentBase):
def __init__(self, name, label) -> None:
super().__init__(name, label=label)
self._meta_attrs.extend(["test_attribute"])
self._test_attribute = TestSnapshotType(12)

@property
def test_attribute(self) -> TestSnapshotType:
return self._test_attribute


def test_snapshot_and_meta_attrs2():
"""Test snapshot of child of InstrumentBase which contains _meta_attrs attribute that is itself Metadatable"""
instr = TestInstrument("instr", label="Label")

assert instr.name == "instr"

assert hasattr(instr, "_meta_attrs")
assert instr._meta_attrs == ["name", "label", "test_attribute"]

snapshot = instr.snapshot()

assert "name" in snapshot
assert "instr" == snapshot["name"]

assert "label" in snapshot
assert "Label" == snapshot["label"]

assert "__class__" in snapshot
assert "TestInstrument" in snapshot["__class__"]

assert "test_attribute" in snapshot
assert {"sample_key": 12} == snapshot["test_attribute"]

0 comments on commit 28546e3

Please sign in to comment.