[Numpy-svn] r3223 - in trunk/numpy/core: . src

numpy-svn at scipy.org numpy-svn at scipy.org
Wed Sep 27 18:20:30 CDT 2006


Author: oliphant
Date: 2006-09-27 18:20:24 -0500 (Wed, 27 Sep 2006)
New Revision: 3223

Modified:
   trunk/numpy/core/arrayprint.py
   trunk/numpy/core/src/scalartypes.inc.src
Log:
Fix printing of arrays with records so that nested arrays print as lists instead of using array syntax

Modified: trunk/numpy/core/arrayprint.py
===================================================================
--- trunk/numpy/core/arrayprint.py	2006-09-27 22:46:11 UTC (rev 3222)
+++ trunk/numpy/core/arrayprint.py	2006-09-27 23:20:24 UTC (rev 3223)
@@ -187,6 +187,17 @@
 
     return lst
 
+def _convert_arrays(obj):
+    newtup = []
+    for k in obj:
+        if isinstance(k, _gen.ndarray):
+            k = k.tolist()
+        elif isinstance(k, tuple):
+            k = _convert_arrays(k)
+        newtup.append(k)
+    return tuple(newtup)
+
+
 def array2string(a, max_line_width = None, precision = None,
                  suppress_small = None, separator=' ', prefix="",
                  style=repr):
@@ -196,6 +207,8 @@
         try:
             lst = a._format(x)
         except AttributeError:
+            if isinstance(x, tuple):
+                x = _convert_arrays(x)
             lst = style(x)
     elif reduce(product, a.shape) == 0:
         # treat as a null array if any of shape elements == 0
@@ -212,6 +225,7 @@
     line += word
     return s, line
 
+
 def _formatArray(a, format_function, rank, max_line_len,
                  next_line_prefix, separator, edge_items, summary_insert):
     """formatArray is designed for two modes of operation:
@@ -222,7 +236,10 @@
 
     """
     if rank == 0:
-        return str(a.item())
+        obj = a.item()
+        if isinstance(obj, tuple):
+            obj = _convert_arrays(obj)
+        return str(obj)
 
     if summary_insert and 2*edge_items < len(a):
         leading_items, trailing_items, summary_insert1 = \

Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src	2006-09-27 22:46:11 UTC (rev 3222)
+++ trunk/numpy/core/src/scalartypes.inc.src	2006-09-27 23:20:24 UTC (rev 3223)
@@ -452,27 +452,26 @@
 gentype_str(PyObject *self)
 {
         PyArrayObject *arr;
-        PyObject *ret, *tmp;
+        PyObject *ret;
 
         arr = (PyArrayObject *)PyArray_FromScalar(self, NULL);
         if (arr==NULL) return NULL;
-        ret = PyObject_Str((tmp=arr->descr->f->getitem(arr->data, arr)));
+        ret = PyObject_Str((PyObject *)arr);
         Py_DECREF(arr);
-        Py_XDECREF(tmp);
         return ret;
 }
 
+
 static PyObject *
 gentype_repr(PyObject *self)
 {
         PyArrayObject *arr;
-        PyObject *ret, *tmp ;
+        PyObject *ret;
 
         arr = (PyArrayObject *)PyArray_FromScalar(self, NULL);
         if (arr==NULL) return NULL;
-        ret = PyObject_Repr((tmp=arr->descr->f->getitem(arr->data, arr)));
+        ret = PyObject_Str((PyObject *)arr);
         Py_DECREF(arr);
-        Py_XDECREF(tmp);
         return ret;
 }
 



More information about the Numpy-svn mailing list