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

#315: Fix freeze when printing ForcingModel #372

Merged
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
6 changes: 6 additions & 0 deletions hydrolib/core/io/bc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ def _to_section(self) -> Section:
class Config:
extra = Extra.ignore

def __repr__(self) -> str:
data = dict(self)
data["datablock"] = "<omitted>"
representable = BaseModel.construct(**data)
return str(representable)


class TimeSeries(ForcingBase):
"""Subclass for a .bc file [Forcing] block with timeseries data."""
Expand Down
17 changes: 17 additions & 0 deletions tests/io/test_bc.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ def test_astronomic_values_with_strings_in_datablock_are_parsed_correctly(
f"No validation error should be raised when creating an {cls.__name__}"
)

def test_representation_is_correct(self):
forcing = ForcingBase(
name="some_name",
function="some_function",
quantityunitpair=[QuantityUnitPair(quantity="some_quantity", unit="some_unit")],
datablock=[[1.2, 2.3]]
)

str_representation_as_single = str(forcing)
str_representation_in_list = str([forcing])

# datablock should be omitted when a `ForcingBase` is represented from within a list
expected_result = "comments=Comments() datablock={0} name='some_name' function='some_function' quantityunitpair=[QuantityUnitPair(quantity='some_quantity', unit='some_unit', vertpositionindex=None)]"
assert str_representation_as_single == expected_result.format("[[1.2, 2.3]]")
assert str_representation_in_list == "[{0}]".format(expected_result.format("'<omitted>'"))



class TestT3D:
@pytest.mark.parametrize(
Expand Down