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