[Numpy-discussion] RecArray.tolist() suggestion

Russell E Owen rowen at u.washington.edu
Fri Jul 16 10:55:23 CDT 2004

>A Divendres 16 Juliol 2004 02:21, Colin J. Williams va escriure:
>>  To allow for multi-word column names, assignment could replace a space
>>  by an underscore
>>  and, in retrieval, the reverse could be done - ie. underscore would be
>>  banned for a column name.
>That's not so easy. What about other chars like '/&%@$()' that cannot be
>part of python names? Finding a biunivocal map between them and allowed
>chars would be difficult (if possible at all). Besides, the resulting
>colnames might become a real mess.

Personally, I think the idea of allowing access to fields via 
attributes is fatally flawed. The problems raised (non-obvious 
mapping between field names with special characters and allowable 
attribute names and also the collision with existing instance 
variable and method names) clearly show it would be forced and 

The obvious solution seems to be some combination of the dict 
interface (an ordered dict that keeps its keys in original field 
order) and the list interface.

My personal leaning is:
- Offer most of the dict methods, including __get/setitem__, keys, 
values and all iterators but NOT set_default pop_item or anything 
else that adds or deletes a field.
- Offer the list version of __get/setitem__, as well, but NONE of 
list's methods.
- Make the default iterator iterate over values, not keys (field names),
   i.e have the item act like a list, not a dict when used as an iterator.

In other words, the following all work (where item is one element of 
a numarray.record array):
item[0] = 10  # set value of field 0 to 10
x = item[0:5]  # get value of fields 0 through 4
item[:] = list of replacement values
item["afield"] = 10
"%s(afield)" % item
the methods iterkeys, itervalues, iteritems, keys, values, has_key all work
the method update might work, but it's an error to add new fields

-- Russell

P.S. Folks are welcome to use my ordered dictionary implementation 
RO.Alg.OrderedDictionary, which is part of the RO package 
<http://www.astro.washington.edu/rowen/ROPython.html>. It is fully 
standalone (despite its location in my hierarchy) and is used in 
production code.

More information about the Numpy-discussion mailing list