MCPcopy Create free account
hub / github.com/numpy/numpy / ComplicatedSubArray

Class ComplicatedSubArray

numpy/ma/tests/test_subclassing.py:108–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

106
107
108class 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
151class WrappedArray(NDArrayOperatorsMixin):

Callers 3

test_subclass_itemsMethod · 0.85
test_subclass_strMethod · 0.85

Calls

no outgoing calls

Tested by 3

test_subclass_itemsMethod · 0.68
test_subclass_strMethod · 0.68