Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. class recarray(ndarray):
  2. """Construct an ndarray that allows field access using attributes.
  3. This constructor can be compared to ``empty``: it creates a new record
  4. array but does not fill it with data.
  5.  
  6. def __getattribute__(self, attr):
  7. # See if ndarray has this attr, and return it if so. (note that this
  8. # means a field with the same name as an ndarray attr cannot be
  9. # accessed by attribute).
  10. try:
  11. return object.__getattribute__(self, attr)
  12. except AttributeError: # attr must be a fieldname
  13. pass
  14.  
  15. # look for a field with this name
  16. fielddict = ndarray.__getattribute__(self, 'dtype').fields
  17. try:
  18. res = fielddict[attr][:2]
  19. except (TypeError, KeyError):
  20. raise AttributeError("recarray has no attribute %s" % attr)
  21. obj = self.getfield(*res)
  22.  
  23. # At this point obj will always be a recarray, since (see
  24. # PyArray_GetField) the type of obj is inherited. Next, if obj.dtype is
  25. # non-structured, convert it to an ndarray. If obj is structured leave
  26. # it as a recarray, but make sure to convert to the same dtype.type (eg
  27. # to preserve numpy.record type if present), since nested structured
  28. # fields do not inherit type.
  29. if obj.dtype.fields:
  30. return obj.view(dtype=(self.dtype.type, obj.dtype.fields))
  31. else:
  32. return obj.view(ndarray)
  33.  
  34. if asrecarray:
  35. output = output.view(recarray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement