[Numpy-discussion] RecArray.tolist() suggestion

Colin J. Williams cjw at sympatico.ca
Thu Jul 15 08:22:07 CDT 2004


Francesc Alted wrote:

>A Dimarts 13 Juliol 2004 10:28, Francesc Alted va escriure:
>  
>
>>A Dilluns 12 Juliol 2004 23:14, Perry Greenfield va escriure:
>>    
>>
>>>What I'm wondering about is what a single element of a record array
>>>should be. Returning a tuple has an undeniable simplicity to it.
>>>      
>>>
>>Yeah, this why I'm strongly biased toward this possibility.
>>
>>    
>>
>>>On the other hand, we've been using recarrays that allow naming the
>>>various columns (which we refer to as "fields").  If one can refer
>>>to fields of a recarray, shouldn't one be able to refer to a field
>>>(by name) of one of it's elements? Or are you proposing that basic
>>>recarrays not have that sort of capability (something added by a
>>>subclass)?
>>>      
>>>
>>Well, I'm not sure about that. But just in case most of people would like to
>>access records by field as well as by index, I would advocate for the
>>possibility that the Record instances would behave as similar as possible as
>>a tuple (or dictionary?). That include creating appropriate __str__() *and*
>>__repr__() methods as well as __getitem__() that supports both name fields
>>and indices. I'm not sure about whether providing an __getattr__() method
>>would ok, but for the sake of simplicity and in order to have (preferably)
>>only one way to do things, I would say no.
>>    
>>
>
>I've been thinking that one can made compatible to return a tuple on a
>single element of a RecArray and still being able to retrieve a field by
>name is to play with the RecArray.__getitem__ and let it to suport key names
>in addition to indices. This would be better seen as an example:
>
>Right now, one can say:
>
>  
>
>>>>r=records.array([(1,"asds", 24.),(2,"pwdw", 48.)], "1i4,1a4,1f8")
>>>>r._fields["c1"]
>>>>        
>>>>
>array([1, 2])
>  
>
>>>>r._fields["c1"][1]
>>>>        
>>>>
>2
>
>What I propose is to be able to say:
>
>  
>
>>>>r["c1"]
>>>>        
>>>>
>array([1, 2])
>  
>
>>>>r["c1"][1]
>>>>
I would suggest going a step beyond this, so that one can have r.c1[1], 
see the script below.
I have not explored the assignment of a value to r.c1.[1], but it seems 
to be achievable.
If changes along this line are acceptable, it is suggested that fields 
be renamed cols, or some
such, to indicate its wider impact.

Colin W.

>>>>        
>>>>
>2
>
>Which would replace the notation:
>
>  
>
>>>>r[1]["c1"]
>>>>        
>>>>
>2
>
>which was recently suggested.
>
>I.e. the suggestion is to realize RecArrays as a collection of columns,
>as well as a collection of rows.
>  
>
# tRecord.py to explore RecArray

import numarray.records as _rec
import sys
#
class Rec1(_rec.RecArray):
  def __new__(cls, buffer, formats, shape=0, names=None, byteoffset=0,
                 bytestride=None, byteorder=sys.byteorder, aligned=0):
    # This calls RecArray.__init__ - reason unclear.
    # Why can't the instance be fully created by RecArray.__init__?
    return _rec.RecArray.__new__(cls, buffer, formats=formats, 
shape=shape, names=names,
                         byteorder=byteorder, aligned=aligned)

  def __init__(self, buffer, formats, shape=0, names=None, byteoffset=0,
               bytestride=None, byteorder=sys.byteorder, aligned=0):
 
    arr= _rec.array(buffer, formats=formats, shape=shape, names=names,
                   byteorder=byteorder, aligned=aligned)

    self.__setstate__(arr.__getstate__())

  def __getattr__(self, name):
    # We reach here if the attribute does not belong to the basic Rec1 set
    return self._fields[name]
       
  def __getattribute__(self, name):
    return _rec.RecArray.__getattribute__(self, name)
 
  def __repr__(self):
    return self.__class__.__name__ + _rec.RecArray.__repr__(self)[8:]

  def __setattr__(self, name, value):
    return _rec.RecArray.__setattr__(self, name, value)
 
  def __str__(self):
    return self.__class__.__name__ + _rec.RecArray.__str__(self)[8:]  
   
if __name__ == '__main__':   
  # Frances Alted 13-Jul-04 05:06
  r= _rec.array([(1,"asds", 24.),(2,"pwdw", 48.)], "1i4,1a4,1f8")
  print r._fields["c1"]
  print r._fields["c1"][1]
  r1= Rec1([(1,"asds", 24.),(2,"pwdw", 48.)], "1i4,1a4,1f8")
  print r1._fields["c1"]
  print r1._fields["c1"][1]
  #
  r1.zz= 99                       #  acceptable
  print r1.c1
  print r1.c1[1]
  try:
    x= r1.ugh
  except:
    print 'ugh not recognized as an attribute'
'''
  The above delivers:
[1 2]
2
[1 2]
2
[1 2]
2
ugh not recognized as an attribute
'''
 






More information about the Numpy-discussion mailing list