array repr

Andrew MacKeith Andrew.MacKeith at ABAQUS.com
Tue Oct 17 13:15:24 CDT 2006


Travis Oliphant wrote:

>Andrew MacKeith wrote:
>
>  
>
>>I would like to use the built-in array_repr in numpy, because
>>I need a fast repr that does not contain new line characters.
>>I see no way of doing this without editing the code in numeric.py,
>>and I hate to edit other people's libraries.
>>
>>    
>>
>>from numpy import array
>  
>
>>causes numeric.py to be executed, which includes a call to
>>multiarray.set_string_function(array_repr, 1)
>>
>>If I want to "undo" this, there is no way of doing it.
>> 
>>
>>    
>>
>Why do want to "undo" this? 
>
>  
>
>>I would like to unset the repr with:
>>numpy.set_string_function(None, 1)
>> 
>>
>>    
>>
>I don't understand what you are trying to do.  Why don't you just set 
>the string function to an actual callable.  
>
Since arrayobject.c contains a fast repr function, I would like to use 
that. Why reinvent the wheel.

>What edits do you propose to 
>make?
>  
>
If you mean how would I propose to change the code, I would have
array_set_string_function recognise None as an acceptable argument
and PyArray_SetStringFunction would recognise this to set
PyArray_ReprFunction = array_repr;

These is the (untested) code.

multiarraymodule.c:
static PyObject *
array_set_string_function(PyObject *dummy, PyObject *args, PyObject *kwds)
{
    PyObject *op;
    int repr=1;
    static char *kwlist[] = {"f", "repr", NULL};

    if(!PyArg_ParseTupleAndKeywords(args, kwds, "O|i", kwlist,
                    &op, &repr)) return NULL;
    if (!PyCallable_Check(op) and op != Py_None) {
        PyErr_SetString(PyExc_TypeError,
                "Argument must be callable.");
        return NULL;
    }
    PyArray_SetStringFunction(op, repr);
    Py_INCREF(Py_None);
    return Py_None;
}

arrayobject.c:
static void
PyArray_SetStringFunction(PyObject *op, int repr)
{
        if (repr) {
                /* Dispose of previous callback */
                Py_XDECREF(PyArray_ReprFunction);
                if (op == Py_None)
                {
                    PyArray_ReprFunction = array_repr;
                }
                else
                {
                    /* Add a reference to new callback */
                    Py_XINCREF(op);
                    /* Remember new callback */
                    PyArray_ReprFunction = op;
                }
        } else {
                /* Dispose of previous callback */
                Py_XDECREF(PyArray_StrFunction);
                if (op == Py_None)
                {
                    PyArray_StrFunction = array_str;
                }
                else
                {
                    /* Add a reference to new callback */
                    Py_XINCREF(op);
                    /* Remember new callback */
                    PyArray_StrFunction = op;
                }
        }
}

>-Travis
>
>
>
>-------------------------------------------------------------------------
>Using Tomcat but need to do more? Need to support web services, security?
>Get stuff done quickly with pre-integrated technology to make your job easier
>Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
>http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
>_______________________________________________
>Numpy-discussion mailing list
>Numpy-discussion at lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>
>  
>

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list