[Numpy-discussion] Padding policy in CharArrays

Todd Miller jmiller at stsci.edu
Mon Jan 3 08:41:27 CST 2005


On Sat, 2005-01-01 at 16:44, Francesc Altet wrote:
> Hi,
> 
> I'm experiencing some problems derived from the fact that CharArrays in
> numarray are padded with spaces. That leads to somewhat curious consequences
> like this:
> 
> In [180]: a=strings.array(None, itemsize = 4, shape=1)    
> In [181]: a[0] = '0'
> In [182]: a >= '0\x00\x00\x00\x01'
> Out[182]: array([1], type=Bool)  # Incorrect
> 
> but...
> 
> In [183]: a[0] >= '0\x00\x00\x00\x01'
> Out[183]: False  # correct
> 
> While this is not a bug (see the padding policy for chararrays) I think it
> would be much better to use '\0x00' as default padding. Would be any problem
> with that?. 

The design intent of numarray.strings was that you could use
RawCharArray, the baseclass of CharArray, for NULL padded arrays.  I
tried it out like this:

>>> a=strings.array(None, itemsize = 4, shape=1, kind=strings.RawCharArray)
>>> a[0] = '0\0\0\0'
>>> print repr(a >= '0\x00\x00\x00\x01')
array([0], type=Bool)  

You'll note that I "hand padded" the assigned value;   because
RawCharArray is a little used feature,  it needs more work.  I think
RawCharArray either makes partial/inconsistent use of NULL padding.

> If yes, well, I've found a workaround on this, but quite
> inelegant I'm afraid :-/

Give RawCharArray a try;  it *is* the basis of CharArray, so it
basically works but there will likely be a few issues to sort out.  My
guess is that anything that really needs fixing can be added for
numarray-1.2.

Regards,
Todd






More information about the Numpy-discussion mailing list