예제 #1
0
 def test_understands(self):
     function = ClsFunction({
         int: lambda _:...,
         str: lambda _:...,
     })
     self.assertTrue(function.understands(1))
     self.assertTrue(function.understands('2'))
     self.assertTrue(not function.understands(3.0))
예제 #2
0
    def _after_subscription(cls, item: Any) -> None:
        method = ClsFunction(
            OrderedDict([
                (_Size, cls._only_size),
                (_Type, cls._only_type),
                (_NSizes, lambda _:...),
                (_SizeAndType, cls._size_and_type),
                (_Sizes, cls._only_sizes),
                (_SizesAndType, cls._sizes_and_type),
                (_NSizesAndType, cls._sizes_and_type),
                (_Default, lambda _:...),
            ]))

        if not method.understands(item):
            raise TypeError('Invalid parameter for NDArray: "{}"'.format(item))
        return method(item)
예제 #3
0
    def __getitem__(cls, item: Any) -> None:
        method = ClsFunction(
            OrderedDict([
                (_Size, cls._only_size),
                (_Type, cls._only_type),
                (_NSizes, lambda _: (None, None)),
                (_SizeAndType, cls._size_and_type),
                (_SizeAndTypeAny, cls._size_and_type),  # For Python 3.5.
                (_Sizes, cls._only_sizes),
                (_SizesAndType, cls._sizes_and_type),
                (_SizesAndTypeAny, cls._sizes_and_type),  # For Python 3.5.
                (_NSizesAndType, cls._sizes_and_type),
                (_NSizesAndTypeAny, cls._sizes_and_type),  # For Python 3.5.
                (_Default, lambda _: (None, None)),
            ]))

        if not method.understands(item):
            raise TypeError('Invalid parameter for NDArray: "{}"'.format(item))
        shape, type_ = method(item)
        return HashedSubscriptableType.__getitem__(cls, (shape, type_))