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

set_parser for seq element setting added #832

Merged
merged 4 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion qcodes/instrument_drivers/tektronix/AWG5014.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ def __init__(self, name, address, timeout=180, num_channels=4, **kwargs):
label='Sequence position',
get_cmd='AWGControl:SEQuencer:POSition?',
set_cmd='SEQuence:JUMP:IMMediate {}',
vals=vals.Ints(1)
vals=vals.PermissiveInts(1),
set_parser=lambda x: int(round(x))
)

# Trigger parameters #
Expand Down
31 changes: 31 additions & 0 deletions qcodes/tests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from unittest import TestCase
from time import sleep

import numpy as np

from qcodes import Function
from qcodes.instrument.parameter import (
Parameter, ArrayParameter, MultiParameter,
Expand Down Expand Up @@ -250,6 +252,35 @@ def test_latest_value(self):
self.assertEqual(p.get_latest(), 21)
self.assertEqual(p.get_values, [21])


class TestValsandParseParameter(TestCase):

def setUp(self):
self.parameter = Parameter(name='foobar',
set_cmd=None, get_cmd=None,
set_parser=lambda x: int(round(x)),
vals=vals.PermissiveInts(0))

def test_setting_int_with_float(self):

a = 0
b = 10
values = np.linspace(a, b, b-a+1)
for i in values:
self.parameter(i)
a = self.parameter()
assert isinstance(a, int)

def test_setting_int_with_float_not_close(self):

a = 0
b = 10
values = np.linspace(a, b, b-a+2)
for i in values[1:-2]:
with self.assertRaises(TypeError):
self.parameter(i)


class SimpleArrayParam(ArrayParameter):
def __init__(self, return_val, *args, **kwargs):
self._return_val = return_val
Expand Down
27 changes: 26 additions & 1 deletion qcodes/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import numpy as np

from qcodes.utils.validators import (Validator, Anything, Bool, Strings,
Numbers, Ints, Enum, MultiType,
Numbers, Ints, PermissiveInts,
Enum, MultiType,
Arrays, Multiples, Lists, Callable, Dict)


Expand Down Expand Up @@ -351,6 +352,30 @@ def test_failed_numbers(self):
Ints(min_value=val)


class TestPermissiveInts(TestCase):


def test_close_to_ints(self):
validator = PermissiveInts()
a = 0
b = 10
values = np.linspace(a, b, b-a+1)
for i in values:
validator.validate(i)

def test_bad_values(self):
validator = PermissiveInts(0, 10)
a = 0
b = 10
values = np.linspace(a, b, b-a+2)
for j,i in enumerate(values):
if j == 0 or j == 11:
validator.validate(i)
else:
with self.assertRaises(TypeError):
validator.validate(i)


class TestEnum(TestCase):
enums = [
[True, False],
Expand Down
21 changes: 21 additions & 0 deletions qcodes/utils/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,27 @@ def __repr__(self):
maxv = self._max_value if self._max_value < BIGINT else None
return '<Ints{}>'.format(range_str(minv, maxv, 'v'))

class PermissiveInts(Ints):
"""
requires an integer or a float close to an integer
optional parameters min_value and max_value enforce
min_value <= value <= max_value
Note that you probably always want to use this with a
set_parser that converts the float repr to an actual int
"""
def validate(self, value, context=''):
if isinstance(value, (float, np.floating)):
intrepr = int(round(value))
remainder = abs(value - intrepr)
if remainder < 1e-05:
castvalue = intrepr
else:
raise TypeError('{} is not an int or close to an int'
'; {}'.format(repr(value), context))
else:
castvalue = value
super().validate(castvalue, context=context)


class Enum(Validator):
"""
Expand Down