コード例 #1
0
ファイル: test_parser.py プロジェクト: jcrist/datashape
 def test_typevar_dims(self):
     self.assertEqual(parse('M * bool', self.sym),
                      ct.DataShape(ct.TypeVar('M'), ct.bool_))
     self.assertEqual(parse('A * B * bool', self.sym),
                      ct.DataShape(ct.TypeVar('A'), ct.TypeVar('B'), ct.bool_))
     self.assertEqual(parse('A... * X * 3 * bool', self.sym),
                      ct.DataShape(ct.Ellipsis(ct.TypeVar('A')), ct.TypeVar('X'),
                                   ct.Fixed(3), ct.bool_))
コード例 #2
0
ファイル: test_parser.py プロジェクト: jcrist/datashape
 def test_ellipses(self):
     self.assertEqual(parse('... * bool', self.sym),
                      ct.DataShape(ct.Ellipsis(), ct.bool_))
     self.assertEqual(parse('M * ... * bool', self.sym),
                      ct.DataShape(ct.TypeVar('M'), ct.Ellipsis(), ct.bool_))
     self.assertEqual(parse('M * ... * 3 * bool', self.sym),
                      ct.DataShape(ct.TypeVar('M'), ct.Ellipsis(),
                                   ct.Fixed(3), ct.bool_))
コード例 #3
0
 def resolver(tvar, tvdict):
     if tvar == T.Ellipsis(T.TypeVar('R')):
         a = tvdict[T.Ellipsis(T.TypeVar('A'))]
         b = tvdict[T.TypeVar('B')]
         result = [b]
         for x in a:
             result.extend([x, b])
         return result
     elif tvar == T.TypeVar('T'):
         return T.int16
コード例 #4
0
    def test_binary_dtype_constr(self):
        # Create a symbol table with no types in it, so we can
        # make some isolated type constructors for testing
        sym = datashape.TypeSymbolTable(bare=True)
        # A limited set of dtypes for testing
        sym.dtype['int8'] = ct.int8
        sym.dtype['uint16'] = ct.uint16
        sym.dtype['float64'] = ct.float64
        # TypeVar type constructor
        sym.dtype_constr['typevar'] = ct.TypeVar
        # Binary dtype constructor that asserts on the argument values
        expected_arg = [None, None]

        def _binary_type_constr(a, b):
            self.assertEqual(a, expected_arg[0])
            self.assertEqual(b, expected_arg[1])
            expected_arg[0] = None
            expected_arg[1] = None
            return ct.float32

        sym.dtype_constr['binary'] = _binary_type_constr

        def assertExpectedParse(ds_str, expected_a, expected_b):
            # Set the expected value, and call the parser
            expected_arg[0] = expected_a
            expected_arg[1] = expected_b
            self.assertEqual(parse(ds_str, sym), ct.DataShape(ct.float32))
            # Make sure the expected value was actually run by
            # check that it reset the expected value to None
            self.assertEqual(expected_arg, [None, None],
                             'The test binary type constructor did not run')

        # Positional args
        assertExpectedParse('binary[1, 0]', 1, 0)
        assertExpectedParse('binary[0, "test"]', 0, 'test')
        assertExpectedParse('binary[int8, "test"]', ct.DataShape(ct.int8),
                            'test')
        assertExpectedParse('binary[[1,3,5], "test"]', [1, 3, 5], 'test')
        # Positional and keyword args
        assertExpectedParse('binary[0, b=1]', 0, 1)
        assertExpectedParse('binary["test", b=A]', 'test',
                            ct.DataShape(ct.TypeVar('A')))
        assertExpectedParse('binary[[3, 6], b=int8]', [3, 6],
                            ct.DataShape(ct.int8))
        assertExpectedParse('binary[Arg, b=["x", "test"]]',
                            ct.DataShape(ct.TypeVar('Arg')), ['x', 'test'])
        # Keyword args
        assertExpectedParse('binary[a=1, b=0]', 1, 0)
        assertExpectedParse('binary[a=[int8, A, uint16], b="x"]', [
            ct.DataShape(ct.int8),
            ct.DataShape(ct.TypeVar('A')),
            ct.DataShape(ct.uint16)
        ], 'x')
コード例 #5
0
ファイル: test_parser.py プロジェクト: jcrist/datashape
 def test_var_dims(self):
     self.assertEqual(parse('var * bool', self.sym),
                      ct.DataShape(ct.Var(), ct.bool_))
     self.assertEqual(parse('var * var * bool', self.sym),
                      ct.DataShape(ct.Var(), ct.Var(), ct.bool_))
     self.assertEqual(parse('M * 5 * var * bool', self.sym),
                      ct.DataShape(ct.TypeVar('M'), ct.Fixed(5), ct.Var(), ct.bool_))
コード例 #6
0
 def test_match_equation_dtype(self):
     # A simple coercion
     eqns = _match_equation(dshape('int32'), dshape('int64'))
     self.assertEqual(eqns, [(T.int32, T.int64)])
     # Matching a data type variable
     eqns = _match_equation(dshape('int32'), dshape('D'))
     self.assertEqual(eqns, [(T.int32, T.TypeVar('D'))])
コード例 #7
0
 def test_match_equation_dim(self):
     # Broadcasting a single dimension
     eqns = _match_equation(dshape('1 * int32'), dshape('10 * int32'))
     self.assertEqual(eqns, [(T.Fixed(1), T.Fixed(10)),
                             (T.int32, T.int32)])
     # Matching a dim type variable
     eqns = _match_equation(dshape('3 * int32'), dshape('M * int32'))
     self.assertEqual(eqns, [(T.Fixed(3), T.TypeVar('M')),
                             (T.int32, T.int32)])
コード例 #8
0
    def test_unary_dtype_constr(self):
        # Create a symbol table with no types in it, so we can
        # make some isolated type constructors for testing
        sym = datashape.TypeSymbolTable(bare=True)
        # A limited set of dtypes for testing
        sym.dtype['int8'] = ct.int8
        sym.dtype['uint16'] = ct.uint16
        sym.dtype['float64'] = ct.float64
        # TypeVar type constructor
        sym.dtype_constr['typevar'] = ct.TypeVar
        # Unary dtype constructor that asserts on the argument value
        expected_blah = [None]

        def _unary_type_constr(blah):
            self.assertEqual(blah, expected_blah[0])
            expected_blah[0] = None
            return ct.float32

        sym.dtype_constr['unary'] = _unary_type_constr

        def assertExpectedParse(ds_str, expected):
            # Set the expected value, and call the parser
            expected_blah[0] = expected
            self.assertEqual(parse(ds_str, sym), ct.DataShape(ct.float32))
            # Make sure the expected value was actually run by
            # check that it reset the expected value to None
            self.assertEqual(expected_blah[0], None,
                             'The test unary type constructor did not run')

        # Integer parameter (positional)
        assertExpectedParse('unary[0]', 0)
        assertExpectedParse('unary[100000]', 100000)
        # String parameter (positional)
        assertExpectedParse('unary["test"]', 'test')
        assertExpectedParse("unary['test']", 'test')
        assertExpectedParse('unary["\\uc548\\ub155"]', u'\uc548\ub155')
        assertExpectedParse(u'unary["\uc548\ub155"]', u'\uc548\ub155')
        # DataShape parameter (positional)
        assertExpectedParse('unary[int8]', ct.DataShape(ct.int8))
        assertExpectedParse('unary[X]', ct.DataShape(ct.TypeVar('X')))
        # Empty list parameter (positional)
        assertExpectedParse('unary[[]]', [])
        # List of integers parameter (positional)
        assertExpectedParse('unary[[0, 3, 12]]', [0, 3, 12])
        # List of strings parameter (positional)
        assertExpectedParse('unary[["test", "one", "two"]]',
                            ["test", "one", "two"])
        # List of datashapes parameter (positional)
        assertExpectedParse('unary[[float64, int8, uint16]]', [
            ct.DataShape(ct.float64),
            ct.DataShape(ct.int8),
            ct.DataShape(ct.uint16)
        ])

        # Integer parameter (keyword)
        assertExpectedParse('unary[blah=0]', 0)
        assertExpectedParse('unary[blah=100000]', 100000)
        # String parameter (keyword)
        assertExpectedParse('unary[blah="test"]', 'test')
        assertExpectedParse("unary[blah='test']", 'test')
        assertExpectedParse('unary[blah="\\uc548\\ub155"]', u'\uc548\ub155')
        assertExpectedParse(u'unary[blah="\uc548\ub155"]', u'\uc548\ub155')
        # DataShape parameter (keyword)
        assertExpectedParse('unary[blah=int8]', ct.DataShape(ct.int8))
        assertExpectedParse('unary[blah=X]', ct.DataShape(ct.TypeVar('X')))
        # Empty list parameter (keyword)
        assertExpectedParse('unary[blah=[]]', [])
        # List of integers parameter (keyword)
        assertExpectedParse('unary[blah=[0, 3, 12]]', [0, 3, 12])
        # List of strings parameter (keyword)
        assertExpectedParse('unary[blah=["test", "one", "two"]]',
                            ["test", "one", "two"])
        # List of datashapes parameter (keyword)
        assertExpectedParse('unary[blah=[float64, int8, uint16]]', [
            ct.DataShape(ct.float64),
            ct.DataShape(ct.int8),
            ct.DataShape(ct.uint16)
        ])
コード例 #9
0
        args += ((), )

    # -------------------------------------------------
    # keywords (**kwargs)

    if kwargs and not argspec.keywords:
        unreachable()
    elif kwargs:
        args += (kwargs, )
    elif argspec.keywords:
        args += ({}, )

    return ArgSpec(f, args)


freshvar = lambda: T.TypeVar(gensym())


def dummy_signature(f):
    """Create a dummy signature for `f`"""
    argspec = inspect.getargspec(f)
    n = len(argspec.args)

    argtypes = [freshvar() for i in range(n)]
    restype = freshvar()

    if argspec.varargs:
        argtypes.append(freshvar())
    if argspec.keywords:
        argtypes.append(freshvar())
コード例 #10
0
 def test_match_equation_ellipsis(self):
     # Matching an ellipsis
     eqns = _match_equation(dshape('int32'), dshape('... * int32'))
     self.assertEqual(eqns, [([], T.Ellipsis()),
                             (T.int32, T.int32)])
     eqns = _match_equation(dshape('3 * int32'), dshape('... * int32'))
     self.assertEqual(eqns, [([T.Fixed(3)], T.Ellipsis()),
                             (T.int32, T.int32)])
     eqns = _match_equation(dshape('3 * var * int32'), dshape('... * int32'))
     self.assertEqual(eqns, [([T.Fixed(3), T.Var()], T.Ellipsis()),
                             (T.int32, T.int32)])
     # Matching an ellipsis type variable
     eqns = _match_equation(dshape('int32'), dshape('A... * int32'))
     self.assertEqual(eqns, [([], T.Ellipsis(T.TypeVar('A'))),
                             (T.int32, T.int32)])
     eqns = _match_equation(dshape('3 * int32'), dshape('A... * int32'))
     self.assertEqual(eqns, [([T.Fixed(3)], T.Ellipsis(T.TypeVar('A'))),
                             (T.int32, T.int32)])
     eqns = _match_equation(dshape('3 * var * int32'), dshape('A... * int32'))
     self.assertEqual(eqns, [([T.Fixed(3), T.Var()], T.Ellipsis(T.TypeVar('A'))),
                             (T.int32, T.int32)])
     # Matching an ellipsis with a dim type variable on the left
     eqns = _match_equation(dshape('3 * var * int32'), dshape('A * B... * int32'))
     self.assertEqual(eqns, [(T.Fixed(3), T.TypeVar('A')),
                             ([T.Var()], T.Ellipsis(T.TypeVar('B'))),
                             (T.int32, T.int32)])
     # Matching an ellipsis with a dim type variable on the right
     eqns = _match_equation(dshape('3 * var * int32'), dshape('A... * B * int32'))
     self.assertEqual(eqns, [([T.Fixed(3)], T.Ellipsis(T.TypeVar('A'))),
                             (T.Var(), T.TypeVar('B')),
                             (T.int32, T.int32)])
     # Matching an ellipsis with a dim type variable on both sides
     eqns = _match_equation(dshape('3 * var * int32'), dshape('A * B... * C * int32'))
     self.assertEqual(eqns, [(T.Fixed(3), T.TypeVar('A')),
                             ([], T.Ellipsis(T.TypeVar('B'))),
                             (T.Var(), T.TypeVar('C')),
                             (T.int32, T.int32)])
     eqns = _match_equation(dshape('3 * var * 4 * M * int32'), dshape('A * B... * C * int32'))
     self.assertEqual(eqns, [(T.Fixed(3), T.TypeVar('A')),
                             ([T.Var(), T.Fixed(4)], T.Ellipsis(T.TypeVar('B'))),
                             (T.TypeVar('M'), T.TypeVar('C')),
                             (T.int32, T.int32)])