diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2018-10-17 15:47:27 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-17 15:47:27 -0500 |
commit | f85c71a6d16ab64695f07fde23e2c19104d36208 (patch) | |
tree | f195d116cdf133dceca189f541bd4cae8b69e2f6 /numpy/core | |
parent | 4126f6603dff45a53dbc2b8206ea928aaf60ca17 (diff) | |
parent | 823431cb61233693f3ffebf15bf7ec350872638c (diff) | |
download | numpy-f85c71a6d16ab64695f07fde23e2c19104d36208.tar.gz |
Merge pull request #12179 from eric-wieser/numerictypes-helpers
MAINT: Move _kind_to_stem to np.core._dtype, so that it can be used as part of dtype.__repr__
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/_dtype.py | 50 | ||||
-rw-r--r-- | numpy/core/_type_aliases.py | 28 | ||||
-rw-r--r-- | numpy/core/numerictypes.py | 5 |
3 files changed, 42 insertions, 41 deletions
diff --git a/numpy/core/_dtype.py b/numpy/core/_dtype.py index 26c44eaaf..d115e0fa6 100644 --- a/numpy/core/_dtype.py +++ b/numpy/core/_dtype.py @@ -5,9 +5,44 @@ String handling is much easier to do correctly in python. """ from __future__ import division, absolute_import, print_function +import sys + import numpy as np +_kind_to_stem = { + 'u': 'uint', + 'i': 'int', + 'c': 'complex', + 'f': 'float', + 'b': 'bool', + 'V': 'void', + 'O': 'object', + 'M': 'datetime', + 'm': 'timedelta' +} +if sys.version_info[0] >= 3: + _kind_to_stem.update({ + 'S': 'bytes', + 'U': 'str' + }) +else: + _kind_to_stem.update({ + 'S': 'string', + 'U': 'unicode' + }) + + +def _kind_name(dtype): + try: + return _kind_to_stem[dtype.kind] + except KeyError: + raise RuntimeError( + "internal dtype error, unknown kind {!r}" + .format(dtype.kind) + ) + + def __str__(dtype): if dtype.fields is not None: return _struct_str(dtype, include_align=True) @@ -122,20 +157,7 @@ def _scalar_str(dtype, short): # Longer repr, like 'float64' else: - kindstrs = { - 'u': "uint", - 'i': "int", - 'f': "float", - 'c': "complex" - } - try: - kindstr = kindstrs[dtype.kind] - except KeyError: - raise RuntimeError( - "internal dtype repr error, unknown kind {!r}" - .format(dtype.kind) - ) - return "'%s%d'" % (kindstr, 8*dtype.itemsize) + return "'%s%d'" % (_kind_name(dtype), 8*dtype.itemsize) elif dtype.isbuiltin == 2: return dtype.type.__name__ diff --git a/numpy/core/_type_aliases.py b/numpy/core/_type_aliases.py index 8d629aa07..cce6c0425 100644 --- a/numpy/core/_type_aliases.py +++ b/numpy/core/_type_aliases.py @@ -29,6 +29,7 @@ from numpy.compat import unicode from numpy._globals import VisibleDeprecationWarning from numpy.core._string_helpers import english_lower, english_capitalize from numpy.core.multiarray import typeinfo, dtype +from numpy.core._dtype import _kind_name sctypeDict = {} # Contains all leaf-node scalar types with aliases @@ -61,28 +62,6 @@ for k, v in typeinfo.items(): _concrete_types = set(v.type for k, v in _concrete_typeinfo.items()) -_kind_to_stem = { - 'u': 'uint', - 'i': 'int', - 'c': 'complex', - 'f': 'float', - 'b': 'bool', - 'V': 'void', - 'O': 'object', - 'M': 'datetime', - 'm': 'timedelta' -} -if sys.version_info[0] >= 3: - _kind_to_stem.update({ - 'S': 'bytes', - 'U': 'str' - }) -else: - _kind_to_stem.update({ - 'S': 'string', - 'U': 'unicode' - }) - def _bits_of(obj): try: @@ -100,8 +79,9 @@ def _bits_of(obj): def bitname(obj): """Return a bit-width name for a given type object""" bits = _bits_of(obj) - char = dtype(obj).kind - base = _kind_to_stem[char] + dt = dtype(obj) + char = dt.kind + base = _kind_name(dt) if base == 'object': bits = 0 diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 3ff9ceef0..2fb841f7c 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -116,8 +116,8 @@ from ._type_aliases import ( _concrete_types, _concrete_typeinfo, _bits_of, - _kind_to_stem, ) +from ._dtype import _kind_name # we don't export these for import *, but we do want them accessible # as numerictypes.bool, etc. @@ -181,8 +181,7 @@ def maximum_sctype(t): if g is None: return t t = g - bits = _bits_of(t) - base = _kind_to_stem[dtype(t).kind] + base = _kind_name(dtype(t)) if base in sctypes: return sctypes[base][-1] else: |