summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTravis Oliphant <oliphant@enthought.com>2007-05-15 23:23:57 +0000
committerTravis Oliphant <oliphant@enthought.com>2007-05-15 23:23:57 +0000
commitf79ec5f6b1aa74bac292bf9938ddd334bdbc85ab (patch)
tree1baad97ce72cf66fb6eb5fd74fd938b0887c50e5
parent3f08181da74e0cfee070065cd56aee04e8927abf (diff)
downloadnumpy-f79ec5f6b1aa74bac292bf9938ddd334bdbc85ab.tar.gz
Fix problem with records with object elements and add pretty-printing to record objects. Remove the global _multiarray_module_loaded.
-rw-r--r--numpy/core/records.py22
-rw-r--r--numpy/core/src/multiarraymodule.c4
2 files changed, 18 insertions, 8 deletions
diff --git a/numpy/core/records.py b/numpy/core/records.py
index 5fe43a173..38d6410d1 100644
--- a/numpy/core/records.py
+++ b/numpy/core/records.py
@@ -133,11 +133,15 @@ class record(nt.void):
if res:
obj = self.getfield(*res[:2])
# if it has fields return a recarray,
- # if it's a string return 'SU' return a chararray
- # otherwise return a normal array
- if obj.dtype.fields:
+ # if it's a string ('SU') return a chararray
+ # otherwise return the object
+ try:
+ dt = obj.dtype
+ except AttributeError:
+ return obj
+ if dt.fields:
return obj.view(obj.__class__)
- if obj.dtype.char in 'SU':
+ if dt.char in 'SU':
return obj.view(chararray)
return obj
else:
@@ -160,6 +164,16 @@ class record(nt.void):
raise AttributeError, "'record' object has no "\
"attribute '%s'" % attr
+ def pprint(self):
+ # pretty-print all fields
+ names = self.dtype.names
+ maxlen = max([len(name) for name in names])
+ rows = []
+ fmt = '%% %ds: %%s' %maxlen
+ for name in names:
+ rows.append(fmt%(name, getattr(self, name)))
+ return "\n".join(rows)
+
# The recarray is almost identical to a standard array (which supports
# named fields already) The biggest difference is that it can use
# attribute-lookup to find the fields and it is constructed using
diff --git a/numpy/core/src/multiarraymodule.c b/numpy/core/src/multiarraymodule.c
index 2b690bae8..8c3f299ed 100644
--- a/numpy/core/src/multiarraymodule.c
+++ b/numpy/core/src/multiarraymodule.c
@@ -26,8 +26,6 @@
static PyObject *typeDict=NULL; /* Must be explicitly loaded */
static PyObject *_numpy_internal=NULL; /* A Python module for callbacks */
-static int _multiarray_module_loaded=0;
-
static PyArray_Descr *
_arraydescr_fromobj(PyObject *obj)
@@ -7501,8 +7499,6 @@ PyMODINIT_FUNC initmultiarray(void) {
PyObject *m, *d, *s;
PyObject *c_api;
- if (_multiarray_module_loaded) return;
- _multiarray_module_loaded = 1;
/* Create the module and add the functions */
m = Py_InitModule("multiarray", array_module_methods);
if (!m) goto err;