[Numpy-discussion] constructing an array from memory

Robert Kern robert.kern@gmail....
Fri Sep 24 12:30:29 CDT 2010


On Fri, Sep 24, 2010 at 11:25, Mathew Yeates <mat.yeates@gmail.com> wrote:
> I'm trying to do something ... unusual.
>
> gdb support scripting with Python. From within my python script, I can
> get the address of a contiguous area of memory that stores a  fortran
> array. I want to creat a NumPy array using "frombuffer". I see that
> the CPython API supports the creation of a buffer, but, is there an
> easier, more direct, way?

You can also make an object with an .__array_interface__ attribute
describing the data.

[~]
|5> x = np.arange(10)

[~]
|6> x.__array_interface__

{'data': (68583072, False),
 'descr': [('', '<i4')],
 'shape': (10,),
 'strides': None,
 'typestr': '<i4',
 'version': 3}

[~]
|7> np.lib.stride_tricks.DummyArray??
Type:           type
Base Class:     <type 'type'>
String Form:    <class 'numpy.lib.stride_tricks.DummyArray'>
Namespace:      Interactive
File:
/Library/Frameworks/Python.framework/Versions/6.1/lib/python2.6/site-packages/numpy/lib/stride_tricks.py
Source:
class DummyArray(object):
    """ Dummy object that just exists to hang __array_interface__ dictionaries
    and possibly keep alive a reference to a base array.
    """
    def __init__(self, interface, base=None):
        self.__array_interface__ = interface
        self.base = base
Constructor information:
Definition:     np.lib.stride_tricks.DummyArray(self, interface, base=None)


Then np.asarray() will consume that object to make an ndarray that
references the given memory.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list