[Numpy-discussion] numpy version and numpy.asarray behavior issue

Ray S subscriber100@rjs....
Mon Oct 29 14:35:58 CDT 2007


I have 2 PCs with 2 different installs:
ActivePython 2.4.3 Build 12 with numpy version '1.0b1'
and
Enthought 2.4.3 (1.0.0 #69)  with numpy version '0.9.7.2476'

The attached runs Ok on numpy v1.0, but on Enthought's,  print a1[0] 
gives:
IndexError: 0-d arrays can't be indexed.

It seems that the 0.9.7 numpy.asarray is not creating a true array 
from Dummy class in the code below. Enthought only comes with 
0.9.9.2706 (now).
When was the asarray behavior supported, or, is there some other 
issue I'm missing?
I'll use Activestate's distro if needed, but I'd like to keep 
Enthought for that one...



def fromaddress(address, dtype, shape, strides=None):
     """ Create a numpy array from an integer address, a dtype
     or dtype string, a shape tuple, and possibly strides.
     """
     import numpy
     # Make sure our dtype is a dtype, not just "f" or whatever.
     dtype = numpy.dtype(dtype)

     class Dummy(object):
         pass
     d = Dummy()
     d.__array_interface__ = dict(
         data = (address, False),
         typestr = dtype.str,
         descr = dtype.descr,
         shape = shape,
         strides = strides,
         version = 3,
     )
     return numpy.asarray(d)

Thanks,
Ray
-------------- next part --------------
""" nFromAddress.py
"""

def fromaddress(address, dtype, shape, strides=None):
    """ Create a numpy array from an integer address, a dtype 
    or dtype string, a shape tuple, and possibly strides.
    """
    import numpy
    # Make sure our dtype is a dtype, not just "f" or whatever.
    dtype = numpy.dtype(dtype)

    class Dummy(object):
        pass
    d = Dummy()
    d.__array_interface__ = dict(
        data = (address, False),
        typestr = dtype.str,
        descr = dtype.descr,
        shape = shape,
        strides = strides,
        version = 3,
    )
    return numpy.asarray(d)


##Numeric example, with address kludge
import Numeric, numpy, ctypes, string
a0 = Numeric.zeros((10000), Numeric.Int16)
nAddress = int(string.split(repr(a0.__copy__))[-1][:-1], 16)
tmp=(ctypes.c_long*1)(0)
ctypes.memmove(tmp, nAddress+8, 4)
nAddress = tmp[0]
a1 = fromaddress(nAddress, numpy.int16, (10000,))
a0[0] = 5
print a1[0]

## numpy example
a2 = numpy.zeros(10000, numpy.int16)
nAddress = a2.__array_interface__['data'][0]
nType = a2.__array_interface__['typestr']
nShape = a2.__array_interface__['shape']
a3 = fromaddress(nAddress, nType, nShape)
a2[0] = 5
print a3[0]


More information about the Numpy-discussion mailing list