Skip to content

Commit a900297

Browse files
committed
More cleanup suggested by @mhvk [skip ci]
1 parent 6db14fc commit a900297

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

astropy/table/info.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def info(self, option='attributes', out=''):
2020
to include. This can be a string, a function, or a list of
2121
strings or functions. Built-in options are:
2222
23-
- ``meta``: basic column meta data like ``dtype`` or ``format``
23+
- ``attributes``: basic column meta data like ``dtype`` or ``format``
2424
- ``stats``: basic statistics: minimum, mean, and maximum
2525
2626
If a function is specified then that function will be called with the
@@ -73,11 +73,15 @@ def info(self, option='attributes', out=''):
7373

7474
outlines = ['<' + ' '.join(descr_vals) + '>']
7575

76-
infos = []
77-
for col in self.columns.values():
78-
infos.append(col.info(option, out=None))
76+
cols = self.columns.values()
77+
if self.colnames:
78+
infos = []
79+
for col in cols:
80+
infos.append(col.info(option, out=None))
7981

80-
info = Table(infos, names=list(infos[0]))
82+
info = Table(infos, names=list(infos[0]))
83+
else:
84+
info = Table()
8185

8286
if out is None:
8387
return info
@@ -88,12 +92,27 @@ def info(self, option='attributes', out=''):
8892
if np.all(info[name] == ''):
8993
del info[name]
9094

91-
if np.all(info['n_bad'] == 0):
95+
if 'class' in info.colnames:
96+
# Remove 'class' info column if all table columns are the same class
97+
# and they are not mixin columns.
98+
uniq_types = set(type(col) for col in cols)
99+
if len(uniq_types) == 1 and not self._is_mixin_column(cols[0]):
100+
del info['class']
101+
102+
if 'class' in info.colnames:
103+
if np.all(info['class'] == 'Column') or np.all(info['class'] == 'MaskedColumn'):
104+
del info['class']
105+
106+
if 'n_bad' in info.colnames and np.all(info['n_bad'] == 0):
92107
del info['n_bad']
93108

94109
# Standard attributes has 'length' but this is typically redundant
95110
if 'length' in info.colnames and np.all(info['length'] == len(self)):
96111
del info['length']
97112

98-
outlines.extend(info.pformat(max_width=-1, max_lines=-1, show_unit=False))
113+
if self.colnames:
114+
outlines.extend(info.pformat(max_width=-1, max_lines=-1, show_unit=False))
115+
else:
116+
outlines.append('<No columns>')
117+
99118
out.writelines(outline + os.linesep for outline in outlines)

astropy/table/tests/test_info.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_table_info_attributes(table_types):
5151
assert np.all(tinfo['unit'] == ['', '', '', 'm', '', 'deg,deg'])
5252
assert np.all(tinfo['format'] == ['%02d', '', '', '', '', ''])
5353
assert np.all(tinfo['description'] == ['', '', '', 'quantity', 'time', 'skycoord'])
54-
cls = 'MyColumn' if subcls else ''
54+
cls = t.ColumnClass.__name__
5555
assert np.all(tinfo['class'] == [cls, cls, cls, cls, 'Time', 'SkyCoord'])
5656

5757
def test_table_info_stats(table_types):
@@ -128,7 +128,7 @@ def test_data_info():
128128
('unit', 'm / s'),
129129
('format', ''),
130130
('description', 'description'),
131-
('class', ''),
131+
('class', type(c).__name__),
132132
('n_bad', 1),
133133
('length', 3)])
134134

@@ -139,6 +139,7 @@ def test_data_info():
139139
'dtype = float64',
140140
'unit = m / s',
141141
'description = description',
142+
'class = {0}'.format(type(c).__name__),
142143
'n_bad = 1',
143144
'length = 3']
144145
assert out.getvalue().splitlines() == exp
@@ -163,16 +164,17 @@ class Column(table.Column):
163164
Confusingly named Column on purpose, but that is legal.
164165
"""
165166
pass
166-
c = Column([1, 2], dtype='int64')
167-
cinfo = c.info(out=None)
168-
assert cinfo == OrderedDict([('dtype', 'int64'),
169-
('shape', ''),
170-
('unit', ''),
171-
('format', ''),
172-
('description', ''),
173-
('class', 'Column'),
174-
('n_bad', 0),
175-
('length', 2)])
167+
for data in ([], [1, 2]):
168+
c = Column(data, dtype='int64')
169+
cinfo = c.info(out=None)
170+
assert cinfo == OrderedDict([('dtype', 'int64'),
171+
('shape', ''),
172+
('unit', ''),
173+
('format', ''),
174+
('description', ''),
175+
('class', 'Column'),
176+
('n_bad', 0),
177+
('length', len(data))])
176178

177179

178180
def test_scalar_info():
@@ -183,3 +185,11 @@ def test_scalar_info():
183185
cinfo = c.info(out=None)
184186
assert cinfo['n_bad'] == 0
185187
assert 'length' not in cinfo
188+
189+
190+
def test_empty_table():
191+
t = table.Table()
192+
out = six.moves.cStringIO()
193+
t.info(out=out)
194+
exp = ['<Table length=0>', '<No columns>']
195+
assert out.getvalue().splitlines() == exp

astropy/utils/data_info.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ def _get_column_attribute(col, attr=None):
6868
"""
6969
Get a column attribute for the ``attributes`` info summary method
7070
"""
71-
from ..table.column import Column, MaskedColumn
72-
7371
if attr == 'class':
74-
val = '' if type(col) in (Column, MaskedColumn) else type(col).__name__
72+
val = type(col).__name__
7573
elif attr == 'dtype':
7674
val = col.info.dtype.name
7775
elif attr == 'shape':

0 commit comments

Comments
 (0)