Source code for cosapp.utils.validate

from __future__ import annotations
from typing import Any, TYPE_CHECKING

if TYPE_CHECKING:
    from cosapp.base import System, Port


[docs] def validate(system: System) -> tuple[dict[str, str], dict[str, str]]: """Check the validity of the provided `System`. Parameters ---------- - model [cosapp.base.System]: The system to be validated Returns ------- tuple[dict[str, str], dict[str, str]] Warning and error dictionaries; key is the variable name and value the error reason. """ from cosapp.ports.enum import Validity results = system.check() def value_str(value: Any) -> str: try: return f"{value:.5g}" except: return str(value) def msg_dict(level: Validity) -> dict[str, str]: def valid_items(items: tuple[str, Validity]) -> bool: name, validity = items # Second condition below filters inwards and outwards in short name return validity == level and "." in name output = dict() for key, _ in filter(valid_items, results.items()): portname, varname = key.rsplit(".", maxsplit=1) port: Port = system[portname] ground = port.get_validity_ground(level, varname) output[key] = f" = {value_str(port[varname])} not in {ground}" return output return msg_dict(Validity.WARNING), msg_dict(Validity.ERROR)