Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend __str__ and __repr__ methods of BaseTableCoordinate, ExtraCoords, GlobalCoords #453

Merged
merged 5 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -392,8 +392,16 @@ def dropped_world_dimensions(self):
return dict()

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 @@ -667,7 +667,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