Example #1
0
    def test_sequenceof(self):
        @arguments.accept(arguments.sequenceof(int))
        def foo(a):
            self.assertTrue(misc.issequenceof(a, int))

        foo([])
        foo([123, 456, 789])

        with self.assertRaises(TypeError):
            foo()
        with self.assertRaises(TypeError):
            foo(123, 456, 789)
        with self.assertRaises(TypeError):
            foo([123, 456, 'blah'])

        @arguments.accept([str])
        def bar(a):
            self.assertTrue(misc.issequenceof(a, str))

        bar([])
        bar(['doo', 'bee', 'doo'])

        with self.assertRaises(TypeError):
            bar()
        with self.assertRaises(TypeError):
            bar('doo', 'bee', 'doo')
        with self.assertRaises(TypeError):
            bar('doo', 'bee', 123)
Example #2
0
    def test_sequenceof(self):
        @arguments.accept(arguments.sequenceof(int))
        def foo(a):
            self.assertTrue(misc.issequenceof(a, int))

        foo([])
        foo([123, 456, 789])

        with self.assertRaises(TypeError):
            foo()
        with self.assertRaises(TypeError):
            foo(123, 456, 789)
        with self.assertRaises(TypeError):
            foo([123, 456, 'blah'])

        @arguments.accept([str])
        def bar(a):
            self.assertTrue(misc.issequenceof(a, str))

        bar([])
        bar(['doo', 'bee', 'doo'])

        with self.assertRaises(TypeError):
            bar()
        with self.assertRaises(TypeError):
            bar('doo', 'bee', 'doo')
        with self.assertRaises(TypeError):
            bar('doo', 'bee', 123)
Example #3
0
    def test_repr(self):
        self.assertEqual(
            repr(arguments._make_constraint(int)),
            'int')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.nullable(int))),
            'nullable(int)')
        self.assertEqual(
            repr(arguments._make_constraint([int])),
            '[int]')
        self.assertEqual(
            repr(arguments._make_constraint((23, 42))),
            '(23, 42)')
        self.assertEqual(
            repr(arguments._make_constraint((int, float, str))),
            '(int, float, str)')
        self.assertEqual(
            repr(arguments._make_constraint([int, float, str])),
            '[int, float, str]')
        self.assertEqual(
            repr(arguments._make_constraint({int: str})),
            '{int: str}')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.flatten(int))),
            'flatten(int)')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.each(int, float))),
            'each(int, float)')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.either(int, str))),
            'either(int, str)')
        self.assertEqual(
            repr(arguments._make_constraint(lambda x: x / 2)),
            'lambda x: x / 2')
        self.assertEqual(
            repr(arguments._make_constraint(
                arguments.condition(lambda x: x < 3))),
            'condition(lambda x: x < 3)')

        def foo(x): x

        constraint = arguments.either(
            arguments.each(int, arguments.condition(lambda x: x%2 == 0)),
            arguments.sequenceof(foo),
            str,
        )
        reprs = 'either(each(int, condition(lambda x: x%2 == 0)), [foo], str)'

        self.assertEqual(repr(arguments._make_constraint(constraint)), reprs)
Example #4
0
    def test_repr(self):
        self.assertEqual(repr(arguments._make_constraint(int)), 'int')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.nullable(int))),
            'nullable(int)')
        self.assertEqual(repr(arguments._make_constraint([int])), '[int]')
        self.assertEqual(repr(arguments._make_constraint((23, 42))),
                         '(23, 42)')
        self.assertEqual(repr(arguments._make_constraint((int, float, str))),
                         '(int, float, str)')
        self.assertEqual(repr(arguments._make_constraint([int, float, str])),
                         '[int, float, str]')
        self.assertEqual(repr(arguments._make_constraint({int: str})),
                         '{int: str}')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.flatten(int))),
            'flatten(int)')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.each(int, float))),
            'each(int, float)')
        self.assertEqual(
            repr(arguments._make_constraint(arguments.either(int, str))),
            'either(int, str)')
        self.assertEqual(repr(arguments._make_constraint(lambda x: x / 2)),
                         'lambda x: x / 2')
        self.assertEqual(
            repr(
                arguments._make_constraint(
                    arguments.condition(lambda x: x < 3))),
            'condition(lambda x: x < 3)')

        def foo(x):
            x

        constraint = arguments.either(
            arguments.each(int, arguments.condition(lambda x: x % 2 == 0)),
            arguments.sequenceof(foo),
            str,
        )
        reprs = 'either(each(int, condition(lambda x: x%2 == 0)), [foo], str)'

        self.assertEqual(repr(arguments._make_constraint(constraint)), reprs)
Example #5
0
    def test_add_varargs(self):
        @arguments.accept(arguments.sequenceof(int), add_varargs=True)
        def foo(a):
            self.assertTrue(misc.issequenceof(a, int))

        foo(123)
        foo(123, 456, 789)

        with self.assertRaises(TypeError):
            foo()
        with self.assertRaises(TypeError):
            foo(123, 456, 'blah')
        with self.assertRaises(TypeError):
            foo(123, [456, 789])

        @arguments.accept(int, [int], add_varargs=True)
        def bar(a, b):
            self.assertEqual(a, 123)
            self.assertTupleEqual(b, (456, 789))

        bar(123, 456, 789)
Example #6
0
    def test_add_varargs(self):
        @arguments.accept(arguments.sequenceof(int), add_varargs=True)
        def foo(a):
            self.assertTrue(misc.issequenceof(a, int))

        foo(123)
        foo(123, 456, 789)

        with self.assertRaises(TypeError):
            foo()
        with self.assertRaises(TypeError):
            foo(123, 456, 'blah')
        with self.assertRaises(TypeError):
            foo(123, [456, 789])

        @arguments.accept(int, [int], add_varargs=True)
        def bar(a, b):
            self.assertEqual(a, 123)
            self.assertTupleEqual(b, (456, 789))

        bar(123, 456, 789)
Example #7
0
    def test_either(self):
        @arguments.accept(arguments.either(int, str))
        def foo(a): pass

        foo(123)
        foo('blah')

        with self.assertRaises(TypeError):
            foo()
        with self.assertRaises(TypeError):
            foo(123.456)

        @arguments.accept(arguments.either(int, arguments.sequenceof(str)))
        def bar(a): pass

        bar(123)
        bar(['doo', 'bee', 'doo'])

        with self.assertRaises(TypeError):
            bar('blah')
        with self.assertRaises(TypeError):
            bar([123, 456, 789])
Example #8
0
    def test_either(self):
        @arguments.accept(arguments.either(int, str))
        def foo(a):
            pass

        foo(123)
        foo('blah')

        with self.assertRaises(TypeError):
            foo()
        with self.assertRaises(TypeError):
            foo(123.456)

        @arguments.accept(arguments.either(int, arguments.sequenceof(str)))
        def bar(a):
            pass

        bar(123)
        bar(['doo', 'bee', 'doo'])

        with self.assertRaises(TypeError):
            bar('blah')
        with self.assertRaises(TypeError):
            bar([123, 456, 789])
Example #9
0
def reset():
    global _config, _config_overridden, _hooks
    _config = _DEFAULT_CONFIG.copy()
    _config_overridden = []
    _hooks = []
    _config_updated()



@_arguments.accept(kwargs = {
    'backend':          tuple(_VALID_BACKENDS),
    'client_name':      str,
    'in_ports':         _arguments.either(
                            _arguments.each(int,
                                _arguments.condition(lambda x: x > 0)),
                            _arguments.sequenceof(
                                _arguments.either(str, [str])),
                        ),
    'out_ports':        _arguments.either(
                            _arguments.each(int,
                                _arguments.condition(lambda x: x > 0)),
                            _arguments.sequenceof(
                                _arguments.either(str, [str])),
                        ),
    'data_offset':      (0, 1),
    'octave_offset':    int,
    'initial_scene':    _arguments.either(
                            int,
                            _arguments.each(tuple, [int]),
                            _arguments.each(tuple, [int, int])
                        ),
    'start_delay':      (int, float, type(None)),
Example #10
0
def reset():
    global _config, _config_overridden, _hooks
    _config = _DEFAULT_CONFIG.copy()
    _config_overridden = []
    _hooks = []
    _config_updated()



@_arguments.accept(kwargs = {
    'backend':          tuple(_VALID_BACKENDS),
    'client_name':      str,
    'in_ports':         _arguments.either(
                            _arguments.each(int,
                                _arguments.condition(lambda x: x >= 0)),
                            _arguments.sequenceof(
                                _arguments.either(str, [str])),
                        ),
    'out_ports':        _arguments.either(
                            _arguments.each(int,
                                _arguments.condition(lambda x: x >= 0)),
                            _arguments.sequenceof(
                                _arguments.either(str, [str])),
                        ),
    'data_offset':      (0, 1),
    'octave_offset':    int,
    'initial_scene':    _arguments.either(
                            int,
                            _arguments.each(tuple, [int]),
                            _arguments.each(tuple, [int, int])
                        ),
    'start_delay':      (int, float, type(None)),