Skip to content

Instantly share code, notes, and snippets.

@dkriegner
Last active December 21, 2022 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkriegner/7af08dc78fdd1461125e079670675bcf to your computer and use it in GitHub Desktop.
Save dkriegner/7af08dc78fdd1461125e079670675bcf to your computer and use it in GitHub Desktop.
pymeasure example issue Channel of Instrument does not use overwritten Instrument methods
from pymeasure.instruments import Channel, Instrument
def values(self, command, cast=int, separator=',', preprocess_reply=None):
"""Write a command to the instrument and return a list of formatted
values from the result.
:param command: SCPI command to be sent to the instrument
:param separator: A separator character to split the string into a list
:param cast: A type to cast the result
:param preprocess_reply: optional callable used to preprocess values
received from the instrument. The callable returns the processed
string.
:returns: A list of the desired type, or strings where the casting fails
"""
results = self.ask(command)
if callable(preprocess_reply):
results = preprocess_reply(results)
for i, result in enumerate(results):
try:
if cast == bool:
# Need to cast to float first since results are usually
# strings and bool of a non-empty string is always True
results[i] = bool(float(result))
else:
results[i] = cast(result)
except Exception:
pass # Keep as bytes
return results
class PresetChannel(Channel):
values = values
prop = Instrument.measurement(
"A",
""" test """,
)
class DKTest(Instrument):
"""test
"""
values = values
channel = Instrument.ChannelCreator(PresetChannel, (1, 2, ))
def __init__(self, adapter, name="issue channel misbehavior", **kwargs):
super().__init__(adapter,
name,
includeSCPI=False,
write_termination="",
read_termination="",
**kwargs)
def read(self):
return self.read_bytes(1)
def write(self, command):
super().write_bytes(command.encode())
prop = Instrument.measurement(
"A",
""" test """,
)
import pytest
from pymeasure.test import expected_protocol
from pymeasure.instruments import DKTest
def test_prop():
"""Verify the processing the load capacity property"""
with expected_protocol(
DKTest,
[(b'A', b'\x01'), ],
) as inst:
assert inst.prop == 1
def test_prop_channel():
"""Verify the processing the load capacity property via Channel"""
with expected_protocol(
DKTest,
[(b'A', b'\x01'), ],
) as inst:
assert inst.ch_1.prop == 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment