Skip to content

Commit

Permalink
BF: generalize date grid formatters for Py3, uint
Browse files Browse the repository at this point in the history
In Python 3, np.int64 no longer subclasses np.int, so the test for
integers was not working, when choosing formatters.  Unsigned ints
should also use the integer formatter.
  • Loading branch information
matthew-brett committed May 31, 2016
1 parent 98ddad3 commit c8761a9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
17 changes: 11 additions & 6 deletions datarray/print_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,21 @@ def format(self, value, width=None):
result = '{0}{1}j'.format(real_part, imag_part)
return '{0:<{width}}'.format(result, width=width)


# Formatters for numpy dtype kinds
_KIND2FORMAT = dict(b = BoolFormatter,
u = IntFormatter,
i = IntFormatter,
f = FloatFormatter,
c = ComplexFormatter)


def get_formatter(arr):
"""
Get a formatter for this array's data type, and prime it on this array.
"""
typeobj = arr.dtype.type
if issubclass(typeobj, np.bool): return BoolFormatter(arr)
elif issubclass(typeobj, np.int): return IntFormatter(arr)
elif issubclass(typeobj, np.floating): return FloatFormatter(arr)
elif issubclass(typeobj, np.complex): return ComplexFormatter(arr)
else: return StrFormatter(arr)
return _KIND2FORMAT.get(arr.dtype.kind, StrFormatter)(arr)


def grid_layout(arr, width=75, height=10):
"""
Expand Down
11 changes: 8 additions & 3 deletions datarray/tests/test_bugfixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ def test_bug34():
from datarray.print_grid import datarray_to_string
from datetime import date as D
A = DataArray([[1,2],[3,4]], [('row', ('a', D(2010,1,1))),('col', 'cd')])
nt.assert_equal(datarray_to_string(A), """row col
exp_out = """row col
--------- -------------------
c d
a 1 2
2010-01-0 3 4""")

2010-01-0 3 4"""
nt.assert_equal(datarray_to_string(A), exp_out)
# Output for unsigned integers
B = A.astype(np.uint32)
nt.assert_equal(datarray_to_string(B), exp_out)


def test_bug35():
"Bug 35"
txt_array = DataArray(['a','b'], axes=['dummy'])
Expand Down

0 comments on commit c8761a9

Please sign in to comment.