Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class recarray(ndarray):
- """Construct an ndarray that allows field access using attributes.
- This constructor can be compared to ``empty``: it creates a new record
- array but does not fill it with data.
- def __getattribute__(self, attr):
- # See if ndarray has this attr, and return it if so. (note that this
- # means a field with the same name as an ndarray attr cannot be
- # accessed by attribute).
- try:
- return object.__getattribute__(self, attr)
- except AttributeError: # attr must be a fieldname
- pass
- # look for a field with this name
- fielddict = ndarray.__getattribute__(self, 'dtype').fields
- try:
- res = fielddict[attr][:2]
- except (TypeError, KeyError):
- raise AttributeError("recarray has no attribute %s" % attr)
- obj = self.getfield(*res)
- # At this point obj will always be a recarray, since (see
- # PyArray_GetField) the type of obj is inherited. Next, if obj.dtype is
- # non-structured, convert it to an ndarray. If obj is structured leave
- # it as a recarray, but make sure to convert to the same dtype.type (eg
- # to preserve numpy.record type if present), since nested structured
- # fields do not inherit type.
- if obj.dtype.fields:
- return obj.view(dtype=(self.dtype.type, obj.dtype.fields))
- else:
- return obj.view(ndarray)
- if asrecarray:
- output = output.view(recarray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement