nptyping.types._ndarray   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A NDArray.type_of() 0 12 1
1
from typing import (
2
    Any,
3
    Callable,
4
    Type,
5
)
6
7
import numpy as np
8
9
from nptyping.types._ndarray_meta import _NDArray
10
11
12
class NDArray(_NDArray, np.ndarray):
13
    """
14
    NDArray is a representation of numpy.ndarray.
15
16
    An Array with any dimensions of any size and any type:
17
    >>> NDArray
18
    NDArray[(typing.Any, ...), typing.Any]
19
20
    An array with 1 dimension of any size and any type:
21
    >>> from typing import Any
22
    >>> from nptyping import Int64
23
    >>> NDArray[Any]
24
    NDArray[(typing.Any,), typing.Any]
25
26
    An array with 1 dimension of size 3 and any type:
27
    >>> NDArray[3]
28
    NDArray[(3,), typing.Any]
29
30
    An array with any dimensions of any size and type int:
31
    >>> NDArray[Int64]
32
    NDArray[(typing.Any, ...), Int[64]]
33
34
    An array with 1 dimension of size 3 and type int:
35
    >>> NDArray[3, Int64]
36
    NDArray[(3,), Int[64]]
37
38
    An array with any dimensions of size 3 and type int:
39
    >>> NDArray[(3, ...), Int64]
40
    NDArray[(3, ...), Int[64]]
41
42
    An array with 3 dimensions of sizes 3, 3, 5 and type int:
43
    >>> NDArray[(3, 3, 5), Int64]
44
    NDArray[(3, 3, 5), Int[64]]
45
46
    """
47
48
    # These variables are to let typish know to use the custom checks.
49
    __instancecheck__ = None  # type: Callable[[Any], bool]
50
    __subclasscheck__ = None  # type: Callable[[Any], bool]
51
52
    @staticmethod
53
    def type_of(arr: np.ndarray) -> Type['NDArray']:
54
        """
55
        Return an nptyping.NDArray type T such that isinstance(arr, T).
56
57
        >>> NDArray.type_of(np.array([[1, 2], [3, 4.0]]))
58
        NDArray[(2, 2), Float[64]]
59
60
        :param arr: any numpy.ndarray.
61
        :return: a nptyping.NDArray.
62
        """
63
        return NDArray[arr.shape, arr.dtype]
64