From 668950285c407593a368336ff2e737c5da84af7d Mon Sep 17 00:00:00 2001 From: Travis Oliphant Date: Thu, 3 Aug 2006 20:14:09 +0000 Subject: [PATCH] Clean up scalar-types functions a bit. --- numpy/core/__init__.py | 2 +- numpy/core/numeric.py | 2 +- numpy/core/numerictypes.py | 77 +++++++++++++++++++++--------------- numpy/core/src/arrayobject.c | 10 ++--- 4 files changed, 52 insertions(+), 39 deletions(-) diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py index ad699b3eca7d..25ace99e3cba 100644 --- a/numpy/core/__init__.py +++ b/numpy/core/__init__.py @@ -5,7 +5,7 @@ import multiarray import umath import numerictypes as nt -multiarray.set_typeDict(nt.typeDict) +multiarray.set_typeDict(nt.sctypeDict) import _sort from numeric import * from fromnumeric import * diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 166a2d5cc061..0d1b69ae94b0 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -354,7 +354,7 @@ def isscalar(num): if isinstance(num, generic): return True else: - return type(num) in ScalarType + return issctype(type(num)) _lkup = { '0':'0000', diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 0b8675504d7b..ddb87294e83a 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -76,19 +76,19 @@ """ # we add more at the bottom -__all__ = ['typeDict', 'typeNA', 'sctypes', 'ScalarType', 'obj2sctype', +__all__ = ['sctypeDict', 'sctypeNA', 'typeDict', 'typeNA', 'sctypes', 'ScalarType', 'obj2sctype', 'cast', 'nbytes', 'sctype2char', 'maximum_sctype', 'issctype', 'typecodes'] -from multiarray import typeinfo, ndarray, array, empty +from multiarray import typeinfo, ndarray, array, empty, dtype import types as _types # we don't export these for import *, but we do want them accessible # as numerictypes.bool, etc. from __builtin__ import bool, int, long, float, complex, object, unicode, str -typeDict = {} # Contains all leaf-node numeric types with aliases -typeNA = {} # Contails all leaf-node types -> numarray type equivalences +sctypeDict = {} # Contains all leaf-node scalar types with aliases +sctypeNA = {} # Contails all leaf-node types -> numarray type equivalences allTypes = {} # Collect the types we will add to the module here def _evalname(name): @@ -151,9 +151,9 @@ def _add_types(): # define C-name and insert typenum and typechar references also allTypes[name] = typeobj - typeDict[name] = typeobj - typeDict[typeinfo[a][0]] = typeobj - typeDict[typeinfo[a][1]] = typeobj + sctypeDict[name] = typeobj + sctypeDict[typeinfo[a][0]] = typeobj + sctypeDict[typeinfo[a][1]] = typeobj else: # generic class allTypes[name] = typeinfo[a] @@ -173,21 +173,21 @@ def _add_aliases(): if (name != 'longdouble' and name != 'clongdouble') or \ myname not in allTypes.keys(): allTypes[myname] = typeobj - typeDict[myname] = typeobj + sctypeDict[myname] = typeobj if base == 'complex': na_name = '%s%d' % (base.capitalize(), bit/2) elif base == 'bool': na_name = base.capitalize() - typeDict[na_name] = typeobj + sctypeDict[na_name] = typeobj else: na_name = "%s%d" % (base.capitalize(), bit) - typeDict[na_name] = typeobj - typeNA[na_name] = typeobj - typeNA[typeobj] = na_name - typeNA[typeinfo[a][0]] = na_name + sctypeDict[na_name] = typeobj + sctypeNA[na_name] = typeobj + sctypeNA[typeobj] = na_name + sctypeNA[typeinfo[a][0]] = na_name if char != '': - typeDict[char] = typeobj - typeNA[char] = na_name + sctypeDict[char] = typeobj + sctypeNA[char] = na_name _add_aliases() # Integers handled so that @@ -214,16 +214,16 @@ def _add_integer_aliases(): uintname = 'uint%d' % bits allTypes[intname] = typeobj allTypes[uintname] = utypeobj - typeDict[intname] = typeobj - typeDict[uintname] = utypeobj - typeDict[Intname] = typeobj - typeDict[UIntname] = utypeobj - typeNA[Intname] = typeobj - typeNA[UIntname] = utypeobj - typeNA[typeobj] = Intname - typeNA[utypeobj] = UIntname - typeNA[val[0]] = Intname - typeNA[uval[0]] = UIntname + sctypeDict[intname] = typeobj + sctypeDict[uintname] = utypeobj + sctypeDict[Intname] = typeobj + sctypeDict[UIntname] = utypeobj + sctypeNA[Intname] = typeobj + sctypeNA[UIntname] = utypeobj + sctypeNA[typeobj] = Intname + sctypeNA[utypeobj] = UIntname + sctypeNA[val[0]] = Intname + sctypeNA[uval[0]] = UIntname _add_integer_aliases() # We use these later @@ -255,13 +255,13 @@ def _set_up_aliases(): ('object_', 'object')] for alias, t in type_pairs: allTypes[alias] = allTypes[t] - typeDict[alias] = typeDict[t] + sctypeDict[alias] = sctypeDict[t] # Remove aliases overriding python types and modules for t in ['ulong', 'object', 'unicode', 'int', 'long', 'float', 'complex', 'bool', 'string']: try: del allTypes[t] - del typeDict[t] + del sctypeDict[t] except KeyError: pass _set_up_aliases() @@ -342,10 +342,14 @@ def _python_type(t): def issctype(rep): """Determines whether the given object represents a numeric array type.""" + if not isinstance(rep, (type, dtype)): + return False try: - char = sctype2char(rep) - return True - except (KeyError, ValueError, TypeError): + res = obj2sctype(rep) + if res: + return True + return False + except: return False def obj2sctype(rep, default=None): @@ -354,11 +358,13 @@ def obj2sctype(rep, default=None): return rep except TypeError: pass + if isinstance(rep, dtype): + return rep.type if isinstance(rep, type): return _python_type(rep) if isinstance(rep, ndarray): return rep.dtype.type - res = typeDict.get(rep, default) + res = sctypeDict.get(rep, default) return res @@ -417,6 +423,11 @@ def sctype2char(sctype): else: _typestr[key] = empty((1,),key).dtype.str[1:] +# Make sure all typestrings are in sctypeDict +for key, val in _typestr.items(): + if val not in sctypeDict: + sctypeDict[val] = key + # Now add the types we've determined to this module for key in allTypes: globals()[key] = allTypes[key] @@ -432,3 +443,7 @@ def sctype2char(sctype): 'AllInteger':'bBhHiIlLqQpP', 'AllFloat':'fdgFDG', 'All':'?bhilqpBHILQPfdgFDGSUVO'} + +# backwards compatibility --- deprecated name +typeDict = sctypeDict +typeNA = sctypeNA diff --git a/numpy/core/src/arrayobject.c b/numpy/core/src/arrayobject.c index 37e0ed569ca0..f17c2098d6fd 100644 --- a/numpy/core/src/arrayobject.c +++ b/numpy/core/src/arrayobject.c @@ -10877,14 +10877,13 @@ arraydescr_richcompare(PyArray_Descr *self, PyObject *other, int cmp_op) } switch (cmp_op) { case Py_LT: - if (PyArray_CanCastTo(self, new)) + if (!PyArray_EquivTypes(self, new) && PyArray_CanCastTo(self, new)) result = Py_True; else result = Py_False; break; case Py_LE: - if (PyArray_EquivTypes(self, new) || - PyArray_CanCastTo(self, new)) + if (PyArray_CanCastTo(self, new)) result = Py_True; else result = Py_False; @@ -10902,14 +10901,13 @@ arraydescr_richcompare(PyArray_Descr *self, PyObject *other, int cmp_op) result = Py_True; break; case Py_GT: - if (PyArray_CanCastTo(new, self)) + if (!PyArray_EquivTypes(self, new) && PyArray_CanCastTo(new, self)) result = Py_True; else result = Py_False; break; case Py_GE: - if (PyArray_EquivTypes(self, new) || - PyArray_CanCastTo(new, self)) + if (PyArray_CanCastTo(new, self)) result = Py_True; else result = Py_False;