Skip to content

Commit

Permalink
#308: Setup for configuring the floating values in a datablock (e,g. …
Browse files Browse the repository at this point in the history
…bc file)
  • Loading branch information
priscavdsluis committed Nov 2, 2022
1 parent a6ac951 commit 686b487
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
4 changes: 2 additions & 2 deletions hydrolib/core/io/dflowfm/bc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ def validate(cls, v):
def _get_identifier(self, data: dict) -> Optional[str]:
return data.get("name")

def _to_section(self) -> Section:
section = super()._to_section()
def _to_section(self, config: SerializerConfig) -> Section:
section = super()._to_section(config)

for quantity in self.quantityunitpair:
for prop in quantity._to_properties():
Expand Down
30 changes: 24 additions & 6 deletions hydrolib/core/io/dflowfm/ini/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .parser import Parser
from .serializer import SerializerConfig, write_ini
from .util import make_list_validator
from hydrolib.core.utils import float_to_str

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -134,7 +135,7 @@ def _convert_value(cls, key: str, v: Any) -> str:
else:
return str(v)

def _to_section(self) -> Section:
def _to_section(self, config: SerializerConfig) -> Section:
props = []
for key, value in self:
if key in self._exclude_fields():
Expand Down Expand Up @@ -165,11 +166,28 @@ class DataBlockINIBasedModel(INIBasedModel):

_make_lists = make_list_validator("datablock")

def _to_section(self) -> Section:
section = super()._to_section()
section.datablock = self.datablock
def _to_section(self, config: SerializerConfig) -> Section:
section = super()._to_section(config)
section.datablock = self._to_datablock(config)
return section

def _to_datablock(self, config: SerializerConfig) -> List[List[str]]:
converted_datablock = []

for row in self.datablock:
converted_row = (DataBlockINIBasedModel._elem_to_str(elem, config) for elem in row)
converted_datablock.append(list(converted_row))

return converted_datablock

@classmethod
def _elem_to_str(cls, elem: Union[float, str], config: SerializerConfig) -> str:
if isinstance(elem, float) and config.number_of_decimals is not None:
return float_to_str(elem, config.number_of_decimals)

return str(elem)



class INIGeneral(INIBasedModel):
_header: Literal["General"] = "General"
Expand Down Expand Up @@ -218,9 +236,9 @@ def _to_document(self) -> Document:
continue
if isinstance(value, list):
for v in value:
sections.append(v._to_section())
sections.append(v._to_section(self.serializer_config))
else:
sections.append(value._to_section())
sections.append(value._to_section(self.serializer_config))
return Document(header_comment=[header], sections=sections)

def _serialize(self, _: dict) -> None:
Expand Down

0 comments on commit 686b487

Please sign in to comment.