[Numpy-discussion] PyArray_DescrConverter - alignment / trailing unused bytes

Martin Wiechert martin.wiechert at gmx.de
Fri Sep 15 09:56:31 CDT 2006


On Friday 15 September 2006 16:40, Francesc Altet wrote:
> A Divendres 15 Setembre 2006 16:13, Martin Wiechert va escriure:
> > Hi list,
> >
> > I'm using PyArray_DescrConverter with a dict object to create a
> > "struct-like" dtype from C.
> > As the struct contains different data types I run into "unaligned access"
> > problems.
> > Is there a way to force alignment or to get trailing unused bytes in the
> > dtpye?
>
> One possible solution is to declare void ('V' charcode) types for filling
> the gaps. For example:
>
>
> In [118]: ra=numpy.rec.array("1"*300,
> dtype=[('sval','<a4'),('unused','|V4'), ('dval','<f8')], shape=2)
>
> In [119]: ra['sval']
> Out[119]:
> recarray([1111, 1111],
>       dtype='|S4')
>
> In [120]: ra['dval']
> Out[120]: array([  9.73041595e-72,   9.73041595e-72])
>

Hm, not exactly elegant but should work. Thanks Francesc!

Note that all but trailing gaps can be controlled using a dictionary 
descriptor with an offsets key:

>>> ra = zeros ((10,), dtype = {'names': ['sval', 'dval'], 'formats': ['<a4', 
'<f8'], 'offsets': [0, 8]})
>>> ra.dtype
dtype([('sval', '|S4'), ('', '|V4'), ('dval', '<f8')])

Still if anybody knows a clean solution I would be very interested.

> You can still access the empty spaces if you want (although it is
> nonsense):
>
> In [121]: ra['unused']
> Out[121]:
> recarray([1111, 1111],
>       dtype='|V4')
>
> Cheers,




More information about the Numpy-discussion mailing list