codekingpro/portable-devtools
115k
1"""
2Container class for backward compatibility with NumArray.
3
4The user_array.container class exists for backward compatibility with NumArray
5and is not meant to be used in new code. If you need to create an array
6container class, we recommend either creating a class that wraps an ndarray
7or subclasses ndarray.
8
9"""
10from numpy._core import (
11 absolute,
12 add,
13 arange,
14 array,
15 asarray,
16 bitwise_and,
17 bitwise_or,
18 bitwise_xor,
19 divide,
20 equal,
21 greater,
22 greater_equal,
23 invert,
24 left_shift,
25 less,
26 less_equal,
27 multiply,
28 not_equal,
29 power,
30 remainder,
31 reshape,
32 right_shift,
33 shape,
34 sin,
35 sqrt,
36 subtract,
37 transpose,
38)
39from numpy._core.overrides import set_module
40
41
42@set_module("numpy.lib.user_array")
43class container:
44 """
45 container(data, dtype=None, copy=True)
46
47 Standard container-class for easy multiple-inheritance.
48
49 Methods
50 -------
51 copy
52 byteswap
53 astype
54
55 """
56 def __init_subclass__(cls) -> None:
57 # Deprecated in NumPy 2.4, 2025-11-24
58 import warnings
59
60 warnings.warn(
61 "The numpy.lib.user_array.container class is deprecated and will be "
62 "removed in a future version.",
63 DeprecationWarning,
64 stacklevel=2,
65 )
66
67 def __init__(self, data, dtype=None, copy=True):
68 self.array = array(data, dtype, copy=copy)
69
70 def __repr__(self):
71 if self.ndim > 0:
72 return self.__class__.__name__ + repr(self.array)[len("array"):]
73 else:
74 return self.__class__.__name__ + "(" + repr(self.array) + ")"
75
76 def __array__(self, t=None):
77 if t:
78 return self.array.astype(t)
79 return self.array
80
81 # Array as sequence
82 def __len__(self):
83 return len(self.array)
84
85 def __getitem__(self, index):
86 return self._rc(self.array[index])
87
88 def __setitem__(self, index, value):
89 self.array[index] = asarray(value, self.dtype)
90
91 def __abs__(self):
92 return self._rc(absolute(self.array))
93
94 def __neg__(self):
95 return self._rc(-self.array)
96
97 def __add__(self, other):
98 return self._rc(self.array + asarray(other))
99
100 __radd__ = __add__
101
102 def __iadd__(self, other):
103 add(self.array, other, self.array)
104 return self
105
106 def __sub__(self, other):
107 return self._rc(self.array - asarray(other))
108
109 def __rsub__(self, other):
110 return self._rc(asarray(other) - self.array)
111
112 def __isub__(self, other):
113 subtract(self.array, other, self.array)
114 return self
115
116 def __mul__(self, other):
117 return self._rc(multiply(self.array, asarray(other)))
118
119 __rmul__ = __mul__
120
121 def __imul__(self, other):
122 multiply(self.array, other, self.array)
123 return self
124
125 def __mod__(self, other):
126 return self._rc(remainder(self.array, other))
127
128 def __rmod__(self, other):
129 return self._rc(remainder(other, self.array))
130
131 def __imod__(self, other):
132 remainder(self.array, other, self.array)
133 return self
134
135 def __divmod__(self, other):
136 return (self._rc(divide(self.array, other)),
137 self._rc(remainder(self.array, other)))
138
139 def __rdivmod__(self, other):
140 return (self._rc(divide(other, self.array)),
141 self._rc(remainder(other, self.array)))
142
143 def __pow__(self, other):
144 return self._rc(power(self.array, asarray(other)))
145
146 def __rpow__(self, other):
147 return self._rc(power(asarray(other), self.array))
148
149 def __ipow__(self, other):
150 power(self.array, other, self.array)
151 return self
152
153 def __lshift__(self, other):
154 return self._rc(left_shift(self.array, other))
155
156 def __rshift__(self, other):
157 return self._rc(right_shift(self.array, other))
158
159 def __rlshift__(self, other):
160 return self._rc(left_shift(other, self.array))
161
162 def __rrshift__(self, other):
163 return self._rc(right_shift(other, self.array))
164
165 def __ilshift__(self, other):
166 left_shift(self.array, other, self.array)
167 return self
168
169 def __irshift__(self, other):
170 right_shift(self.array, other, self.array)
171 return self
172
173 def __and__(self, other):
174 return self._rc(bitwise_and(self.array, other))
175
176 def __rand__(self, other):
177 return self._rc(bitwise_and(other, self.array))
178
179 def __iand__(self, other):
180 bitwise_and(self.array, other, self.array)
181 return self
182
183 def __xor__(self, other):
184 return self._rc(bitwise_xor(self.array, other))
185
186 def __rxor__(self, other):
187 return self._rc(bitwise_xor(other, self.array))
188
189 def __ixor__(self, other):
190 bitwise_xor(self.array, other, self.array)
191 return self
192
193 def __or__(self, other):
194 return self._rc(bitwise_or(self.array, other))
195
196 def __ror__(self, other):
197 return self._rc(bitwise_or(other, self.array))
198
199 def __ior__(self, other):
200 bitwise_or(self.array, other, self.array)
201 return self
202
203 def __pos__(self):
204 return self._rc(self.array)
205
206 def __invert__(self):
207 return self._rc(invert(self.array))
208
209 def _scalarfunc(self, func):
210 if self.ndim == 0:
211 return func(self[0])
212 else:
213 raise TypeError(
214 "only rank-0 arrays can be converted to Python scalars.")
215
216 def __complex__(self):
217 return self._scalarfunc(complex)
218
219 def __float__(self):
220 return self._scalarfunc(float)
221
222 def __int__(self):
223 return self._scalarfunc(int)
224
225 def __hex__(self):
226 return self._scalarfunc(hex)
227
228 def __oct__(self):
229 return self._scalarfunc(oct)
230
231 def __lt__(self, other):
232 return self._rc(less(self.array, other))
233
234 def __le__(self, other):
235 return self._rc(less_equal(self.array, other))
236
237 def __eq__(self, other):
238 return self._rc(equal(self.array, other))
239
240 def __ne__(self, other):
241 return self._rc(not_equal(self.array, other))
242
243 def __gt__(self, other):
244 return self._rc(greater(self.array, other))
245
246 def __ge__(self, other):
247 return self._rc(greater_equal(self.array, other))
248
249 def copy(self):
250 ""
251 return self._rc(self.array.copy())
252
253 def tobytes(self):
254 ""
255 return self.array.tobytes()
256
257 def byteswap(self):
258 ""
259 return self._rc(self.array.byteswap())
260
261 def astype(self, typecode):
262 ""
263 return self._rc(self.array.astype(typecode))
264
265 def _rc(self, a):
266 if len(shape(a)) == 0:
267 return a
268 else:
269 return self.__class__(a)
270
271 def __array_wrap__(self, *args):
272 return self.__class__(args[0])
273
274 def __setattr__(self, attr, value):
275 if attr == 'array':
276 object.__setattr__(self, attr, value)
277 return
278 try:
279 self.array.__setattr__(attr, value)
280 except AttributeError:
281 object.__setattr__(self, attr, value)
282
283 # Only called after other approaches fail.
284 def __getattr__(self, attr):
285 if (attr == 'array'):
286 return object.__getattribute__(self, attr)
287 return self.array.__getattribute__(attr)
288
289
290#############################################################
291# Test of class container
292#############################################################
293if __name__ == '__main__':
294 temp = reshape(arange(10000), (100, 100))
295
296 ua = container(temp)
297 # new object created begin test
298 print(dir(ua))
299 print(shape(ua), ua.shape) # I have changed Numeric.py
300
301 ua_small = ua[:3, :5]
302 print(ua_small)
303 # this did not change ua[0,0], which is not normal behavior
304 ua_small[0, 0] = 10
305 print(ua_small[0, 0], ua[0, 0])
306 print(sin(ua_small) / 3. * 6. + sqrt(ua_small ** 2))
307 print(less(ua_small, 103), type(less(ua_small, 103)))
308 print(type(ua_small * reshape(arange(15), shape(ua_small))))
309 print(reshape(ua_small, (5, 3)))
310 print(transpose(ua_small))
311 