diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2017-09-29 00:47:13 -0700 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2017-10-23 21:09:06 -0700 |
commit | e0c60faed08ed5f726a9566466f7c20c08255dee (patch) | |
tree | e49006c527db0bcbae32a506354333c09abde8bd /numpy/ma/core.py | |
parent | 63e2f6d5ee34b759e419e29661a9823a4d8b069e (diff) | |
download | numpy-e0c60faed08ed5f726a9566466f7c20c08255dee.tar.gz |
MAINT: Remove branching from MaskedArray.__repr__, and line-wrap
Diffstat (limited to 'numpy/ma/core.py')
-rw-r--r-- | numpy/ma/core.py | 84 |
1 files changed, 47 insertions, 37 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 5f4d14a73..66dbd7a8f 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -25,6 +25,7 @@ from __future__ import division, absolute_import, print_function import sys import operator import warnings +import textwrap from functools import reduce if sys.version_info[0] >= 3: @@ -2436,32 +2437,35 @@ def _recursive_printoption(result, mask, printopt): np.copyto(result, printopt, where=mask) return -_print_templates = dict(long_std="""\ -masked_%(name)s(data = - %(data)s, - %(nlen)s mask = - %(mask)s, - %(nlen)s fill_value = %(fill)s) -""", - short_std="""\ -masked_%(name)s(data = %(data)s, - %(nlen)s mask = %(mask)s, -%(nlen)s fill_value = %(fill)s) -""", - long_flx="""\ -masked_%(name)s(data = - %(data)s, - %(nlen)s mask = - %(mask)s, -%(nlen)s fill_value = %(fill)s, - %(nlen)s dtype = %(dtype)s) -""", - short_flx="""\ -masked_%(name)s(data = %(data)s, -%(nlen)s mask = %(mask)s, -%(nlen)s fill_value = %(fill)s, -%(nlen)s dtype = %(dtype)s) -""") +# For better or worse, these end in a newline +_print_templates = dict( + long_std=textwrap.dedent("""\ + masked_%(name)s(data = + %(data)s, + %(nlen)s mask = + %(mask)s, + %(nlen)s fill_value = %(fill)s) + """), + long_flx=textwrap.dedent("""\ + masked_%(name)s(data = + %(data)s, + %(nlen)s mask = + %(mask)s, + %(nlen)s fill_value = %(fill)s, + %(nlen)s dtype = %(dtype)s) + """), + short_std=textwrap.dedent("""\ + masked_%(name)s(data = %(data)s, + %(nlen)s mask = %(mask)s, + %(nlen)s fill_value = %(fill)s) + """), + short_flx=textwrap.dedent("""\ + masked_%(name)s(data = %(data)s, + %(nlen)s mask = %(mask)s, + %(nlen)s fill_value = %(fill)s, + %(nlen)s dtype = %(dtype)s) + """) +) ############################################################################### # MaskedArray class # @@ -3865,22 +3869,28 @@ class MaskedArray(ndarray): Literal string representation. """ - n = self.ndim if self._baseclass is np.ndarray: name = 'array' else: name = self._baseclass.__name__ - parameters = dict(name=name, nlen=" " * len(name), - data=str(self), mask=str(self._mask), - fill=str(self.fill_value), dtype=str(self.dtype)) - if self.dtype.names: - if n <= 1: - return _print_templates['short_flx'] % parameters - return _print_templates['long_flx'] % parameters - elif n <= 1: - return _print_templates['short_std'] % parameters - return _print_templates['long_std'] % parameters + is_long = self.ndim > 1 + is_structured = bool(self.dtype.names) + + parameters = dict( + name=name, + nlen=" " * len(name), + data=str(self), + mask=str(self._mask), + fill=str(self.fill_value), + dtype=str(self.dtype) + ) + + key = '{}_{}'.format( + 'long' if is_long else 'short', + 'flx' if is_structured else 'std' + ) + return _print_templates[key] % parameters def _delegate_binop(self, other): # This emulates the logic in |