Skip to content

Commit

Permalink
use the getattr to fetch attributes from the object
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Dec 3, 2024
1 parent 07a5cb8 commit 0c0d2a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
19 changes: 13 additions & 6 deletions hydrolib/tools/ext_old_to_new/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,24 @@ def convert(self, forcing: ExtOldForcing) -> Boundary:
"""
data = {
"quantity": forcing.quantity,
"nodeid": "aNodeId",
"locationFile": forcing.filename,
"locationfile": forcing.filename,
"forcingfile": ForcingModel(),
}
# HINT: not sure if these keywords exists in the old external files or they are new so they are not expected
# here and these following lines should be removed.

if "bndwidth1d" in forcing.__dict__:
data["bndwidth1d"] = forcing.bndwidth1d
if "bndbldepth" in forcing.__dict__:
data["bndbldepth"] = forcing.bndwidth2d
nodeid = getattr(forcing, "nodeid", None)
if nodeid:
data["nodeid"] = nodeid

bndwidth1d = getattr(forcing, "bndwidth1d", None)
bndbldepth = getattr(forcing, "bndbldepth", None)

if bndwidth1d:
data["bndwidth1d"] = bndwidth1d

if bndbldepth:
data["bndbldepth"] = bndbldepth

new_block = Boundary(**data)

Expand Down
2 changes: 1 addition & 1 deletion tests/tools/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ def test_default(self):
assert new_quantity_block.quantity == "waterlevelbnd"
assert new_quantity_block.forcingfile == ForcingModel()
assert new_quantity_block.locationfile == DiskOnlyFileModel(file_name)
assert new_quantity_block.nodeid == "aNodeId"
assert new_quantity_block.nodeid is None
assert new_quantity_block.bndwidth1d is None
assert new_quantity_block.bndbldepth is None

0 comments on commit 0c0d2a6

Please sign in to comment.