-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from Deltares/feat/308-improve-file-float-for…
…matting #308: Improve file float formatting
- Loading branch information
Showing
23 changed files
with
269 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,37 @@ | ||
from pathlib import Path | ||
from typing import Dict | ||
from typing import Dict, Generator | ||
|
||
from hydrolib.core.basemodel import SerializerConfig | ||
|
||
|
||
class XYZSerializer: | ||
@staticmethod | ||
def serialize(path: Path, data: Dict) -> None: | ||
def serialize(path: Path, data: Dict, config: SerializerConfig) -> None: | ||
""" | ||
Serializes the XYZ data to the file at the specified path. | ||
Attributes: | ||
path (Path): The path to the destination file. | ||
data (Dict): The data to be serialized. | ||
config (SerializerConfig): The serialization configuration. | ||
""" | ||
path.parent.mkdir(parents=True, exist_ok=True) | ||
|
||
space = 1 * " " | ||
format_float = lambda x: f"{x:{config.float_format}}" | ||
|
||
with path.open("w") as f: | ||
for point in data["points"]: | ||
geometry: str = space.join( | ||
[format_float(p) for p in XYZSerializer._get_point_values(point)] | ||
) | ||
if point.comment: | ||
f.write(f"{point.x} {point.y} {point.z} # {point.comment}\n") | ||
f.write(f"{geometry} # {point.comment}\n") | ||
else: | ||
f.write(f"{point.x} {point.y} {point.z}\n") | ||
f.write(f"{geometry}\n") | ||
|
||
@staticmethod | ||
def _get_point_values(point) -> Generator[float, None, None]: | ||
yield point.x | ||
yield point.y | ||
yield point.z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.