[Numpy-svn] r2845 - in trunk/numpy/core: . src tests
numpy-svn at scipy.org
numpy-svn at scipy.org
Wed Jul 19 11:24:52 EDT 2006
Author: oliphant
Date: 2006-07-19 10:24:47 -0500 (Wed, 19 Jul 2006)
New Revision: 2845
Modified:
trunk/numpy/core/records.py
trunk/numpy/core/src/arrayobject.c
trunk/numpy/core/tests/test_records.py
Log:
Fix up rec.array when dtype and formats are both None for cases that support it. Alter dtype=object parsing so that tuples and lists are recognized as sequences.
Modified: trunk/numpy/core/records.py
===================================================================
--- trunk/numpy/core/records.py 2006-07-19 07:08:41 UTC (rev 2844)
+++ trunk/numpy/core/records.py 2006-07-19 15:24:47 UTC (rev 2845)
@@ -460,35 +460,44 @@
raise ValueError("Must define formats (or dtype) if object is "\
"None, string, or an open file")
+ kwds = {}
if dtype is not None:
dtype = sb.dtype(dtype)
- else:
+ elif formats is not None:
dtype = format_parser(formats, names, titles,
aligned, byteorder)._descr
+ else:
+ kwds = {'formats': formats,
+ 'names' : names,
+ 'titles' : titles,
+ 'aligned' : aligned,
+ 'byteorder' : byteorder
+ }
if obj is None:
if shape is None:
raise ValueError("Must define a shape if obj is None")
return recarray(shape, dtype, buf=obj, offset=offset, strides=strides)
elif isinstance(obj, str):
- return fromstring(obj, dtype, shape=shape, offset=offset)
+ return fromstring(obj, dtype, shape=shape, offset=offset, **kwds)
elif isinstance(obj, (list, tuple)):
if isinstance(obj[0], sb.ndarray):
- return fromarrays(obj, dtype=dtype, shape=shape)
+ return fromarrays(obj, dtype=dtype, shape=shape, **kwds)
else:
- return fromrecords(obj, dtype=dtype, shape=shape)
+ return fromrecords(obj, dtype=dtype, shape=shape, **kwds)
elif isinstance(obj, recarray):
new = obj.copy()
- new.dtype = dtype
+ if dtype is not None:
+ new.dtype = dtype
return new
elif isinstance(obj, file):
return fromfile(obj, dtype=dtype, shape=shape, offset=offset)
elif isinstance(obj, sb.ndarray):
- if (obj.dtype != dtype):
+ if dtype is not None and (obj.dtype != dtype):
obj.dtype = dtype
res = obj.view(recarray)
if issubclass(res.dtype.type, nt.void):
Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c 2006-07-19 07:08:41 UTC (rev 2844)
+++ trunk/numpy/core/src/arrayobject.c 2006-07-19 15:24:47 UTC (rev 2845)
@@ -6913,10 +6913,13 @@
{
intp *newdims, *test_dims;
int nd, test_nd;
- int i;
+ int i, islist;
intp size;
+ PyObject *obj;
- if (!PyList_Check(s) || ((size=PyList_GET_SIZE(s)) == 0))
+ islist = PyList_Check(s);
+ if (!(islist || PyTuple_Check(s)) ||
+ ((size = PySequence_Size(s)) == 0))
return 0;
if (max < 2) {
if (max < 1) return 0;
@@ -6925,11 +6928,13 @@
}
newdims = PyDimMem_NEW(2*(max-1));
test_dims = newdims + (max-1);
- nd = object_depth_and_dimension(PyList_GET_ITEM(s, 0),
- max-1, newdims);
+ if (islist) obj = PyList_GET_ITEM(s, 0);
+ else obj = PyTuple_GET_ITEM(s, 0);
+ nd = object_depth_and_dimension(obj, max-1, newdims);
for (i=1; i<size; i++) {
- test_nd = object_depth_and_dimension(PyList_GET_ITEM(s, i),
- max-1, test_dims);
+ if (islist) obj = PyList_GET_ITEM(s, 0);
+ else obj = PyTuple_GET_ITEM(s, 0);
+ test_nd = object_depth_and_dimension(obj, max-1, test_dims);
if ((nd != test_nd) ||
(!PyArray_CompareLists(newdims, test_dims, nd))) {
nd = 0;
Modified: trunk/numpy/core/tests/test_records.py
===================================================================
--- trunk/numpy/core/tests/test_records.py 2006-07-19 07:08:41 UTC (rev 2844)
+++ trunk/numpy/core/tests/test_records.py 2006-07-19 15:24:47 UTC (rev 2845)
@@ -54,5 +54,21 @@
assert(mine.data1[i]==0.0)
assert(mine.data2[i]==0.0)
+ def check_recarray_from_names(self):
+ ra = rec.array([
+ (1, 'abc', 3.7000002861022949, 0),
+ (2, 'xy', 6.6999998092651367, 1),
+ (0, ' ', 0.40000000596046448, 0)],
+ names='c1, c2, c3, c4')
+ pa = rec.fromrecords([
+ (1, 'abc', 3.7000002861022949, 0),
+ (2, 'xy', 6.6999998092651367, 1),
+ (0, ' ', 0.40000000596046448, 0)],
+ names='c1, c2, c3, c4')
+ assert ra.dtype == pa.dtype
+ assert ra.shape == pa.shape
+ for k in xrange(len(ra)):
+ assert ra[k].item() == pa[k].item()
+
if __name__ == "__main__":
NumpyTest().run()
More information about the Numpy-svn
mailing list