Skip to content

Implement the framework for entities. #258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,7 @@ $RECYCLE.BIN/

tmp/

/.vscode
/.vscode

# test residual
flow360/examples/cylinder/flow360mesh.json
32 changes: 0 additions & 32 deletions flow360/component/simulation/entities_base.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import hashlib
import json
from typing import List, Literal, Optional
from typing import Literal, Optional, Union, get_args

import pydantic as pd
import rich
import yaml
from pydantic import ConfigDict
from pydantic.fields import FieldInfo

from flow360.component.simulation.framework.entity_registry import EntityRegistry
from flow360.component.types import TYPE_TAG_STR
from flow360.error_messages import do_not_modify_file_manually_msg
from flow360.exceptions import Flow360FileError
Expand Down Expand Up @@ -39,14 +39,23 @@ def __init__(self, filename: str = None, **kwargs):

@classmethod
def _handle_dict(cls, **kwargs):
"""Handle dictionary input for the model."""
model_dict = kwargs
model_dict = cls._handle_dict_with_hash(model_dict)
return model_dict

@classmethod
def _handle_file(cls, filename: str = None, **kwargs):
"""Handle file input for the model.

Parameters
----------
filename : str
Full path to the .json or .yaml file to load the :class:`Flow360BaseModel` from.
**kwargs
Keyword arguments to be passed to the model."""
if filename is not None:
return cls.dict_from_file(filename=filename)
return cls._dict_from_file(filename=filename)
return kwargs

@classmethod
Expand All @@ -67,7 +76,7 @@ def __pydantic_init_subclass__(cls, **kwargs) -> None:
"""
model_config = ConfigDict(
##:: Pydantic kwargs
arbitrary_types_allowed=True,
arbitrary_types_allowed=True, # ?
extra="forbid",
frozen=False,
populate_by_name=True,
Expand Down Expand Up @@ -155,8 +164,7 @@ def copy(self, update=None, **kwargs) -> Flow360BaseModel:
"""Copy a Flow360BaseModel. With ``deep=True`` as default."""
if "deep" in kwargs and kwargs["deep"] is False:
raise ValueError("Can't do shallow copy of component, set `deep=True` in copy().")
kwargs.update({"deep": True})
new_copy = pd.BaseModel.model_copy(self, update=update, **kwargs)
new_copy = pd.BaseModel.model_copy(self, update=update, deep=True, **kwargs)
data = new_copy.model_dump()
return self.model_validate(data)

Expand Down Expand Up @@ -195,7 +203,7 @@ def from_file(cls, filename: str) -> Flow360BaseModel:
return cls(filename=filename)

@classmethod
def dict_from_file(cls, filename: str) -> dict:
def _dict_from_file(cls, filename: str) -> dict:
"""Loads a dictionary containing the model from a .json or .yaml file.

Parameters
Expand Down Expand Up @@ -263,7 +271,7 @@ def from_json(cls, filename: str, **parse_obj_kwargs) -> Flow360BaseModel:
-------
>>> params = Flow360BaseModel.from_json(filename='folder/flow360.json') # doctest: +SKIP
"""
model_dict = cls.dict_from_file(filename=filename)
model_dict = cls._dict_from_file(filename=filename)
return cls.model_validate(model_dict, **parse_obj_kwargs)

@classmethod
Expand Down Expand Up @@ -327,7 +335,7 @@ def from_yaml(cls, filename: str, **parse_obj_kwargs) -> Flow360BaseModel:
-------
>>> params = Flow360BaseModel.from_yaml(filename='folder/flow360.yaml') # doctest: +SKIP
"""
model_dict = cls.dict_from_file(filename=filename)
model_dict = cls._dict_from_file(filename=filename)
return cls.model_validate(model_dict, **parse_obj_kwargs)

@classmethod
Expand Down
Loading
Loading