Skip to content

Commit 325adf6

Browse files
committed
[Enh] Moved 'cheats' to 'helpers' (2/2)
1 parent 8d39baa commit 325adf6

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import np

spm/__wrapper__/helpers/np.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
Struct cannot expose np.ndarray attributes/methods, as they may conflict
3+
with its keys.
4+
5+
This module implements external functions to access these attributes.
6+
7+
Some attributes/methods do not need to be implemented here, as they
8+
already exist in the numpy namespace. Examples include:
9+
10+
* `np.ndim`
11+
* `np.shape`
12+
* `np.size`
13+
* `np.reshape`
14+
* `np.squeeze`
15+
* `np.copy`
16+
* `np.ravel`
17+
18+
as well as all math functions (`np.sum`, `np.mean`, etc.)
19+
"""
20+
import numpy as np
21+
22+
23+
# ----------
24+
# properties
25+
# ----------
26+
27+
28+
def base(array):
29+
"""Equivalent to `array.base`."""
30+
return np.ndarray.view(array, np.ndarray).base
31+
32+
33+
def data(array):
34+
"""Equivalent to `array.data`."""
35+
return np.ndarray.view(array, np.ndarray).data
36+
37+
38+
def dtype(array):
39+
"""Equivalent to `array.dtype`."""
40+
return np.ndarray.view(array, np.ndarray).dtype
41+
42+
43+
def flags(array):
44+
"""Equivalent to `array.flags`."""
45+
return np.ndarray.view(array, np.ndarray).flags
46+
47+
48+
def flat(array):
49+
"""Equivalent to `array.flat`."""
50+
return np.ndarray.view(array, np.ndarray).flat
51+
52+
53+
def strides(array):
54+
"""Equivalent to `array.strides`."""
55+
return np.ndarray.view(array, np.ndarray).strides
56+
57+
58+
def T(array):
59+
"""Equivalent to `array.T`."""
60+
kls = type(array)
61+
return np.ndarray.view(array, np.ndarray).T.view(kls)
62+
63+
64+
def mT(array):
65+
"""Equivalent to `array.mT`."""
66+
kls = type(array)
67+
return np.ndarray.view(array, np.ndarray).mT.view(kls)
68+
69+
70+
# -------
71+
# methods
72+
# -------
73+
74+
75+
def dump(array, file):
76+
"""Equivalent to `array.dump(file)`."""
77+
return np.ndarray.dump(array, file)
78+
79+
80+
def dumps(array, file):
81+
"""Equivalent to `array.dumps(file)`."""
82+
return np.ndarray.dumps(array, file)
83+
84+
85+
def fill(array, value):
86+
"""Equivalent to `array.fill(value)`."""
87+
return np.ndarray.fill(array, value)
88+
89+
90+
def flatten(array):
91+
"""Equivalent to `array.flatten()`."""
92+
return np.ndarray.flatten(array)
93+
94+
95+
def item(array):
96+
"""Equivalent to `array.item()`."""
97+
return np.ndarray.item(array)
98+
99+
100+
def resize(array, *new_shape, **kwargs) -> None:
101+
"""Equivalent to `array.resize(*new_shape)`."""
102+
np.ndarray.resize(array, *new_shape, **kwargs)
103+
104+
105+
def tolist(array):
106+
"""Equivalent to `array.tolist()`."""
107+
return np.ndarray.tolist(array)

0 commit comments

Comments
 (0)