| 106 | |
| 107 | |
| 108 | class ComplicatedSubArray(SubArray): |
| 109 | |
| 110 | def __str__(self): |
| 111 | return f'myprefix {self.view(SubArray)} mypostfix' |
| 112 | |
| 113 | def __repr__(self): |
| 114 | # Return a repr that does not start with 'name(' |
| 115 | return f'<{self.__class__.__name__} {self}>' |
| 116 | |
| 117 | def _validate_input(self, value): |
| 118 | if not isinstance(value, ComplicatedSubArray): |
| 119 | raise ValueError("Can only set to MySubArray values") |
| 120 | return value |
| 121 | |
| 122 | def __setitem__(self, item, value): |
| 123 | # validation ensures direct assignment with ndarray or |
| 124 | # masked_print_option will fail |
| 125 | super().__setitem__(item, self._validate_input(value)) |
| 126 | |
| 127 | def __getitem__(self, item): |
| 128 | # ensure getter returns our own class also for scalars |
| 129 | value = super().__getitem__(item) |
| 130 | if not isinstance(value, np.ndarray): # scalar |
| 131 | value = value.__array__().view(ComplicatedSubArray) |
| 132 | return value |
| 133 | |
| 134 | @property |
| 135 | def flat(self): |
| 136 | return CSAIterator(self) |
| 137 | |
| 138 | @flat.setter |
| 139 | def flat(self, value): |
| 140 | y = self.ravel() |
| 141 | y[:] = value |
| 142 | |
| 143 | def __array_wrap__(self, obj, context=None): |
| 144 | obj = super().__array_wrap__(obj, context) |
| 145 | if context is not None and context[0] is np.multiply: |
| 146 | obj.info['multiplied'] = obj.info.get('multiplied', 0) + 1 |
| 147 | |
| 148 | return obj |
| 149 | |
| 150 | |
| 151 | class WrappedArray(NDArrayOperatorsMixin): |
no outgoing calls