From 2e7f6b5850bb45b5e456c83ab539ec91801b17f5 Mon Sep 17 00:00:00 2001 From: Henrik Andersson Date: Tue, 12 Mar 2024 16:08:10 +0100 Subject: [PATCH] _Dfsu repr --- mikeio/dfsu/_dfsu.py | 31 +++++++++++++------------------ tests/test_consistency.py | 9 +++++++++ 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/mikeio/dfsu/_dfsu.py b/mikeio/dfsu/_dfsu.py index 9b147c8db..008c3006b 100644 --- a/mikeio/dfsu/_dfsu.py +++ b/mikeio/dfsu/_dfsu.py @@ -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""] if self._type is not DfsuFileType.DfsuSpectral0D: if self._type is not DfsuFileType.DfsuSpectral1D: @@ -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]: diff --git a/tests/test_consistency.py b/tests/test_consistency.py index 314da71a1..ce1c9e7de 100644 --- a/tests/test_consistency.py +++ b/tests/test_consistency.py @@ -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 "