示例#1
0
def test_no_cmd():
    with pytest.raises(NoCommandError):
        Command(0)

    def no_cmd_function():
        raise CustomError('no command')

    no_cmd = Command(0, no_cmd_function=no_cmd_function)
    with pytest.raises(CustomError):
        no_cmd()
示例#2
0
    def test_no_cmd(self):
        with self.assertRaises(NoCommandError):
            Command(0)

        def no_cmd_function():
            raise CustomError('no command')

        no_cmd = Command(0, no_cmd_function=no_cmd_function)
        with self.assertRaises(CustomError):
            no_cmd()
示例#3
0
    def _set_get(self, get_cmd, get_parser):
        exec_str = self._instrument.ask if self._instrument else None
        self._get = Command(arg_count=0, cmd=get_cmd, exec_str=exec_str,
                            output_parser=get_parser,
                            no_cmd_function=no_getter)

        self.has_get = (get_cmd is not None)
示例#4
0
    def test_cmd_function(self):
        def myexp(a, b):
            return a ** b

        cmd = Command(2, myexp)
        self.assertEqual(cmd(10, 3), 1000)

        with self.assertRaises(TypeError):
            Command(3, myexp)

        # with output parsing
        cmd = Command(2, myexp, output_parser=lambda x: 5 * x)
        self.assertEqual(cmd(10, 3), 5000)

        # input parsing
        cmd = Command(1, abs, input_parser=lambda x: x + 1)
        self.assertEqual(cmd(-10), 9)

        # input *and* output parsing
        cmd = Command(1, abs, input_parser=lambda x: x + 2,
                      output_parser=lambda x: 3 * x)
        self.assertEqual(cmd(-6), 12)

        # multi-input parsing, no output parsing
        cmd = Command(2, myexp, input_parser=lambda x, y: (y, x))
        self.assertEqual(cmd(3, 10), 1000)

        # multi-input parsing *and* output parsing
        cmd = Command(2, myexp, input_parser=lambda x, y: (y, x),
                      output_parser=lambda x: 10 * x)
        self.assertEqual(cmd(8, 2), 2560)
示例#5
0
def test_cmd_function():
    def myexp(a, b):
        return a**b

    cmd = Command(2, myexp)
    assert cmd(10, 3) == 1000

    with pytest.raises(TypeError):
        Command(3, myexp)

    # with output parsing
    cmd = Command(2, myexp, output_parser=lambda x: 5 * x)
    assert cmd(10, 3) == 5000

    # input parsing
    cmd = Command(1, abs, input_parser=lambda x: x + 1)
    assert cmd(-10) == 9

    # input *and* output parsing
    cmd = Command(1,
                  abs,
                  input_parser=lambda x: x + 2,
                  output_parser=lambda x: 3 * x)
    assert cmd(-6) == 12

    # multi-input parsing, no output parsing
    cmd = Command(2, myexp, input_parser=lambda x, y: (y, x))
    assert cmd(3, 10) == 1000

    # multi-input parsing *and* output parsing
    cmd = Command(2,
                  myexp,
                  input_parser=lambda x, y: (y, x),
                  output_parser=lambda x: 10 * x)
    assert cmd(8, 2) == 2560
示例#6
0
    def _set_set(self, set_cmd, set_parser):
        # note: this does not set the final setter functions. that's handled
        # in self.set_sweep, when we choose a swept or non-swept setter.
        # TODO(giulioungaretti) lies! that method does not exis.
        # probably alexj left it out :(
        exec_str = self._instrument.write if self._instrument else None
        self._set = Command(arg_count=1, cmd=set_cmd, exec_str=exec_str,
                            input_parser=set_parser, no_cmd_function=no_setter)

        self.has_set = set_cmd is not None
示例#7
0
    def _set_set(self, set_cmd, set_parser):
        # note: this does not set the final setter functions. that's handled
        # in self.set_sweep, when we choose a swept or non-swept setter.
        exec_str = self._instrument.write if self._instrument else None
        self._set = Command(arg_count=1,
                            cmd=set_cmd,
                            exec_str=exec_str,
                            input_parser=set_parser,
                            no_cmd_function=no_setter)

        self.has_set = set_cmd is not None
示例#8
0
    def _set_call(self, call_cmd, arg_parser, return_parser):
        if self._instrument:
            ask_or_write = self._instrument.write
            if isinstance(call_cmd, str) and return_parser:
                ask_or_write = self._instrument.ask
        else:
            ask_or_write = None

        self._call = Command(arg_count=self._arg_count,
                             cmd=call_cmd,
                             exec_str=ask_or_write,
                             input_parser=arg_parser,
                             output_parser=return_parser)
示例#9
0
def test_cmd_str():
    def f_now(x):
        return x + ' now'

    def upper(s):
        return s.upper()

    def reversestr(s):
        return s[::-1]

    def swap(a, b):
        return b, a

    # basic exec_str
    cmd = Command(0, 'pickles', exec_str=f_now)
    assert cmd() == 'pickles now'

    # with output parsing
    cmd = Command(0, 'blue', exec_str=f_now, output_parser=upper)
    assert cmd() == 'BLUE NOW'

    # parameter insertion
    cmd = Command(3, '{} is {:.2f}% better than {}', exec_str=f_now)
    assert cmd('ice cream', 56.2, 'cake') == \
                     'ice cream is 56.20% better than cake now'
    with pytest.raises(ValueError):
        cmd('cake', 'a whole lot', 'pie')

    with pytest.raises(TypeError):
        cmd('donuts', 100, 'bagels', 'with cream cheese')

    # input parsing
    cmd = Command(1, 'eat some {}', exec_str=f_now, input_parser=upper)
    assert cmd('ice cream') == 'eat some ICE CREAM now'

    # input *and* output parsing
    cmd = Command(1,
                  'eat some {}',
                  exec_str=f_now,
                  input_parser=upper,
                  output_parser=reversestr)
    assert cmd('ice cream') == 'won MAERC ECI emos tae'

    # multi-input parsing, no output parsing
    cmd = Command(2, '{} and {}', exec_str=f_now, input_parser=swap)
    assert cmd('I', 'you') == 'you and I now'

    # multi-input parsing *and* output parsing
    cmd = Command(2,
                  '{} and {}',
                  exec_str=f_now,
                  input_parser=swap,
                  output_parser=upper)
    assert cmd('I', 'you') == 'YOU AND I NOW'
示例#10
0
文件: function.py 项目: zhinst/Qcodes
    def _set_call(self, call_cmd: Optional[Union[str, Callable]],
                  arg_parser: Optional[Callable],
                  return_parser: Optional[Callable]) -> None:
        if self._instrument:
            ask_or_write = self._instrument.write
            if isinstance(call_cmd, str) and return_parser:
                ask_or_write = self._instrument.ask
        else:
            ask_or_write = None

        self._call = Command(arg_count=self._arg_count,
                             cmd=call_cmd,
                             exec_str=ask_or_write,
                             input_parser=arg_parser,
                             output_parser=return_parser)
示例#11
0
    def test_bad_calls(self):
        with self.assertRaises(TypeError):
            Command()

        with self.assertRaises(TypeError):
            Command(cmd='')

        with self.assertRaises(TypeError):
            Command(0, '', output_parser=lambda: 1)

        with self.assertRaises(TypeError):
            Command(1, '', input_parser=lambda: 1)

        with self.assertRaises(TypeError):
            Command(0, cmd='', exec_str='not a function')

        with self.assertRaises(TypeError):
            Command(0, cmd=lambda: 1, no_cmd_function='not a function')