diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-11-19 01:11:05 -0800 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-11-20 00:50:55 -0800 |
commit | 18f5436641ef0a288639d81d53144706ebcd7cd3 (patch) | |
tree | 7c62faf84e54047521fb987e2ca2491693eda313 /numpy/core/arrayprint.py | |
parent | dc0b8bdf5aaf03a9698182a7db6deffd830a81e0 (diff) | |
download | numpy-18f5436641ef0a288639d81d53144706ebcd7cd3.tar.gz |
MAINT: Extract dtype printing code into helper function
Diffstat (limited to 'numpy/core/arrayprint.py')
-rw-r--r-- | numpy/core/arrayprint.py | 37 |
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) |