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

Method __setattr__

numpy/ma/mrecords.py:238–292  ·  view source on GitHub ↗

Sets the attribute attr to the value val.

(self, attr, val)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

filledFunction · 0.90
getmaskarrayFunction · 0.90
__setmask__Method · 0.80
__getattribute__Method · 0.45
__setitem__Method · 0.45

Tested by

no test coverage detected