[Numpy-discussion] Example of power of new data-type descriptors.

Travis Oliphant oliphant.travis at ieee.org
Mon Dec 26 01:01:07 EST 2005

I'd like more people to know about the new power that is in scipy core 
due to the general data-type descriptors that can now be used to define 
numeric arrays.  Towards that effort here is a simple example (be sure 
to use latest SVN -- there were a coupld of minor changes that improve 
usability made recently).  Notice this example does not use a special 
"record" array subclass.  This is just a regular array.  I'm kind of 
intrigued (though not motivated to pursue) the possibility of accessing 
(or defining) databases directly into scipy_core arrays using the record 

# Define a new data-type descriptor
 >>> import scipy

 >>> dtype = scipy.dtypedescr({'names': ['name', 'age', 'weight'], 
'formats': ['S30', 'i2', 'f4']})
 >>> a = scipy.array([('Bill',31,260),('Fred',15,135)], dtype=dtype) 
# the argument to dtypedescr could have also been placed here as the 
argument to dtype

 >>> print a['name']
[Bill Fred]

 >>> print a['age']
[31 15]

 >>> print a['weight']
[ 260.  135.]

 >>> print a[0]
('Bill', 31, 260.0)

 >>> print a[1]
('Fred', 15, 135.0)

It seems to me there are some very interesting possibilities with this 
new ability.  The record array subclass adds an improved scalar type 
(the record) and attribute access to get at the fields:  (e.g.  a.name, 
a.age, and a.weight).    But, if you don't need attribute access you can 
use regular arrays to do a lot of what you might need a record array to 
accomplish for you.  I'd love to see what people come up with using this 
new facility.

The new array PEP for Python basically proposes adding a very simple 
array object (just the basic PyArrayObject * of Numeric with a 
bare-bones type-object table) plus this new data-type descriptor object 
to Python and a very few builtin data-type descriptors (perhaps just 
object initially).   This would basically add the array interface to 
Python directly and allow people to start using it generally.  The PEP 
is slow going because it is not on my priority list right now because it 
is not essential to making scipy_core work well.  But, I would love to 
have more people ruminating on the basic ideas which I think are 

Best wishes for a new year,

-Travis Oliphant

More information about the NumPy-Discussion mailing list