subclassing RecordArrays

Is there a simple way to subclass RecordArrays? it seems not. suppose i want to have a class RecArrayD(RecArray) that returns a list of its fields, much like a Dict does: recarrayD.keys() the problem is that the fromXXX functions in records.py know only about the one RecArray. and RecArrayD._copyFrom() seems to be of little help, since the new style RecArray has to be framed out first, so to speak. which leaves me to write a new fromRecArray() function that uses the new class. all to get a list of the fields. is this by design, or am i missing something? yes, i can get the keys like so: recarray._names, but i assume that the '_' in front of '_names' is there for a reason, like maybe don't count on it??? or would the designers be willing to add a keys()-like method in for the next release? les schaffer

A Sunday 03 July 2005 04:39, Les Schaffer va escriure:
suppose i want to have a class RecArrayD(RecArray) that returns a list of its fields, much like a Dict does: recarrayD.keys()
the problem is that the fromXXX functions in records.py know only about the one RecArray. and RecArrayD._copyFrom() seems to be of little help, since the new style RecArray has to be framed out first, so to speak.
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?.
yes, i can get the keys like so: recarray._names, but i assume that the '_' in front of '_names' is there for a reason, like maybe don't count on it???
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. Cheers, --
0,0< Francesc Altet http://www.carabos.com/ V V Cárabos Coop. V. Enjoy Data "-"

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

A Tuesday 05 July 2005 19:35, Les Schaffer va escriure:
there is no way to tell fromXXXX to use my new class. one would need something like:
I see. Yep, you are right. [snip...]
THEN i could use my MyRecArrayWithDict class to my hearts content.
Yes. We encountered similar problems to this that you reported in a subclass of RecArray to support nested columns. We finally ended implementing specific versions of array() and fromarray() factories. Sorry, as I was not the main author of the module, I did not remember that issue. Cheers, --
0,0< Francesc Altet http://www.carabos.com/ V V Cárabos Coop. V. Enjoy Data "-"
participants (2)
-
Francesc Altet
-
Les Schaffer