Skip to content

Commit c43ac72

Browse files
committed
Clean up type checking in rex2txt (Fixes #5884)
The main goal is to remove the reference to np.string0. In the end, it became better to clean up the checking of various types using np.issubdtype.
1 parent ed0b475 commit c43ac72

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

lib/matplotlib/mlab.py

+7-22
Original file line numberDiff line numberDiff line change
@@ -3185,35 +3185,20 @@ def get_type(item, atype=int):
31853185
return atype
31863186

31873187
def get_justify(colname, column, precision):
3188-
ntype = type(column[0])
3188+
ntype = column.dtype
31893189

3190-
if (ntype == np.str or ntype == np.str_ or ntype == np.string0 or
3191-
ntype == np.string_):
3192-
length = max(len(colname), column.itemsize)
3190+
if np.issubdtype(ntype, str) or np.issubdtype(ntype, bytes):
3191+
# The division below handles unicode stored in array, which could
3192+
# have 4 bytes per char
3193+
length = max(len(colname), column.itemsize // column[0].itemsize)
31933194
return 0, length+padding, "%s" # left justify
31943195

3195-
if (ntype == np.int or ntype == np.int16 or ntype == np.int32 or
3196-
ntype == np.int64 or ntype == np.int8 or ntype == np.int_):
3196+
if np.issubdtype(ntype, np.int):
31973197
length = max(len(colname),
31983198
np.max(list(map(len, list(map(str, column))))))
31993199
return 1, length+padding, "%d" # right justify
32003200

3201-
# JDH: my powerbook does not have np.float96 using np 1.3.0
3202-
"""
3203-
In [2]: np.__version__
3204-
Out[2]: '1.3.0.dev5948'
3205-
3206-
In [3]: !uname -a
3207-
Darwin Macintosh-5.local 9.4.0 Darwin Kernel Version 9.4.0: Mon Jun
3208-
9 19:30:53 PDT 2008; root:xnu-1228.5.20~1/RELEASE_I386 i386 i386
3209-
3210-
In [4]: np.float96
3211-
---------------------------------------------------------------------------
3212-
AttributeError Traceback (most recent call la
3213-
"""
3214-
if (ntype == np.float or ntype == np.float32 or ntype == np.float64 or
3215-
(hasattr(np, 'float96') and (ntype == np.float96)) or
3216-
ntype == np.float_):
3201+
if np.issubdtype(ntype, np.float):
32173202
fmt = "%." + str(precision) + "f"
32183203
length = max(
32193204
len(colname),

lib/matplotlib/tests/test_mlab.py

+16
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,22 @@ def test_csv2rec_yearfirst(self):
393393
assert_array_equal(array['a'].tolist(), expected)
394394

395395

396+
class rec2txt_testcase(CleanupTestCase):
397+
def test_csv2txt_basic(self):
398+
# str() calls around field names necessary b/c as of numpy 1.11
399+
# dtype doesn't like unicode names (caused by unicode_literals import)
400+
a = np.array([(1.0, 2, 'foo', 'bing'),
401+
(2.0, 3, 'bar', 'blah')],
402+
dtype=np.dtype([(str('x'), np.float32),
403+
(str('y'), np.int8),
404+
(str('s'), str, 3),
405+
(str('s2'), str, 4)]))
406+
truth = (' x y s s2\n'
407+
' 1.000 2 foo bing \n'
408+
' 2.000 3 bar blah ')
409+
assert_equal(mlab.rec2txt(a), truth)
410+
411+
396412
class window_testcase(CleanupTestCase):
397413
def setUp(self):
398414
np.random.seed(0)

0 commit comments

Comments
 (0)