Skip to content

Commit

Permalink
_Dfsu repr
Browse files Browse the repository at this point in the history
  • Loading branch information
ecomodeller committed Mar 12, 2024
1 parent 99cedf7 commit 2e7f6b5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
31 changes: 13 additions & 18 deletions mikeio/dfsu/_dfsu.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,7 @@ def __init__(self, filename: str | Path) -> None:
self._items = self._read_items(filename)

def __repr__(self):
out = []
type_name = "Flexible Mesh" if self._type is None else self.type_name
out.append(type_name)
out = [f"<mikeio.{self.__class__.__name__}>"]

if self._type is not DfsuFileType.DfsuSpectral0D:
if self._type is not DfsuFileType.DfsuSpectral1D:
Expand All @@ -161,21 +159,18 @@ def __repr__(self):
or self._type == DfsuFileType.Dfsu3DSigmaZ
):
out.append(f"max number of z layers: {self.n_layers - self.n_sigma_layers}")
if hasattr(self, "items") and self.items is not None:
if self.n_items < 10:
out.append("items:")
for i, item in enumerate(self.items):
out.append(f" {i}: {item}")
else:
out.append(f"number of items: {self.n_items}")
if self.n_timesteps is not None:
if self.n_timesteps == 1:
out.append(f"time: time-invariant file (1 step) at {self._start_time}")
else:
out.append(
f"time: {str(self.time[0])} - {str(self.time[-1])} ({self.n_timesteps} records)"
)
out.append(f" {self.start_time} -- {self.end_time}")
if self.n_items < 10:
out.append("items:")
for i, item in enumerate(self.items):
out.append(f" {i}: {item}")
else:
out.append(f"number of items: {self.n_items}")
if self.n_timesteps == 1:
out.append(f"time: time-invariant file (1 step) at {self.time[0]}")
else:
out.append(
f"time: {str(self.time[0])} - {str(self.time[-1])} ({self.n_timesteps} records)"
)
return str.join("\n", out)

def _read_items(self, filename: str) -> list[ItemInfo]:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,15 @@ def test_read_dfs_time_selection_str():
assert dsr.shape == dsgetitem.shape


def test_open_dfs_repr():
extensions = ["dfsu", "dfs2", "dfs1", "dfs0"]
for ext in extensions:
filename = f"tests/testdata/consistency/oresundHD.{ext}"
dfs = mikeio.open(filename=filename)
text = repr(dfs)
assert "<mikeio" in text


def test_read_dfs_time_selection_str_specific():
extensions = ["dfsu", "dfs2", "dfs1", "dfs0"]
for ext in extensions:
Expand Down

0 comments on commit 2e7f6b5

Please sign in to comment.