| 21 | assert_equal(a[:len(b)], b) |
| 22 | |
| 23 | class SubArray(np.ndarray): |
| 24 | # Defines a generic np.ndarray subclass, that stores some metadata |
| 25 | # in the dictionary `info`. |
| 26 | def __new__(cls,arr,info={}): |
| 27 | x = np.asanyarray(arr).view(cls) |
| 28 | x.info = info.copy() |
| 29 | return x |
| 30 | |
| 31 | def __array_finalize__(self, obj): |
| 32 | super().__array_finalize__(obj) |
| 33 | self.info = getattr(obj, 'info', {}).copy() |
| 34 | return |
| 35 | |
| 36 | def __add__(self, other): |
| 37 | result = super().__add__(other) |
| 38 | result.info['added'] = result.info.get('added', 0) + 1 |
| 39 | return result |
| 40 | |
| 41 | def __iadd__(self, other): |
| 42 | result = super().__iadd__(other) |
| 43 | result.info['iadded'] = result.info.get('iadded', 0) + 1 |
| 44 | return result |
| 45 | |
| 46 | |
| 47 | subarray = SubArray |
no outgoing calls