summaryrefslogtreecommitdiff
path: root/numpy/core/arrayprint.py
diff options
context:
space:
mode:
authorAllan Haldane <ealloc@gmail.com>2017-11-20 17:16:53 +0100
committerGitHub <noreply@github.com>2017-11-20 17:16:53 +0100
commitf0557b8f077d0d7d7d2f9b12054c59f118a207ef (patch)
tree44411e186f6fe0f3000dafcf58a39ff3441fdbfd /numpy/core/arrayprint.py
parent3d5d12f186c7fe375d0b21f72b28a3bc87d25cc0 (diff)
parent8bac6eed123c68560ffdb5d7f7093ad4015a9d85 (diff)
downloadnumpy-f0557b8f077d0d7d7d2f9b12054c59f118a207ef.tar.gz
Merge pull request #9792 from eric-wieser/maskedarray-repr
ENH: Various improvements to Maskedarray repr
Diffstat (limited to 'numpy/core/arrayprint.py')
-rw-r--r--numpy/core/arrayprint.py37
1 files changed, 25 insertions, 12 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index 95c770972..d57d76368 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -1182,6 +1182,30 @@ def dtype_is_implied(dtype):
return dtype.type in _typelessdata
+def dtype_short_repr(dtype):
+ """
+ Convert a dtype to a short form which evaluates to the same dtype.
+
+ The intent is roughly that the following holds
+
+ >>> from numpy import *
+ >>> assert eval(dtype_short_repr(dt)) == dt
+ """
+ # handle these separately so they don't give garbage like str256
+ if issubclass(dtype.type, flexible):
+ if dtype.names:
+ return "%s" % str(dtype)
+ else:
+ return "'%s'" % str(dtype)
+
+ typename = dtype.name
+ # quote typenames which can't be represented as python variable names
+ if typename and not (typename[0].isalpha() and typename.isalnum()):
+ typename = repr(typename)
+
+ return typename
+
+
def array_repr(arr, max_line_width=None, precision=None, suppress_small=None):
"""
Return the string representation of an array.
@@ -1245,18 +1269,7 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None):
if skipdtype:
return "%s(%s)" % (class_name, lst)
-
- # determine typename
- if issubclass(arr.dtype.type, flexible):
- if arr.dtype.names:
- typename = "%s" % str(arr.dtype)
- else:
- typename = "'%s'" % str(arr.dtype)
- else:
- typename = arr.dtype.name
- # quote typenames which can't be represented as python variable names
- if typename and not (typename[0].isalpha() and typename.isalnum()):
- typename = "'%s'" % typename
+ typename = dtype_short_repr(arr.dtype)
prefix = "{}({},".format(class_name, lst)
suffix = "dtype={})".format(typename)