summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAllan Haldane <ealloc@gmail.com>2017-11-28 20:17:39 +0100
committerGitHub <noreply@github.com>2017-11-28 20:17:39 +0100
commit1493023518f956bec74a9f5113d83510c28f44c7 (patch)
tree7b3714ee2168cd5f719c8640a0ca3204d20b3632
parentaaa46bac652a449e21dedcde41388610be19e75b (diff)
parent7693f9df2dab9dd5f27e9d2bb89c6f80341c6e61 (diff)
downloadnumpy-1493023518f956bec74a9f5113d83510c28f44c7.tar.gz
Merge pull request #10112 from eric-wieser/arrayprint-tidy
MAINT: Simplify IntegerFormatter
-rw-r--r--numpy/core/arrayprint.py24
-rw-r--r--numpy/testing/tests/test_utils.py15
2 files changed, 14 insertions, 25 deletions
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index 460661df7..2aa35224c 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -50,12 +50,6 @@ from .numerictypes import (longlong, intc, int_, float_, complex_, bool_,
flexible)
import warnings
-if sys.version_info[0] >= 3:
- _MAXINT = sys.maxsize
- _MININT = -sys.maxsize - 1
-else:
- _MAXINT = sys.maxint
- _MININT = -sys.maxint - 1
_format_options = {
'edgeitems': 3, # repr N leading and trailing items of each dimension
@@ -988,23 +982,15 @@ def format_float_positional(x, precision=None, unique=True,
class IntegerFormat(object):
def __init__(self, data):
- try:
+ if data.size > 0:
max_str_len = max(len(str(np.max(data))),
len(str(np.min(data))))
- self.format = '%' + str(max_str_len) + 'd'
- except (TypeError, NotImplementedError):
- # if reduce(data) fails, this instance will not be called, just
- # instantiated in formatdict.
- pass
- except ValueError:
- # this occurs when everything is NA
- pass
+ else:
+ max_str_len = 0
+ self.format = '%{}d'.format(max_str_len)
def __call__(self, x):
- if _MININT < x < _MAXINT:
- return self.format % x
- else:
- return "%s" % x
+ return self.format % x
class BoolFormat(object):
diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py
index 8f259cf4a..08d67153a 100644
--- a/numpy/testing/tests/test_utils.py
+++ b/numpy/testing/tests/test_utils.py
@@ -4,6 +4,7 @@ import warnings
import sys
import os
import itertools
+import textwrap
import numpy as np
from numpy.testing import (
@@ -289,17 +290,19 @@ class TestEqual(TestArrayEqual):
except AssertionError as e:
msg = str(e)
msg2 = msg.replace("shapes (2L,), (1L, 2L)", "shapes (2,), (1, 2)")
- msg_reference = "\nArrays are not equal\n\n" \
- "(shapes (2,), (1, 2) mismatch)\n" \
- " x: array([1, 2])\n" \
- " y: [repr failed for <matrix>: The truth value of an array " \
- "with more than one element is ambiguous. Use a.any() or " \
- "a.all()]"
+ msg_reference = textwrap.dedent("""\
+
+ Arrays are not equal
+
+ (shapes (2,), (1, 2) mismatch)
+ x: array([1, 2])
+ y: [repr failed for <matrix>: %d format: a number is required, not matrix]""")
try:
self.assertEqual(msg, msg_reference)
except AssertionError:
self.assertEqual(msg2, msg_reference)
+
class TestArrayAlmostEqual(_GenericTest, unittest.TestCase):
def setUp(self):