Skip to content

Commit

Permalink
Use matrepr for rich __str__ and _repr_html_ (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
alugowski authored Sep 7, 2023
1 parent 096f9b0 commit 44e434d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
6 changes: 4 additions & 2 deletions sparse/_compressed/compressed.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,10 @@ def T(self):
return self.transpose()

def __str__(self):
return "<GCXS: shape={}, dtype={}, nnz={}, fill_value={}, compressed_axes={}>".format(
summary = "<GCXS: shape={}, dtype={}, nnz={}, fill_value={}, compressed_axes={}>".format(
self.shape, self.dtype, self.nnz, self.fill_value, self.compressed_axes
)
return self._str_impl(summary)

__repr__ = __str__

Expand Down Expand Up @@ -864,13 +865,14 @@ def __init__(
)

def __str__(self):
return "<{}: shape={}, dtype={}, nnz={}, fill_value={}>".format(
summary = "<{}: shape={}, dtype={}, nnz={}, fill_value={}>".format(
type(self).__name__,
self.shape,
self.dtype,
self.nnz,
self.fill_value,
)
return self._str_impl(summary)

__repr__ = __str__

Expand Down
3 changes: 2 additions & 1 deletion sparse/_coo/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,9 +702,10 @@ def __sizeof__(self):
__getitem__ = getitem

def __str__(self):
return "<COO: shape={!s}, dtype={!s}, nnz={:d}, fill_value={!s}>".format(
summary = "<COO: shape={!s}, dtype={!s}, nnz={:d}, fill_value={!s}>".format(
self.shape, self.dtype, self.nnz, self.fill_value
)
return self._str_impl(summary)

__repr__ = __str__

Expand Down
3 changes: 2 additions & 1 deletion sparse/_dok.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,10 @@ def _setitem(self, key_list, value):
del self.data[key]

def __str__(self):
return "<DOK: shape={!s}, dtype={!s}, nnz={:d}, fill_value={!s}>".format(
summary = "<DOK: shape={!s}, dtype={!s}, nnz={:d}, fill_value={!s}>".format(
self.shape, self.dtype, self.nnz, self.fill_value
)
return self._str_impl(summary)

__repr__ = __str__

Expand Down
39 changes: 38 additions & 1 deletion sparse/_sparse_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,44 @@ def _repr_html_(self):
Diagnostic report about this array.
Renders in Jupyter.
"""
return html_table(self)
try:
from matrepr import to_html
from matrepr.adapters.sparse_driver import PyDataSparseDriver

return to_html(PyDataSparseDriver.adapt(self), notebook=True)
except ImportError:
return html_table(self)

def _str_impl(self, summary):
"""
A human-readable representation of this array, including a metadata summary
and a tabular view of the array values.
Values view only included if `matrepr` is available.
Parameters
----------
summary
A type-specific summary of this array, used as the first line of return value.
Returns
-------
str
A human-readable representation of this array.
"""
try:
from matrepr import to_str
from matrepr.adapters.sparse_driver import PyDataSparseDriver

values = to_str(
PyDataSparseDriver.adapt(self),
title=False, # disable matrepr description
width_str=0, # autodetect terminal width
max_cols=9999,
)
return "\n".join([summary, values])
except ImportError:
return summary

@abstractmethod
def asformat(self, format):
Expand Down

0 comments on commit 44e434d

Please sign in to comment.