Skip to content
Snippets Groups Projects
Commit fcf9a7d7 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

[libs.common.scripts.utils] Define a better type for JSON-able content

parent 6355dc02
No related branches found
No related tags found
1 merge request!46Create common library
...@@ -8,6 +8,7 @@ import logging ...@@ -8,6 +8,7 @@ import logging
import pathlib import pathlib
import re import re
import shutil import shutil
import typing
import lightning.pytorch import lightning.pytorch
import lightning.pytorch.callbacks import lightning.pytorch.callbacks
...@@ -16,6 +17,17 @@ from mednet.libs.common.engine.device import SupportedPytorchDevice ...@@ -16,6 +17,17 @@ from mednet.libs.common.engine.device import SupportedPytorchDevice
logger = logging.getLogger("mednet") logger = logging.getLogger("mednet")
JSONable: typing.TypeAlias = (
typing.Mapping[str, "JSONable"]
| typing.Sequence["JSONable"]
| str
| int
| float
| bool
| None
)
"""Defines types that can be encoded in a JSON string."""
def model_summary( def model_summary(
model: torch.nn.Module, model: torch.nn.Module,
...@@ -198,7 +210,7 @@ def execution_metadata() -> dict[str, int | float | str | dict[str, str] | list[ ...@@ -198,7 +210,7 @@ def execution_metadata() -> dict[str, int | float | str | dict[str, str] | list[
} }
def save_json_with_backup(path: pathlib.Path, data: dict | list) -> None: def save_json_with_backup(path: pathlib.Path, data: JSONable) -> None:
"""Save a dictionary into a JSON file with path checking and backup. """Save a dictionary into a JSON file with path checking and backup.
This function will save a dictionary into a JSON file. It will check to This function will save a dictionary into a JSON file. It will check to
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment