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 3 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
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}"
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)}"
3 changes: 2 additions & 1 deletion ndcube/ndcube.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ 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}
Data: Array{self.data.shape} [{self.unit}] '{self.data.dtype}'""")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Data: Array{self.data.shape} [{self.unit}] '{self.data.dtype}'""")
Unit: {self.unit}
Data Type: {self.data.dtype}""")

I suggest his change because the data shape is already captured by the dimensions line, above.


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