Пример #1
0
    def test_type_cast(self):
        self.assertTrue(is_function(int, 1))
        self.assertTrue(is_function(float, 1))
        self.assertTrue(is_function(str, 1))

        self.assertFalse(is_function(int, 0) or is_function(int, 2))
        self.assertFalse(is_function(float, 0) or is_function(float, 2))
        self.assertFalse(is_function(str, 0) or is_function(str, 2))
Пример #2
0
def test_type_cast():
    assert is_function(int, 1)
    assert is_function(float, 1)
    assert is_function(str, 1)

    assert not (is_function(int, 0) or is_function(int, 2))
    assert not (is_function(float, 0) or is_function(float, 2))
    assert not (is_function(str, 0) or is_function(str, 2))
Пример #3
0
    def test_coroutine_check(self):
        def f_sync():
            raise RuntimeError('function should not get called')

        self.assertTrue(is_function(f_sync, 0))
        self.assertTrue(is_function(f_sync, 0, coroutine=False))

        async def f_async():
            raise RuntimeError('function should not get called')

        self.assertFalse(is_function(f_async, 0, coroutine=False))
        self.assertTrue(is_function(f_async, 0, coroutine=True))
        self.assertFalse(is_function(f_async, 0))
Пример #4
0
def test_coroutine_check():
    def f_sync():
        raise RuntimeError('function should not get called')

    assert is_function(f_sync, 0)
    assert is_function(f_sync, 0, coroutine=False)

    async def f_async():
        raise RuntimeError('function should not get called')

    assert not is_function(f_async, 0, coroutine=False)
    assert is_function(f_async, 0, coroutine=True)
    assert not is_function(f_async, 0)
Пример #5
0
    def test_coroutine_check(self):
        def f_sync():
            raise RuntimeError('function should not get called')

        self.assertTrue(is_function(f_sync, 0))
        self.assertTrue(is_function(f_sync, 0, coroutine=False))

        # support pre-py3.5 async syntax
        @asyncio.coroutine
        def f_async_old():
            raise RuntimeError('function should not get called')

        self.assertFalse(is_function(f_async_old, 0, coroutine=False))
        self.assertTrue(is_function(f_async_old, 0, coroutine=True))
        self.assertFalse(is_function(f_async_old, 0))

        # test py3.5 syntax async functions
        try:
            from qcodes.tests.py35_syntax import f_async
            py35 = True
        except:
            py35 = False

        if py35:
            self.assertFalse(is_function(f_async, 0, coroutine=False))
            self.assertTrue(is_function(f_async, 0, coroutine=True))
            self.assertFalse(is_function(f_async, 0))
Пример #6
0
 def test_methods(self):
     a = self.AClass()
     self.assertTrue(is_function(a.method_a, 0))
     self.assertFalse(is_function(a.method_a, 1))
     self.assertTrue(is_function(a.method_b, 1))
     self.assertTrue(is_function(a.method_c, 1, coroutine=True))
Пример #7
0
    def test_function(self):
        def f0():
            raise RuntimeError('function should not get called')

        def f1(a):
            raise RuntimeError('function should not get called')

        def f2(a, b):
            raise RuntimeError('function should not get called')

        self.assertTrue(is_function(f0, 0))
        self.assertTrue(is_function(f1, 1))
        self.assertTrue(is_function(f2, 2))

        self.assertFalse(is_function(f0, 1) or is_function(f0, 2))
        self.assertFalse(is_function(f1, 0) or is_function(f1, 2))
        self.assertFalse(is_function(f2, 0) or is_function(f2, 1))

        # make sure we only accept valid arg_count
        with self.assertRaises(TypeError):
            is_function(f0, 'lots')
        with self.assertRaises(TypeError):
            is_function(f0, -1)
Пример #8
0
 def test_non_function(self):
     self.assertFalse(is_function(0, 0))
     self.assertFalse(is_function('hello!', 0))
     self.assertFalse(is_function(None, 0))
Пример #9
0
    def __init__(self,
                 arg_count,
                 cmd=None,
                 exec_str=None,
                 input_parser=None,
                 output_parser=None,
                 no_cmd_function=None):

        self.arg_count = arg_count

        if (no_cmd_function is not None
                and not is_function(no_cmd_function, arg_count)):
            raise TypeError('no_cmd_function must be none or a function '
                            'taking the same args as the command, not '
                            '{}'.format(no_cmd_function))

        if input_parser is None:
            parse_input = False
        elif is_function(input_parser, arg_count):
            parse_input = True if arg_count == 1 else 'multi'
            self.input_parser = input_parser
        else:
            raise TypeError(
                'input_parser must be a function with one arg_count args,'
                ' not {}'.format(repr(input_parser)))

        if output_parser is None:
            parse_output = False
        elif is_function(output_parser, 1):
            parse_output = True
            self.output_parser = output_parser
        else:
            raise TypeError('output_parser must be a function with one arg,'
                            ' not {}'.format(repr(output_parser)))

        if isinstance(cmd, str):
            self.cmd_str = cmd
            self.exec_str = exec_str

            if is_function(exec_str, 1):
                self.exec_function = {  # (parse_input, parse_output)
                    (False, False): self.call_by_str,
                    (False, True): self.call_by_str_parsed_out,
                    (True, False): self.call_by_str_parsed_in,
                    (True, True): self.call_by_str_parsed_in_out,
                    ('multi', False): self.call_by_str_parsed_in2,
                    ('multi', True): self.call_by_str_parsed_in2_out
                }[(parse_input, parse_output)]

            elif exec_str is not None:
                raise TypeError('exec_str must be a function with one arg,' +
                                ' not {}'.format(repr(exec_str)))

        elif is_function(cmd, arg_count):
            self._cmd = cmd
            self.exec_function = {  # (parse_input, parse_output)
                (False, False): cmd,
                (False, True): self.call_cmd_parsed_out,
                (True, False): self.call_cmd_parsed_in,
                (True, True): self.call_cmd_parsed_in_out,
                ('multi', False): self.call_cmd_parsed_in2,
                ('multi', True): self.call_cmd_parsed_in2_out
            }[(parse_input, parse_output)]

        elif cmd is None:
            if no_cmd_function is not None:
                self.exec_function = no_cmd_function
            else:
                raise NoCommandError('no ``cmd`` provided')

        else:
            raise TypeError('cmd must be a string or function with ' +
                            '{} args'.format(arg_count))
Пример #10
0
 def _validate_callable(self, func, arg_count=0):
     if not is_function(func, arg_count):
         raise TypeError('function must be a callable taking '
                         '{} arguments, not {}'.format(
                             arg_count, repr(func)))
Пример #11
0
 def __init__(self, condition):
     if not is_function(condition, 0):
         raise TypeError('BreakIf condition must be a callable with '
                         'no arguments')
     self.condition = condition
Пример #12
0
def test_methods():
    a = AClass()
    assert is_function(a.method_a, 0)
    assert not is_function(a.method_a, 1)
    assert is_function(a.method_b, 1)
    assert is_function(a.method_c, 1, coroutine=True)
Пример #13
0
def test_non_function():
    assert not is_function(0, 0)
    assert not is_function('hello!', 0)
    assert not is_function(None, 0)
Пример #14
0
def test_function():
    def f0():
        raise RuntimeError('function should not get called')

    def f1(a):
        raise RuntimeError('function should not get called')

    def f2(a, b):
        raise RuntimeError('function should not get called')

    assert is_function(f0, 0)
    assert is_function(f1, 1)
    assert is_function(f2, 2)

    assert not (is_function(f0, 1) or is_function(f0, 2))
    assert not (is_function(f1, 0) or is_function(f1, 2))
    assert not (is_function(f2, 0) or is_function(f2, 1))

    # make sure we only accept valid arg_count
    with pytest.raises(TypeError):
        is_function(f0, 'lots')
    with pytest.raises(TypeError):
        is_function(f0, -1)
Пример #15
0
    def __init__(
        self,
        arg_count: int,
        cmd: Optional[Union[str, Callable[..., Output]]] = None,
        exec_str: Optional[Callable[[str], Output]] = None,
        input_parser: Optional[Callable] = None,
        output_parser: Optional[Callable[[Output], ParsedOutput]] = None,
        no_cmd_function: Optional[Callable] = None,
    ):

        self.arg_count = arg_count

        if no_cmd_function is not None and not is_function(
                no_cmd_function, arg_count):
            raise TypeError(f"no_cmd_function must be None or a function "
                            f"taking the same args as the command, not "
                            f"{no_cmd_function}")

        if input_parser is None:
            parse_input: Union[bool, Literal["multi"]] = False
        elif is_function(input_parser, arg_count):
            parse_input = True if arg_count == 1 else "multi"
            self.input_parser = input_parser
        else:
            raise TypeError(
                f"input_parser must be a function with arg_count = "
                f"{arg_count} args or None, not {input_parser!r}")

        if output_parser is None:
            parse_output = False
        elif is_function(output_parser, 1):
            parse_output = True
            self.output_parser = output_parser
        else:
            raise TypeError(f"output_parser must be a function with one arg "
                            f"or None, not {output_parser!r}")

        if isinstance(cmd, str):
            self.cmd_str = cmd
            if exec_str is None:
                raise TypeError("exec_str cannot be None if cmd is a str.")

            self.exec_str = exec_str

            if is_function(exec_str, 1):
                self.exec_function = {  # (parse_input, parse_output)
                    (False, False): self.call_by_str,
                    (False, True): self.call_by_str_parsed_out,
                    (True, False): self.call_by_str_parsed_in,
                    (True, True): self.call_by_str_parsed_in_out,
                    ("multi", False): self.call_by_str_parsed_in2,
                    ("multi", True): self.call_by_str_parsed_in2_out,
                }[(parse_input, parse_output)]

            elif exec_str is not None:
                raise TypeError(
                    f"exec_str must be a function with one arg, not {exec_str!r}"
                )

        elif is_function(cmd, arg_count):
            assert cmd is not None
            self._cmd = cmd
            self.exec_function = {  # (parse_input, parse_output)
                (False, False): cmd,
                (False, True): self.call_cmd_parsed_out,
                (True, False): self.call_cmd_parsed_in,
                (True, True): self.call_cmd_parsed_in_out,
                ("multi", False): self.call_cmd_parsed_in2,
                ("multi", True): self.call_cmd_parsed_in2_out,
            }[(parse_input, parse_output)]

        elif cmd is None:
            if no_cmd_function is not None:
                self.exec_function = no_cmd_function
            else:
                raise NoCommandError("no ``cmd`` provided")

        else:
            raise TypeError(
                f"cmd must be a string or function with arg_count={arg_count} args"
            )