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

numpy-svn at scipy.org numpy-svn at scipy.org
Fri Nov 3 19:47:40 CST 2006


Author: oliphant
Date: 2006-11-03 19:47:36 -0600 (Fri, 03 Nov 2006)
New Revision: 3428

Modified:
   trunk/numpy/core/_internal.py
   trunk/numpy/core/src/arraymethods.c
Log:
Allow order keyword to sort for sorting record arrays.

Modified: trunk/numpy/core/_internal.py
===================================================================
--- trunk/numpy/core/_internal.py	2006-11-03 21:57:11 UTC (rev 3427)
+++ trunk/numpy/core/_internal.py	2006-11-04 01:47:36 UTC (rev 3428)
@@ -267,3 +267,21 @@
     shape = property(get_shape, None, doc="c-types shape")
     strides = property(get_strides, None, doc="c-types strides")
     _as_parameter_ = property(get_as_parameter, None, doc="_as parameter_")
+
+
+# Given a datatype and an order object
+#  return a new names tuple
+#  with the order indicated
+def _newnames(datatype, order):
+    oldnames = datatype.names
+    nameslist = list(oldnames)
+    if isinstance(order, str):
+        order = [order]
+    if isinstance(order, (list, tuple)):
+        for name in order:
+            try:
+                nameslist.remove(name)
+            except ValueError:
+                raise ValueError, "unknown field name: %s" % (name,)
+        return tuple(list(order) + nameslist)
+    raise ValueError, "unsupported order value: %s" % (order,)

Modified: trunk/numpy/core/src/arraymethods.c
===================================================================
--- trunk/numpy/core/src/arraymethods.c	2006-11-03 21:57:11 UTC (rev 3427)
+++ trunk/numpy/core/src/arraymethods.c	2006-11-04 01:47:36 UTC (rev 3428)
@@ -852,14 +852,38 @@
 	int axis=-1;
 	int val;
 	PyArray_SORTKIND which=PyArray_QUICKSORT;
-	static char *kwlist[] = {"axis", "kind", NULL};
+        PyObject *order=NULL;
+        PyArray_Descr *saved=NULL;
+        PyArray_Descr *newd;
+        static char *kwlist[] = {"axis", "kind", "order", NULL};
+        
+        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&O", kwlist, &axis,
+                                         PyArray_SortkindConverter, &which,
+                                         &order))
+                return NULL;
 
-	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO&", kwlist, &axis,
-					 PyArray_SortkindConverter, &which))
-		return NULL;
+        if (order != NULL) {
+                PyObject *new_name;
+                saved = self->descr;
+                if (saved->names == NULL) {
+                        PyErr_SetString(PyExc_ValueError, "Cannot specify " \
+                                        "order with no fields.");
+                        return NULL;
+                }
+                new_name = PyObject_CallMethod(_numpy_internal, "_newnames",
+                                               "OO", saved, order);
+                if (new_name == NULL) return NULL;
+                newd = PyArray_DescrNew(saved);
+                newd->names = new_name;
+                self->descr = newd;
+        }
 
-	val = PyArray_Sort(self, axis, which);
-	if (val < 0) return NULL;
+        val = PyArray_Sort(self, axis, which);
+        if (order != NULL) {
+                Py_XDECREF(self->descr);
+                self->descr = saved;
+        }
+        if (val < 0) return NULL;
 	Py_INCREF(Py_None);
 	return Py_None;
 }



More information about the Numpy-svn mailing list