Skip to content

Commit

Permalink
Merge pull request #453 from dhomeier/str-repr-2.0
Browse files Browse the repository at this point in the history
Extend `__str__` and `__repr__` methods of `BaseTableCoordinate`, `ExtraCoords`, `GlobalCoords`
  • Loading branch information
DanRyanIrish authored Oct 22, 2021
2 parents 06c9bb4 + f7402bd commit b73edc4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog/453.doc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved information and formatting of ``__str__`` methods.
12 changes: 10 additions & 2 deletions ndcube/extra_coords/extra_coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,16 @@ def _cube_array_axes_without_extra_coords(self):
return set(range(len(self._ndcube.dimensions))) - set(self.mapping)

def __str__(self):
elements = [f"{', '.join(table.names)} ({axes}): {table}" for axes, table in self._lookup_tables]
return f"ExtraCoords({', '.join(elements)})"
classname = self.__class__.__name__
elements = [f"{', '.join(table.names)} ({axes}) {table.physical_types}: {table}"
for axes, table in self._lookup_tables]
length = len(classname) + 2 * len(elements) + sum(len(e) for e in elements)
if length > np.get_printoptions()['linewidth']:
joiner = ',\n ' + len(classname) * ' '
else:
joiner = ', '

return f"{classname}({joiner.join(elements)})"

def __repr__(self):
return f"{object.__repr__(self)}\n{self}"
16 changes: 14 additions & 2 deletions ndcube/extra_coords/table_coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ def __and__(self, other):
return MultipleTableCoordinate(self, other)

def __str__(self):
return str(self.table)
header = f"{self.__class__.__name__} {self.names or ''} {self.physical_types or '[None]'}:"
content = str(self.table).lstrip('(').rstrip(',)')
if len(header) + len(content) >= np.get_printoptions()['linewidth']:
return '\n'.join((header, content))
else:
return ' '.join((header, content))

def __repr__(self):
return f"{object.__repr__(self)}\n{self}"
Expand Down Expand Up @@ -477,7 +482,14 @@ def __init__(self, *table_coordinates):
self._dropped_coords = list()

def __str__(self):
return f"MultipleTableCoordinate(tables=[{', '.join([str(t) for t in self._table_coords])}])"
classname = self.__class__.__name__
length = len(classname) + sum(len(str(t)) for t in self._table_coords) + 10
if length > np.get_printoptions()['linewidth']:
joiner = ',\n ' + (len(classname) + 8) * ' '
else:
joiner = ', '

return f"{classname}(tables=[{joiner.join([str(t) for t in self._table_coords])}])"

def __and__(self, other):
if not isinstance(other, BaseTableCoordinate):
Expand Down
7 changes: 5 additions & 2 deletions ndcube/extra_coords/tests/test_lookup_table_coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,14 @@ def test_2d_quantity():
def test_repr_str(lut_1d_time, lut_1d_wave):
assert str(lut_1d_time.table) in str(lut_1d_time)
assert "TimeTableCoordinate" in repr(lut_1d_time)
assert str(lut_1d_wave.table)[1:-2] in str(lut_1d_wave)
assert "QuantityTableCoordinate" in repr(lut_1d_wave)

join = lut_1d_time & lut_1d_wave
assert str(lut_1d_time.table) in str(join)
assert str(lut_1d_wave.table) in str(join)
assert "TimeTableCoordinate" not in repr(join)
assert str(lut_1d_wave.table)[1:-2] in str(join)
assert "TimeTableCoordinate" in repr(join)
assert "QuantityTableCoordinate" in repr(join)
assert "MultipleTableCoordinate" in repr(join)


Expand Down
13 changes: 11 additions & 2 deletions ndcube/global_coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections import OrderedDict, defaultdict
from collections.abc import Mapping

import numpy as np
from astropy.coordinates.sky_coordinate import SkyCoord
from astropy.wcs.wcsapi.high_level_api import default_order
from astropy.wcs.wcsapi.utils import deserialize_class
Expand Down Expand Up @@ -195,8 +196,16 @@ def __len__(self):
return len(self._all_coords)

def __str__(self):
elements = [f"{name}: {repr(coord)}" for name, coord in self.items()]
return f"GlobalCoords({', '.join(elements)})"
classname = self.__class__.__name__
elements = [f"{name} {[ptype]}:\n{repr(coord)}" for (name, coord), ptype in
zip(self.items(), self.physical_types.values())]
length = len(classname) + 2 * len(elements) + sum(len(e) for e in elements)
if length > np.get_printoptions()['linewidth']:
joiner = ',\n ' + len(classname) * ' '
else:
joiner = ', '

return f"{classname}({joiner.join(elements)})"

def __repr__(self):
return f"{object.__repr__(self)}\n{str(self)}"
4 changes: 3 additions & 1 deletion ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,9 @@ def __str__(self):
NDCube
------
Dimensions: {self.dimensions}
Physical Types of Axes: {self.array_axis_physical_types}""")
Physical Types of Axes: {self.array_axis_physical_types}
Unit: {self.unit}
Data Type: {self.data.dtype}""")

def __repr__(self):
return f"{object.__repr__(self)}\n{str(self)}"
Expand Down

0 comments on commit b73edc4

Please sign in to comment.