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

Method __setattr__

numpy/ma/mrecords.py:227–281  ·  view source on GitHub ↗

Sets the attribute attr to the value val.

(self, attr, val)

Source from the content-addressed store, hash-verified

225 return obj
226
227 def __setattr__(self, attr, val):
228 """
229 Sets the attribute attr to the value val.
230
231 """
232 # Should we call __setmask__ first ?
233 if attr in ['mask', 'fieldmask']:
234 self.__setmask__(val)
235 return
236 # Create a shortcut (so that we don't have to call getattr all the time)
237 _localdict = object.__getattribute__(self, '__dict__')
238 # Check whether we're creating a new field
239 newattr = attr not in _localdict
240 try:
241 # Is attr a generic attribute ?
242 ret = object.__setattr__(self, attr, val)
243 except Exception:
244 # Not a generic attribute: exit if it's not a valid field
245 fielddict = np.ndarray.__getattribute__(self, 'dtype').fields or {}
246 optinfo = np.ndarray.__getattribute__(self, '_optinfo') or {}
247 if not (attr in fielddict or attr in optinfo):
248 raise
249 else:
250 # Get the list of names
251 fielddict = np.ndarray.__getattribute__(self, 'dtype').fields or {}
252 # Check the attribute
253 if attr not in fielddict:
254 return ret
255 if newattr:
256 # We just added this one or this setattr worked on an
257 # internal attribute.
258 try:
259 object.__delattr__(self, attr)
260 except Exception:
261 return ret
262 # Let's try to set the field
263 try:
264 res = fielddict[attr][:2]
265 except (TypeError, KeyError) as e:
266 raise AttributeError(
267 f'record array has no attribute {attr}') from e
268
269 if val is ma.masked:
270 _fill_value = _localdict['_fill_value']
271 if _fill_value is not None:
272 dval = _localdict['_fill_value'][attr]
273 else:
274 dval = val
275 mval = True
276 else:
277 dval = ma.filled(val)
278 mval = ma.getmaskarray(val)
279 obj = np.ndarray.__getattribute__(self, '_data').setfield(dval, *res)
280 _localdict['_mask'].__setitem__(attr, mval)
281 return obj
282
283 def __getitem__(self, indx):
284 """

Callers

nothing calls this directly

Calls 4

__setmask__Method · 0.80
__getattribute__Method · 0.45
filledMethod · 0.45
__setitem__Method · 0.45

Tested by

no test coverage detected