[pypy-commit] pypy default: Add issubdtype
rguillebert
noreply at buildbot.pypy.org
Tue Oct 8 17:07:14 CEST 2013
Author: Romain Guillebert <romain.py at gmail.com>
Branch:
Changeset: r67218:8f6cfebce395
Date: 2013-10-08 17:06 +0200
http://bitbucket.org/pypy/pypy/changeset/8f6cfebce395/
Log: Add issubdtype
diff --git a/lib_pypy/numpypy/core/numerictypes.py b/lib_pypy/numpypy/core/numerictypes.py
--- a/lib_pypy/numpypy/core/numerictypes.py
+++ b/lib_pypy/numpypy/core/numerictypes.py
@@ -1,1 +1,75 @@
from _numpypy.numerictypes import *
+import numpypy
+
+def issubclass_(arg1, arg2):
+ """
+ Determine if a class is a subclass of a second class.
+
+ `issubclass_` is equivalent to the Python built-in ``issubclass``,
+ except that it returns False instead of raising a TypeError is one
+ of the arguments is not a class.
+
+ Parameters
+ ----------
+ arg1 : class
+ Input class. True is returned if `arg1` is a subclass of `arg2`.
+ arg2 : class or tuple of classes.
+ Input class. If a tuple of classes, True is returned if `arg1` is a
+ subclass of any of the tuple elements.
+
+ Returns
+ -------
+ out : bool
+ Whether `arg1` is a subclass of `arg2` or not.
+
+ See Also
+ --------
+ issubsctype, issubdtype, issctype
+
+ Examples
+ --------
+ >>> np.issubclass_(np.int32, np.int)
+ True
+ >>> np.issubclass_(np.int32, np.float)
+ False
+
+ """
+ try:
+ return issubclass(arg1, arg2)
+ except TypeError:
+ return False
+
+def issubdtype(arg1, arg2):
+ """
+ Returns True if first argument is a typecode lower/equal in type hierarchy.
+
+ Parameters
+ ----------
+ arg1, arg2 : dtype_like
+ dtype or string representing a typecode.
+
+ Returns
+ -------
+ out : bool
+
+ See Also
+ --------
+ issubsctype, issubclass_
+ numpy.core.numerictypes : Overview of numpy type hierarchy.
+
+ Examples
+ --------
+ >>> np.issubdtype('S1', str)
+ True
+ >>> np.issubdtype(np.float64, np.float32)
+ False
+
+ """
+ if issubclass_(arg2, generic):
+ return issubclass(numpypy.dtype(arg1).type, arg2)
+ mro = numpypy.dtype(arg2).type.mro()
+ if len(mro) > 1:
+ val = mro[1]
+ else:
+ val = mro[0]
+ return issubclass(numpypy.dtype(arg1).type, val)
More information about the pypy-commit
mailing list