[Numpy-discussion] subclassing RecordArrays

Les Schaffer schaffer at optonline.net
Tue Jul 5 10:36:49 CDT 2005


Francesc Altet wrote:

>I don't know exactly what do you mean here. If RecArrayD inherits from
>RecArray, then RecArrayD should be a RecArray object (and also a
>RecArrayD, of course!). So I would say that the fromXXX fucntions
>should work just fine for these objects. May you put some example on
>what are you trying to do?.
>  
>

consider the calling sequence:

<<<<<<<<<<<<<<<<<
def array(buffer=None, formats=None, shape=0, names=None, 
byteorder=sys.byteorder, aligned=0):
...
  [code to decide which fromXXXX to use]

def fromrecords(recList, formats=None, names=None, shape=0, 
byteorder=sys.byteorder, aligned=0):

   ...

         _array = fromarrays(arrlist, formats=formats, shape=_shape, 
names=names,
                byteorder=byteorder, aligned=aligned)



...
def fromarrays(arrayList, formats=None, names=None, shape=0, 
byteorder=sys.byteorder, aligned=0):
...
    _array = RecArray(None, formats=formats, shape=_shape, names=names,
                byteorder=byteorder, aligned=aligned)
 >>>>>>>>>>>>>>>>>>>>>


there is no way to tell fromXXXX to use my new class. one would need 
something like:

 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def array(buffer=None, formats=None, shape=0, names=None, 
byteorder=sys.byteorder, aligned=0,rec_class = RecArray ):

def fromrecords(recList, formats=None, names=None, shape=0, 
byteorder=sys.byteorder, aligned=0, rec_class):

         _array = fromarrays(arrlist, formats=formats, shape=_shape, 
names=names,
                byteorder=byteorder, aligned=aligned, rec_class)

def fromarrays(arrayList, formats=None, names=None, shape=0, 
byteorder=sys.byteorder, aligned=0, rec_class):
...
    _array = rec_class(None, formats=formats, shape=_shape, names=names,
                byteorder=byteorder, aligned=aligned)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

THEN i could use my MyRecArrayWithDict class to my hearts content.

all that though to have a RecArray.keys() or .names() or 
.RemindMeWhatWasTheNamesOfTheFields()  ????

>Maybe. However I guess that "_names" would not change for some time so
>it might be safe using it for a while :-P. Nevertheless I agree that
>"_names" attribute should be promoted to "names" because it's a
>first-class attribute for RecArray object IMO.
>


agreed. seems the safest design tho would be to GRAB the "names" entered 
when instantiating the RecArray (names='XXX, YYY, ...') plus whatever 
col_N's are added for un-named columns and keep it in an attribute 
accessible by keys() (or whatever name you like).

les schaffer




More information about the Numpy-discussion mailing list