From 18f5436641ef0a288639d81d53144706ebcd7cd3 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sun, 19 Nov 2017 01:11:05 -0800 Subject: MAINT: Extract dtype printing code into helper function --- numpy/core/arrayprint.py | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'numpy/core/arrayprint.py') 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) -- cgit v1.2.1