Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from typing import Any, Tuple, Union
import numpy as np
from typish import SubscriptableType, Literal, ClsFunction, EllipsisType
_Size = Union[int, Literal[Any]] # TODO add type vars as well
_Type = Union[type, Literal[Any], np.dtype]
_NSizes = Tuple[_Size, EllipsisType]
_SizeAndType = Tuple[_Size, _Type]
_Sizes = Tuple[_Size, ...]
_SizesAndType = Tuple[Tuple[_Size, ...], _Type]
_NSizesAndType = Tuple[_NSizes, _Type]
_Default = Tuple[Tuple[Literal[Any], EllipsisType], Literal[Any]]
class _NDArrayMeta(SubscriptableType):
_shape = tuple() # Overridden by _NDArray._shape.
_type = ... # Overridden by _NDArray._type.
@property
def dtype(cls) -> np.dtype:
"""
Return the numpy dtype.
:return: the numpy dtype.
"""
return np.dtype(cls._type) # TODO if type is Any, this wont work
@property
def shape(cls) -> Tuple[int, int]:
"""
Return the shape as a tuple of ints.
:return: the shape as a tuple of ints.
from typish import SubscriptableType
class NPType:
"""
The baseclass of all nptyping types.
"""
def __new__(cls, *args, **kwargs) -> None:
raise TypeError('Type {} cannot be instantiated'.format(cls.__name__))
class SimpleNPTypeMeta(SubscriptableType):
"""
A metaclass for all simple NPTypes (e.g. float, int, etc.).
"""
def __repr__(cls):
repr_args = getattr(cls, '_repr_args', None)
if not repr_args:
return cls.__name__
return '{}[{}]'.format(cls.__name__, repr_args)
__str__ = __repr__
"""
PRIVATE MODULE: do not import (from) it directly.
This module contains meta functionality for the ``Array`` type.
"""
from functools import lru_cache
import numpy as np
from typish import SubscriptableType
from typish._types import Ellipsis_, NoneType
class _ArrayMeta(SubscriptableType):
# A Meta class for the Array class.
@lru_cache()
def __getitem__(self, item):
return SubscriptableType.__getitem__(self, item)
def __instancecheck__(self, inst):
result = False
if isinstance(inst, np.ndarray):
result = True # In case of an empty array or no _generic_type.
rows = 0
cols = 0
if len(inst.shape) > 0:
rows = inst.shape[0]
if len(inst.shape) > 1:
cols = inst.shape[1]